src/Security/Voters/SustainabilityOptionVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\SustainabilityOption;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class SustainabilityOptionVoter extends Voter
  7. {
  8.     public const CAN_BE_DELETED  'option.can_be_deleted';
  9.     private const ATTRIBUTES = [
  10.         self::CAN_BE_DELETED,
  11.     ];
  12.     protected function supports($attribute$subject): bool
  13.     {
  14.         return in_array($attributeself::ATTRIBUTES);
  15.     }
  16.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  17.     {
  18.         switch ($attribute) {
  19.             case self::CAN_BE_DELETED:
  20.                 return $this->canBeDeleted($subject);
  21.         }
  22.         throw new \LogicException('Invalid attribute: '.$attribute);
  23.     }
  24.     private function canBeDeleted(SustainabilityOption $option): bool
  25.     {
  26.         if (count($option->getPriceMaps())) return false;
  27.         return true;
  28.     }
  29. }