<?php
namespace App\Security\Voter;
use App\Entity\CoachSport;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class CoachSportVoter extends Voter
{
public const DELETE = 'delete';
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::DELETE])
&& $subject instanceof CoachSport;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return match ($attribute) {
self::DELETE => $subject->getCoach() === $user->getProfile()?->getCoach(),
default => false,
};
}
}