src/Component/Sanctions/ForceTasksListener.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Component\Sanctions;
  4. use App\Controller\LeadRegistrantController;
  5. use App\Controller\MySief\SanctionsController;
  6. use App\Controller\MySief\Substances\Member\SubstancesController as MemberSubstancesController;
  7. use App\Controller\MySief\Substances\NonMember\SubstancesController as NonMemberSubstancesController;
  8. use App\Entity\LeadRegistrant;
  9. use App\Entity\LeadRegistrant\DossierUpdate;
  10. use App\Entity\LegalEntity;
  11. use App\Entity\User;
  12. use App\Service\UserLookup;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  15. class ForceTasksListener
  16. {
  17.     private SanctionsInterface $sanctions;
  18.     private SanctionsController $controller;
  19.     private UserLookup $userLookup;
  20.     private EntityManagerInterface $em;
  21.     public function __construct(
  22.         SanctionsInterface $sanctions,
  23.         SanctionsController $controller,
  24.         UserLookup $userLookup,
  25.         EntityManagerInterface $em
  26.     ) {
  27.         $this->sanctions $sanctions;
  28.         $this->controller $controller;
  29.         $this->userLookup $userLookup;
  30.         $this->em $em;
  31.     }
  32.     public function onKernelController(ControllerEvent $event): void
  33.     {
  34.         $user $this->userLookup->lookup();
  35.         if (
  36.             !$user instanceof User
  37.             || !$this->sanctions->isActive()
  38.             || $user->isStaff()
  39.             || === $user->getValidApprovedEntityCount()
  40.         ) {
  41.             return;
  42.         }
  43.         $controller $event->getController();
  44.         if (is_array($controller)) {
  45.             $controller $controller[0];
  46.         }
  47.         if ($controller instanceof SanctionsController) {
  48.             return;
  49.         }
  50.         $entityId null;
  51.         $attributes $event->getRequest()->attributes;
  52.         if ($controller instanceof MemberSubstancesController) {
  53.             $entityId $attributes->get('entity');
  54.         } elseif ($controller instanceof NonMemberSubstancesController) {
  55.             $entityId $attributes->get('entity');
  56.         } elseif ($controller instanceof LeadRegistrantController) {
  57.             $updateId $attributes->get('update');
  58.             $lrId $attributes->get('leadRegistrant');
  59.             if (null !== $updateId) {
  60.                 $update $this->em->find(DossierUpdate::class, $updateId);
  61.                 $entityId $update->getLeadRegistrant()->getLegalEntity()->getId();
  62.             } elseif (null !== $lrId) {
  63.                 $lr $this->em->find(LeadRegistrant::class, $lrId);
  64.                 $entityId $lr->getLegalEntity()->getId();
  65.             }
  66.         }
  67.         if (null !== $entityId) {
  68.             $entity $this->em->find(LegalEntity::class, $entityId);
  69.             if (=== $entity->getOrderCount()) {
  70.                 return;
  71.             }
  72.             if (!$this->sanctions->isAccessDenied($user$entity)) {
  73.                 return;
  74.             }
  75.         } else {
  76.             if (!$this->sanctions->isAccessDenied($user)) {
  77.                 return;
  78.             }
  79.         }
  80.         $event->getRequest()->attributes->set('sanctions_enforcement'true);
  81.         $event->setController([$this->controller'indexAction']);
  82.         $event->stopPropagation();
  83.     }
  84. }