src/Listener/ValidationListener.php line 211

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Controller\Ajax\TutorialController;
  4. use App\Controller\MySief\LegalEntityController;
  5. use App\Entity\LegalEntity;
  6. use App\Entity\LegalEntity\Billing\ContactPerson;
  7. use App\Entity\StaffRequest;
  8. use App\Entity\User;
  9. use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Doctrine\ORM\Event\LifecycleEventArgs;
  12. use Doctrine\ORM\Events;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  16. use Symfony\Component\HttpKernel\Controller\ErrorController;
  17. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  18. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  19. use Symfony\Component\Routing\RouterInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  22. use Symfony\Component\Validator\Exception\ValidatorException;
  23. use Symfony\Component\Validator\Validator\ValidatorInterface;
  24. class ValidationListener implements EventSubscriberInterfaceWarmableInterface
  25. {
  26.     /**
  27.      * @var bool
  28.      */
  29.     private static $enabled true;
  30.     /**
  31.      * @var ValidatorInterface
  32.      */
  33.     private $validator;
  34.     /**
  35.      * @var TokenStorageInterface
  36.      */
  37.     private $storage;
  38.     /**
  39.      * @var EntityManagerInterface
  40.      */
  41.     private $em;
  42.     /**
  43.      * @var RouterInterface
  44.      */
  45.     private $router;
  46.     /**
  47.      * @var RequestStack
  48.      */
  49.     private $requestStack;
  50.     /**
  51.      * @var LegalEntityController
  52.      */
  53.     private $controller;
  54.     public function __construct(TokenStorageInterface $storageEntityManagerInterface $emRouterInterface $routerValidatorInterface $validatorRequestStack $requestStackLegalEntityController $controllerContainerInterface $container)
  55.     {
  56.         $this->storage $storage;
  57.         $this->em $em;
  58.         $this->router $router;
  59.         $this->validator $validator;
  60.         $this->requestStack $requestStack;
  61.         $this->controller $controller;
  62.         $this->controller->setContainer($container);
  63.     }
  64.     public function getSubscribedEvents()
  65.     {
  66.         return [Events::prePersistEvents::preUpdate];
  67.     }
  68.     private function validate(LifecycleEventArgs $args)
  69.     {
  70.         if (!self::$enabled) {
  71.             return;
  72.         }
  73.         $entity $args->getObject();
  74.         if (defined('IMPORT_RUNNING')) {
  75.             if ($entity instanceof ContactPerson) {
  76.                 return;
  77.             }
  78.         }
  79.         $violations $this->validator->validate($entity);
  80.         if ($violations->count() > 0) {
  81.             $violation $violations->get(0);
  82.             $message sprintf('[%s->%s] %s'get_class($entity), $violation->getPropertyPath(), $violation->getMessage());
  83.             throw new ValidatorException($message);
  84.         }
  85.     }
  86.     public static function disable()
  87.     {
  88.         self::$enabled false;
  89.     }
  90.     public static function enable()
  91.     {
  92.         self::$enabled true;
  93.     }
  94.     public function prePersist(LifecycleEventArgs $args)
  95.     {
  96.         $this->validate($args);
  97.     }
  98.     public function preUpdate(LifecycleEventArgs $args)
  99.     {
  100.         $this->validate($args);
  101.     }
  102.     public function onKernelController(ControllerEvent $event)
  103.     {
  104.         if (!self::$enabled) {
  105.             return;
  106.         }
  107.         $token $this->storage->getToken();
  108.         if (!$token instanceof TokenInterface) {
  109.             return;
  110.         }
  111.         $user $token->getUser();
  112.         if (!$user instanceof User) {
  113.             return;
  114.         }
  115.         if ($user->isStaff()) {
  116.             return;
  117.         }
  118.         $entities $this->em->getRepository(LegalEntity::class)->findBy(['user' => $user]);
  119.         $invalidEntities = [];
  120.         $pending = [];
  121.         if (count($entities) > 0) {
  122.             foreach ($entities as $entity) {
  123.                 /**
  124.                  * @var LegalEntity $entity
  125.                  */
  126.                 $billingRequest $this->em->getRepository(StaffRequest\NonMemberAlterLicensingRequest::class)->findOneBy(['legalEntity' => $entity'status' => 'pending']);
  127.                 $licensingRequest $this->em->getRepository(StaffRequest\NonMemberAlterLicensingRequest::class)->findOneBy(['legalEntity' => $entity'status' => 'pending']);
  128.                 if ($billingRequest instanceof StaffRequest\NonMemberAlterBillingRequest || $licensingRequest instanceof StaffRequest\NonMemberAlterLicensingRequest) {
  129.                     $pending[] = $entity;
  130.                     ValidationListener::disable();
  131.                     $violations $this->validator->validate($entity);
  132.                     if ($violations->count() > 0) {
  133.                         $entity->setLimited(true);
  134.                     }
  135.                     $this->em->persist($entity);
  136.                     $this->em->flush();
  137.                     ValidationListener::enable();
  138.                     continue;
  139.                 }
  140.                 /** @var LegalEntity $entity */
  141.                 $violations $this->validator->validate($entity);
  142.                 if ($violations->count() > 0) {
  143.                     $invalidEntities[] = $entity->getId();
  144.                 } else {
  145.                     $entity->setLimited(false);
  146.                 }
  147.             }
  148.         }
  149.         $session $this->requestStack->getSession();
  150.         if (count($invalidEntities) > 0) {
  151.             $session->remove('validation/pending');
  152.             $attribute 'validation/multipleInvalid';
  153.             if (=== count($invalidEntities)) {
  154.                 $message "An invalid legal entity was found (some compulsory information is missing), please correct the data for the highlighted legal entity and await approval. To fill in the data, click on the edit button next to the legal entity's name.";
  155.             } else {
  156.                 $message sprintf("%s invalid legal entities were found (some compulsory information is missing), please correct the data for the highlighted legal entities and await approval. To fill in the data, click on the edit button next to the legal entity's name"count($invalidEntities));
  157.             }
  158.             $session->set('validation/invalidEntities'$invalidEntities);
  159.             $data true;
  160.         } elseif (count($pending) > 0) {
  161.             $session->remove('validation/multipleInvalid');
  162.             $session->remove('validation/invalidEntities');
  163.             $attribute 'validation/pending';
  164.             $message sprintf("You have %s pending alter request's, awaiting staff approval."count($pending));
  165.             $data true;
  166.         } else {
  167.             $session->remove('validation/pending');
  168.             $session->remove('validation/multipleInvalid');
  169.             $session->remove('validation/invalidEntities');
  170.             return;
  171.         }
  172.         $controller $event->getController();
  173.         if (!is_array($controller)) {
  174.             return;
  175.         }
  176.         if ($controller[0] instanceof TutorialController) {
  177.             return;
  178.         }
  179.         if ($controller[0] instanceof ErrorController) {
  180.             // We always need to show error pages
  181.             $event->getRequest()->attributes->remove($attribute);
  182.         } elseif ($controller[0] instanceof LegalEntityController) {
  183.             $event->getRequest()->attributes->set($attributefalse);
  184.         } else {
  185.             // Redirect to substances
  186.             $session->set($attribute$message);
  187.             if (!isset($entity)) {
  188.                 throw new \Exception('entity is not set');
  189.             }
  190.             if (!$entity->isLimited()) {
  191.                 return;
  192.             }
  193.             $event->getRequest()->attributes->set($attribute$data);
  194.             $event->setController([$this->controller'indexAction']);
  195.         }
  196.     }
  197.     public function onKernelResponse(ResponseEvent $event)
  198.     {
  199.     }
  200.     public function warmUp($cacheDir)
  201.     {
  202.         return [TokenStorageInterface::class, EntityManagerInterface::class, RouterInterface::class, ValidatorInterface::class, RequestStack::class, LegalEntityController::class, ContainerInterface::class];
  203.     }
  204. }