src/Security/Voter/CanSetRate.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Project\DeveloperRate;
  5. use App\Entity\User;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class CanSetRate extends Voter
  10. {
  11.     /**
  12.      * {@inheritDoc}
  13.      */
  14.     protected function supports(string $attribute$subject): bool
  15.     {
  16.         return $attribute === 'CAN_SET_RATE' && $subject instanceof DeveloperRate;
  17.     }
  18.     /**
  19.      * @param string              $attribute CAN_SET_RATE
  20.      * @param DeveloperRate|mixed $subject
  21.      */
  22.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  23.     {
  24.         $user $token->getUser();
  25.         if (!$user instanceof User) {
  26.             return false;
  27.         }
  28.         $manager $user->getManager();
  29.         if ($manager === null) {
  30.             return false;
  31.         }
  32.         $managers = ($project $subject->getProject()) !== null $project->getManagers() : new ArrayCollection();
  33.         // TRUE if this collection contained the specified element, FALSE otherwise
  34.         /** @psalm-suppress InvalidArgument */
  35.         return $managers->removeElement($manager);
  36.     }
  37. }