<?php
declare(strict_types=1);
namespace App\UI\Merchant\Security;
use App\Domain\Merchant\Model\Merchant;
use App\Domain\Site\Model\Site;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SiteVoter extends Voter
{
public const EDIT = 'edit';
protected function supports(string $attribute, mixed $subject): bool
{
if (!in_array($attribute, [self::EDIT])) {
return false;
}
if (!$subject instanceof Site) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof Merchant) {
return true;
}
/** @var Site $site */
$site = $subject;
return match ($attribute) {
self::EDIT => $this->canEdit($site, $user),
default => throw new \LogicException('This code should not be reached!')
};
}
private function canEdit(Site $site, Merchant $merchant): bool
{
if ($site->getMerchant() === $merchant) {
return true;
}
return false;
}
}