src/Security/Voter/CloneVoter.php line 10

  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authorization\Voter\Voter,
  4.     Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use App\Lib\Actions,
  6.     App\Repository\RegistryEntryRepository;
  7. final class CloneVoter extends Voter
  8. {
  9.     const SINCE '10 secs';
  10.     const MESSAGE 'Clone operation can be performed once a ' self::SINCE '.';
  11.     private RegistryEntryRepository $repository;
  12.     public function __construct(RegistryEntryRepository $repository)
  13.     {
  14.         $this->repository $repository;
  15.     }
  16.     protected function supports($attribute$subject): bool
  17.     {
  18.         if (Actions::CLONE !== $attribute) {
  19.             return false;
  20.         }
  21.         return true;
  22.     }
  23.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  24.     {
  25.         $operations $this->repository->findAllCloneOperations(
  26.             admin$token->getUser(),
  27.             since: new \DateTimeImmutable('-' self::SINCE)
  28.         );
  29.         if (count($operations)) {
  30.             return false;
  31.         }
  32.         return true;
  33.     }
  34. }