src/Security/Voter/ProcessFormVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Form;
  4. use App\Service\ProcessFormService;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class ProcessFormVoter extends Voter
  8. {
  9.     public const PROCESS_CREATE 'PROCESS_CREATE';
  10.     public const PROCESS_EDIT 'PROCESS_EDIT';
  11.     public const PROCESS_SUCCESS 'PROCESS_SUCCESS';
  12.     /** @var ProcessFormService $service */
  13.     private $service;
  14.     /**
  15.      * @param ProcessFormService $service
  16.      */
  17.     public function __construct(ProcessFormService $service)
  18.     {
  19.         $this->service $service;
  20.     }
  21.     /**
  22.      * @param string $attribute
  23.      * @param $subject
  24.      * @return bool
  25.      */
  26.     protected function supports(string $attribute$subject): bool
  27.     {
  28.         return in_array($attribute, [self::PROCESS_CREATEself::PROCESS_EDITself::PROCESS_SUCCESS]) && $subject instanceof Form;
  29.     }
  30.     /**
  31.      * @param string $attribute
  32.      * @param $subject
  33.      * @param TokenInterface $token
  34.      * @return bool
  35.      */
  36.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  37.     {
  38.         switch ($attribute) {
  39.             case self::PROCESS_CREATE:
  40.                 return $this->canCreate($subject);
  41.             case self::PROCESS_EDIT:
  42.                 return $this->canEdit($subject);
  43.             case self::PROCESS_SUCCESS:
  44.                 return $this->canSuccess($subject);
  45.         }
  46.         return false;
  47.     }
  48.     /**
  49.      * @param Form $form
  50.      * @return bool
  51.      */
  52.     private function canCreate(Form $form): bool
  53.     {
  54.         return $this->service->checkActiveStatus($form);
  55.     }
  56.     /**
  57.      * @param Form $form
  58.      * @return bool
  59.      */
  60.     private function canEdit(Form $form): bool
  61.     {
  62.         return $this->service->checkActiveStatus($form);
  63.     }
  64.     /**
  65.      * @param Form $form
  66.      * @return bool
  67.      */
  68.     private function canSuccess(Form $form): bool
  69.     {
  70.         return $this->service->checkActiveStatus($form);
  71.     }
  72. }