src/EventSubscriber/ParticipationSubscriber.php line 115

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Entity\Participation;
  6. use App\Service\CodeDotationService;
  7. use App\Service\ConsolationPrizeService;
  8. use App\Service\WinningTimeService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Validator\Validator\ValidatorInterface;
  17. final class ParticipationSubscriber implements EventSubscriberInterface
  18. {
  19.     public function __construct(
  20.         private EntityManagerInterface $manager,
  21.         private ValidatorInterface $validator,
  22.         private WinningTimeService $drawService,
  23.         private ConsolationPrizeService $consolationService,
  24.         private CodeDotationService $codeService,
  25.         private Security $security
  26.     )
  27.     {
  28.     }
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             KernelEvents::VIEW => [
  33.                 ['checkExists'EventPriorities::PRE_WRITE],
  34.                 ['draw'EventPriorities::POST_WRITE],
  35.                 ['setUser'EventPriorities::PRE_VALIDATE],
  36.             ],
  37.         ];
  38.     }
  39.     public function checkExists(ViewEvent $event)
  40.     {
  41.         $this->manager
  42.             ->getConnection()
  43.             ->prepare('SET SQL_BIG_SELECTS=1')
  44.             ->executeQuery()
  45.         ;
  46.         // get new participation
  47.         $newPart $event->getControllerResult();
  48.         if(!$newPart instanceof Participation) {
  49.             return;
  50.         }
  51.         // check participation exists with external_id
  52.         $partRepo $this->manager->getRepository(Participation::class);
  53.         $existingPart $partRepo
  54.             ->findOneBy([
  55.                 "operation" => $newPart->getOperation(),
  56.                 "externalId" => $newPart->getExternalId()
  57.             ])
  58.         ;
  59.         // if not exists do nothing
  60.         if( !$existingPart instanceof Participation) {
  61.             return;
  62.         }
  63.         // if exists replace new one by existing one
  64.         $event->setControllerResult($existingPart);
  65.     }
  66.     public function draw(ViewEvent $event)
  67.     {
  68.         $participation $event->getControllerResult();
  69.         $method $event->getRequest()->getMethod();
  70.         if (!$participation instanceof Participation || Request::METHOD_POST !== $method) {
  71.             return;
  72.         }
  73.         
  74.         $violations $this->validator->validate($participation);
  75.         if (!== count($violations)) {
  76.             throw new ValidationException($violations);
  77.         }
  78.         ////  Fix doublons
  79.         $ig $participation->getIg();
  80.         $cp $participation->getConsolationPrize();
  81.         if( $ig || $cp ) {
  82.             $code $participation->getCode();
  83.             if(!$code) {
  84.                 $this->codeService->get($participation);
  85.             }
  86.             return;
  87.         }
  88.         /////
  89.         // this service flush data in db
  90.         $this->drawService->draw($participation);
  91.         if ($participation->getOperation()->getConsolation() && $participation->getStatus() === Participation::STATUS_LOST) {
  92.             // this service flush data in db
  93.             $this->consolationService->draw($participation);
  94.         }
  95.         $this->codeService->get($participation);
  96.     }
  97.     public function setUser(ViewEvent $event)
  98.     {
  99.         $user $this->security->getUser();
  100.         $request $event->getRequest();
  101.         $object $event->getControllerResult();
  102.         $method $request->getMethod();
  103.         if ($method !== Request::METHOD_POST)
  104.         {
  105.             return;
  106.         }
  107.         if (!$object instanceof Participation)
  108.         {
  109.             return;
  110.         }
  111.         if (!$user instanceof UserInterface) {
  112.             throw new AuthenticationException("Token expired");
  113.         }
  114.         $object->setUser($user);
  115.     }
  116. }