src/Domain/User/Voter/UserAccessProfilVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Domain\User\Voter;
  3. use App\Domain\Application\Voter\AbstractVoter;
  4. use App\Domain\User\Entity\User;
  5. use App\Domain\User\Entity\UserAccessProfil;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. class UserAccessProfilVoter extends AbstractVoter
  8. {
  9.     public const DELETE 'delete';
  10.     public function supportsType(string $subjectType): bool
  11.     {
  12.         return is_a($subjectTypeUserAccessProfil::class, true);
  13.     }
  14.     public function supportsAttribute(string $attribute): bool
  15.     {
  16.         return \in_array($attribute, [
  17.             self::DELETE,
  18.         ]);
  19.     }
  20.     /**
  21.      * @param UserAccessProfil $profil
  22.      */
  23.     protected function voteOnAttribute(string $attributemixed $profilTokenInterface $token): bool
  24.     {
  25.         $user $token->getUser();
  26.         if (!$user instanceof User) {
  27.             return false;
  28.         }
  29.         return match ($attribute) {
  30.             self::DELETE => $this->canDelete($user$profil),
  31.             default => false,
  32.         };
  33.     }
  34.     protected function canDelete(User $userUserAccessProfil $profil): bool
  35.     {
  36.         return $profil->getId()
  37.             && $profil->getUsers()->isEmpty();
  38.     }
  39. }