Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

1.1 KB
Raw Blame
<?php

declare(strict_types=1);

namespace HiEvents\Services\Domain\Location;

use HiEvents\Exceptions\ResourceNotFoundException;
use HiEvents\Repository\Interfaces\LocationRepositoryInterface;

class LocationOwnershipValidator
{
    public function __construct(
        private readonly LocationRepositoryInterface $locationRepository,
        private readonly LocationLockService $locationLockService,
    ) {}

    /**
     * @throws ResourceNotFoundException
     */
    public function assertOwnedBy(?int $locationId, int $organizerId, int $accountId): void
    {
        if ($locationId === null) {
            return;
        }

        $this->locationLockService->acquireSharedTransactionLock($locationId);

        $location = $this->locationRepository->findFirstWhere([
            'id' => $locationId,
            'account_id' => $accountId,
            'organizer_id' => $organizerId,
        ]);

        if ($location === null) {
            throw new ResourceNotFoundException(
                __('Location :id not found', ['id' => $locationId]),
            );
        }
    }
}