src/Listener/NonMember/TransferListener.php line 214

Open in your IDE?
  1. <?php
  2. namespace App\Listener\NonMember;
  3. use App\Controller\Ajax\TutorialController;
  4. use App\Controller\MySief\LegalEntityController;
  5. use App\Controller\MySief\TransferController;
  6. use App\Entity\LegalEntity;
  7. use App\Entity\Notification;
  8. use App\Entity\Notification\NonMember\TransferIncomingNotification;
  9. use App\Entity\TransferEntity;
  10. use App\Entity\User;
  11. use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\ORM\Event\LifecycleEventArgs;
  14. use Doctrine\ORM\Event\OnFlushEventArgs;
  15. use Doctrine\ORM\Events;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  18. use Symfony\Component\HttpKernel\Controller\ErrorController;
  19. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  20. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  21. use Symfony\Component\Routing\RouterInterface;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  24. use Symfony\Component\Validator\Validator\ValidatorInterface;
  25. class TransferListener implements EventSubscriberInterfaceWarmableInterface
  26. {
  27.     /**
  28.      * @var TokenStorageInterface
  29.      */
  30.     private $storage;
  31.     /**
  32.      * @var EntityManagerInterface
  33.      */
  34.     private $em;
  35.     /**
  36.      * @var RouterInterface
  37.      */
  38.     private $router;
  39.     /**
  40.      * @var ValidatorInterface
  41.      */
  42.     private $validator;
  43.     public function __construct(TokenStorageInterface $storageEntityManagerInterface $emRouterInterface $routerValidatorInterface $validator)
  44.     {
  45.         $this->storage $storage;
  46.         $this->em $em;
  47.         $this->router $router;
  48.         $this->validator $validator;
  49.     }
  50.     public function getSubscribedEvents()
  51.     {
  52.         return [Events::postPersistEvents::postUpdateEvents::postRemoveEvents::onFlush];
  53.     }
  54.     public function postPersist(LifecycleEventArgs $args)
  55.     {
  56.         if (!$args->getEntity() instanceof TransferEntity) {
  57.             return;
  58.         }
  59.         $this->processRequest($args->getEntityManager(), $args->getEntity());
  60.     }
  61.     public function postUpdate(LifecycleEventArgs $args)
  62.     {
  63.         if (!$args->getEntity() instanceof TransferEntity) {
  64.             return;
  65.         }
  66.         $this->processRequest($args->getEntityManager(), $args->getEntity());
  67.     }
  68.     public function postRemove(LifecycleEventArgs $args)
  69.     {
  70.     }
  71.     public function onFlush(OnFlushEventArgs $args)
  72.     {
  73.     }
  74.     private function processRequest(EntityManagerInterface $emTransferEntity $transfer)
  75.     {
  76.         if (!$transfer->getSend()) {
  77.             return;
  78.         }
  79.         $repository $em->getRepository(Notification::class);
  80.         $user $this->em->getRepository(User::class)->findUser($transfer->getUserEmail());
  81.         if (!$user instanceof User) {
  82.             return;
  83.         }
  84.         if ($transfer->getAwaitingApproval()) {
  85.             $notifications $repository->findBy(['internalCode' => $transfer->getId()]);
  86.             foreach ($notifications as $notification) {
  87.                 if (!$notification instanceof TransferIncomingNotification) {
  88.                     continue;
  89.                 }
  90.                 $repository->removeByInternalCode($notification);
  91.                 break;
  92.             }
  93.         } elseif (!$transfer->isAccepted()) {
  94.             // show notification
  95.             $notification = (new TransferIncomingNotification())
  96.                 ->setup($this->em$user$transfer)
  97.                 ->setUser($user);
  98.             // Remove the existing notification (if there is one)
  99.             $repository->removeByInternalCode($notification);
  100.             // Create a new one if one or more contracts require acceptance
  101.             /** @var TransferIncomingNotification $notification */
  102.             if ($notification->getPending() > 0) {
  103.                 $repository->notifyUser($notification);
  104.             }
  105.         }
  106.     }
  107.     public function onKernelController(ControllerEvent $event)
  108.     {
  109.         $token $this->storage->getToken();
  110.         if (!$token instanceof TokenInterface) {
  111.             return;
  112.         }
  113.         $user $token->getUser();
  114.         if (!$user instanceof User) {
  115.             return;
  116.         }
  117.         $transferEntity $this->em->getRepository(TransferEntity::class)->findOneBy(['userEmail' => $user->getEmail(), 'destinationEntityId' => null]);
  118.         if (!$transferEntity instanceof TransferEntity) {
  119.             return;
  120.         }
  121.         foreach ($user->getEntities() as $entity) {
  122.             if ($entity->isNonMember()) {
  123.                 if (!$entity->getInitialOrder()) {
  124.                     continue;
  125.                 }
  126.                 if (!$entity->getLicenseAgreement()->isAccepted()) {
  127.                     if ($entity->getLicenseAgreement()->getFile()->hasFile()) {
  128.                         return;
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.         if (!$transferEntity->getSend()) {
  134.             return;
  135.         }
  136.         $controller $event->getController();
  137.         $entities $this->em->getRepository(LegalEntity::class)->findBy(['user' => $user]);
  138.         if (count($entities) > 0) {
  139.             foreach ($entities as $entity) {
  140.                 $violations $this->validator->validate($entity);
  141.                 if ($violations->count() > 0) {
  142.                     return;
  143.                 }
  144.             }
  145.         }
  146.         if ($transferEntity->getDestinationEntityId()) {
  147.             $entity $this->em->getRepository(LegalEntity::class)->find($transferEntity->getDestinationEntityId());
  148.         } else {
  149.             $entity '';
  150.         }
  151.         $pending $this->em->createQueryBuilder()
  152.             ->from(TransferEntity::class, 'tr')
  153.             ->select('COUNT(tr)')
  154.             ->where('tr.userEmail = :user')
  155.             ->setParameter('user'$user->getEmail())
  156.             ->andWhere('tr.accepted = FALSE')
  157.             ->getQuery()->useQueryCache(true)->getSingleScalarResult();
  158.         if (=== $pending && $entity instanceof LegalEntity) {
  159.             return;
  160.         }
  161.         if (!is_array($controller)) {
  162.             return;
  163.         }
  164.         if ($controller[0] instanceof TutorialController) {
  165.             return;
  166.         }
  167.         if ('CancelTransferEntityCreation' == $controller[1]) {
  168.             $transferEntity->setAccepted(false);
  169.         }
  170.         if (!$transferEntity->isAccepted()) {
  171.             if (count($entities) > 1) {
  172.                 return;
  173.             }
  174.         }
  175.         if (!$entity instanceof LegalEntity && !$transferEntity->isAccepted()) {
  176.             if ($controller[0] instanceof ErrorController) {
  177.                 // We always need to show error pages
  178.                 $event->getRequest()->attributes->remove('new');
  179.             } elseif ($controller[0] instanceof TransferController) {
  180.                 $event->getRequest()->attributes->set('new'false);
  181.             } else {
  182.                 // Redirect to overview transfer page
  183.                 $event->getRequest()->attributes->set('new'true);
  184.             }
  185.         } elseif (!$entity instanceof LegalEntity && $transferEntity->isAccepted()) {
  186.             if ($controller[0] instanceof ErrorController) {
  187.                 // We always need to show error pages
  188.                 $event->getRequest()->attributes->remove('newAccepted');
  189.             } elseif ($controller[0] instanceof LegalEntityController) {
  190.                 $event->getRequest()->attributes->set('newAccepted'false);
  191.             } else {
  192.                 // Redirect to create entity page
  193.                 $event->getRequest()->attributes->set('newAccepted'true);
  194.                 $event->getRequest()->attributes->set('member'$transferEntity->getMember());
  195.             }
  196.         }
  197.     }
  198.     public function onKernelResponse(ResponseEvent $event)
  199.     {
  200.         if (true === $event->getRequest()->attributes->get('new')) {
  201.             $url $this->router->generate('my-sief-page.overview.transfer', [], RouterInterface::ABSOLUTE_URL);
  202.             $event->setResponse(new RedirectResponse($url));
  203.         } elseif (true === $event->getRequest()->attributes->get('newAccepted')) {
  204.             $url $this->router->generate('my-sief-page.legal-entities.create', ['step' => 1'member' => (int) $event->getRequest()->attributes->get('member')], RouterInterface::ABSOLUTE_URL);
  205.             $event->setResponse(new RedirectResponse($url));
  206.         }
  207.     }
  208.     public function warmUp($cacheDir): array
  209.     {
  210.         return [TokenStorageInterface::class, EntityManagerInterface::class, RouterInterface::class, ValidatorInterface::class];
  211.     }
  212. }