src/AppBundle/Security/BarAssociationVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Security;
  3. use App\UserRegistration\LawFirmSubscriptionPlanEnum;
  4. use AppBundle\Entity\LawFirm;
  5. use AppBundle\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class BarAssociationVoter extends Voter
  9. {
  10.     public const LAW_FIRM_FROM_BAR_ASSOCIATION 'law_firm.from_bar_association';
  11.     public function isFromBarAssociation(LawFirm $lawFirm): bool
  12.     {
  13.         return $lawFirm->getOffer() !== null && $lawFirm->getSubscriptionPlan() === LawFirmSubscriptionPlanEnum::SPECIAL_OFFER;
  14.     }
  15.     protected function supports($attribute$subject)
  16.     {
  17.         if (self::LAW_FIRM_FROM_BAR_ASSOCIATION === $attribute && (null === $subject || $subject instanceof LawFirm)) {
  18.             return true;
  19.         }
  20.         return false;
  21.     }
  22.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  23.     {
  24.         $user $token->getUser();
  25.         if (!$user instanceof User) {
  26.             return false;
  27.         }
  28.         switch ($attribute) {
  29.             case self::LAW_FIRM_FROM_BAR_ASSOCIATION:
  30.                 return $this->isFromBarAssociation($subject ?? $user->getLawFirm());
  31.             default:
  32.                 throw new \LogicException('This code should not be reached');
  33.         }
  34.     }
  35. }