src/AppBundle/Security/FreemiumVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Security;
  3. use AppBundle\Entity\LawFirm;
  4. use AppBundle\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. /**
  8.  * Class FreemiumVoter
  9.  */
  10. class FreemiumVoter extends Voter
  11. {
  12.     public const LAW_FIRM_FROM_FREEMIUM 'law_firm.from_freemium';
  13.     public function isFromFreemium(LawFirm $lawFirm): bool
  14.     {
  15.         return $lawFirm->getZohoId() !== null;
  16.     }
  17.     /**
  18.      * {@inheritdoc}
  19.      */
  20.     protected function supports($attribute$subject): bool
  21.     {
  22.         if (self::LAW_FIRM_FROM_FREEMIUM === $attribute && (null === $subject || $subject instanceof LawFirm)) {
  23.             return true;
  24.         }
  25.         return false;
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  31.     {
  32.         $user $token->getUser();
  33.         if (!$user instanceof User) {
  34.             return false;
  35.         }
  36.         switch ($attribute) {
  37.             case self::LAW_FIRM_FROM_FREEMIUM:
  38.                 return $this->isFromFreemium($subject ?? $user->getLawFirm());
  39.             default:
  40.                 throw new \LogicException('This code should not be reached');
  41.         }
  42.     }
  43. }