<?php
namespace App\Security\Voter;
use App\Entity\Project;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class ProjectVoter extends AbstractVoter
{
public const VIEW = 'Project:View';
public const EDIT = 'Project:Edit';
private ?Project $project = null;
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::VIEW, self::EDIT])
&& $subject instanceof Project;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$this->user = $token->getUser();
$this->project = $subject;
return match ($attribute) {
self::VIEW => $this->canViewProject(),
self::EDIT => $this->canEditProject(),
default => false,
};
}
private function canViewProject(): bool
{
return $this->security->isGranted(MagasinVoter::VIEW, subject: $this->project->getMagasin());
}
private function canEditProject(): bool
{
return $this->security->isGranted(MagasinVoter::EDIT, subject: $this->project->getMagasin());
}
}