src/Security/SuperheroOffice/StatusVoter.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Security\SuperheroOffice;
  3. use App\Entity\SuperheroOffice\UserOffice;
  4. use App\Handler\SuperheroOffice\ShareholderHandler;
  5. use App\Handler\SuperheroOffice\UserOfficeHandler;
  6. use Psr\Container\ContainerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  11. /**
  12.  * Class StatusVoter
  13.  *
  14.  * @package App\Security\SuperheroOffice
  15.  */
  16. class StatusVoter extends Voter implements ServiceSubscriberInterface
  17. {
  18.     /**
  19.      * @var ContainerInterface
  20.      */
  21.     private $container;
  22.     /**
  23.      * StatusVoter constructor.
  24.      *
  25.      * @param ContainerInterface $container
  26.      */
  27.     public function __construct(ContainerInterface $container)
  28.     {
  29.         $this->container $container;
  30.     }
  31.     /**
  32.      * @param string $attribute
  33.      * @param mixed  $subject
  34.      *
  35.      * @return bool
  36.      */
  37.     protected function supports($attribute$subject)
  38.     {
  39.         return array_key_exists($attributeUserOffice::STATUSES)
  40.             && $subject instanceof Request;
  41.     }
  42.     /**
  43.      * @param string         $attribute
  44.      * @param mixed          $subject
  45.      * @param TokenInterface $token
  46.      *
  47.      * @return bool
  48.      * @throws \Doctrine\ORM\ORMException
  49.      */
  50.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  51.     {
  52.         $userOffice $this->container->get(UserOfficeHandler::class)->getUserOffice();
  53.         $shareholderHandler $this->container->get(ShareholderHandler::class);
  54.         switch ($attribute) {
  55.             case 'started':
  56.                 if ($userOffice->getStatus() === UserOffice::STATUSES['fill_business_information']) {
  57.                     $shareholderHandler->entityHandler->fullSave($userOffice->setStatus(UserOffice::STATUSES['started']));
  58.                     return true;
  59.                 }
  60.                 return false;
  61.             case 'fill_business_information':
  62.                 return
  63.                     (
  64.                         $userOffice->getRegistrationType() !== UserOffice::REGISTRATION_TYPES['banking'] ||
  65.                         $userOffice->getRegistrationType() !== UserOffice::REGISTRATION_TYPES['switching']
  66.                     )
  67.                     &&
  68.                     (
  69.                         $userOffice->getStatus() === UserOffice::STATUSES['declined'] ||
  70.                         $userOffice->getStatus() === UserOffice::STATUSES['started'] ||
  71.                         $userOffice->getStatus() === UserOffice::STATUSES['fill_shareholders']
  72.                     )
  73.                 ;
  74.             case 'fill_shareholders':
  75.                 return
  76.                     (
  77.                         $userOffice->getRegistrationType() === UserOffice::REGISTRATION_TYPES['company'] ||
  78.                         $userOffice->getRegistrationType() === UserOffice::REGISTRATION_TYPES['both']
  79.                     )
  80.                     &&
  81.                     (
  82.                         $userOffice->getStatus() === UserOffice::STATUSES['fill_shareholders']
  83.                     )
  84.                 ;
  85.             case 'waiting_letter_of_engagement':
  86.                 return
  87.                     (
  88.                         $userOffice->getShareholders()->count() > &&
  89.                         $userOffice->getStatus() === UserOffice::STATUSES['fill_shareholders']
  90.                     )
  91.                     ||
  92.                     (
  93.                         $userOffice->getStatus() === UserOffice::STATUSES['setup_company']
  94.                     )
  95.                 ;
  96.             case 'setup_company':
  97.                 return $userOffice->getStatus() === UserOffice::STATUSES['started'] ||
  98.                     $userOffice->getStatus() === UserOffice::STATUSES['declined'];
  99.         }
  100.         return false;
  101.     }
  102.     /**
  103.      * @return array
  104.      */
  105.     public static function getSubscribedServices(): array
  106.     {
  107.         return [
  108.             UserOfficeHandler::class,
  109.             ShareholderHandler::class
  110.         ];
  111.     }
  112. }