<?php
namespace App\Domain\User\Voter;
use App\Domain\Application\Voter\AbstractVoter;
use App\Domain\User\Entity\User;
use App\Domain\User\Entity\UserAccessProfil;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class UserAccessProfilVoter extends AbstractVoter
{
public const DELETE = 'delete';
public function supportsType(string $subjectType): bool
{
return is_a($subjectType, UserAccessProfil::class, true);
}
public function supportsAttribute(string $attribute): bool
{
return \in_array($attribute, [
self::DELETE,
]);
}
/**
* @param UserAccessProfil $profil
*/
protected function voteOnAttribute(string $attribute, mixed $profil, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
return match ($attribute) {
self::DELETE => $this->canDelete($user, $profil),
default => false,
};
}
protected function canDelete(User $user, UserAccessProfil $profil): bool
{
return $profil->getId()
&& $profil->getUsers()->isEmpty();
}
}