src/Security/SuperheroOffice/CommunicationVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\SuperheroOffice;
  3. use App\Entity\SuperheroOffice\Communication;
  4. use App\Handler\SuperheroOffice\UserOfficeHandler;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. /**
  8.  * Class CommunicationVoter
  9.  *
  10.  * @package App\Security\SuperheroOffice
  11.  */
  12. class CommunicationVoter extends Voter
  13. {
  14.     const VIEW     'view';
  15.     const COMPLETE 'complete';
  16.     /**
  17.      * base voter actions
  18.      */
  19.     private const ATTRIBUTES = [
  20.         self::VIEW,
  21.         self::COMPLETE
  22.     ];
  23.     /**
  24.      * @var UserOfficeHandler
  25.      */
  26.     private $userOfficeHandler;
  27.     /**
  28.      * ShareholderVoter constructor.
  29.      *
  30.      * @param UserOfficeHandler $userOfficeHandler
  31.      */
  32.     public function __construct(UserOfficeHandler $userOfficeHandler)
  33.     {
  34.         $this->userOfficeHandler $userOfficeHandler;
  35.     }
  36.     /**
  37.      * @param string $attribute
  38.      * @param mixed  $subject
  39.      *
  40.      * @return bool
  41.      */
  42.     protected function supports($attribute$subject)
  43.     {
  44.         return $subject instanceof Communication
  45.             && in_array($attributeself::ATTRIBUTES);
  46.     }
  47.     /**
  48.      * @param                $attribute
  49.      * @param Communication  $communication
  50.      * @param TokenInterface $token
  51.      *
  52.      * @return bool
  53.      */
  54.     protected function voteOnAttribute(
  55.         $attribute,
  56.         $communication,
  57.         TokenInterface $token
  58.     ): bool {
  59.         switch ($attribute) {
  60.             case self::COMPLETE:
  61.             case self::VIEW:
  62.                 return $this
  63.                     ->userOfficeHandler
  64.                     ->getUserOffice()
  65.                     ->getCommunications()
  66.                     ->contains($communication);
  67.                 break;
  68.         }
  69.         throw new \LogicException('Invalid attribute: ' $attribute);
  70.     }
  71. }