src/EventListener/SendSignCodeSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Api\Dto\SendSignCode;
  5. use App\Factory\SignatureFactory;
  6. use App\Service\SignOperation;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. /**
  13.  * Class SendSignCodeSubscriber.
  14.  */
  15. final class SendSignCodeSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     private $manager;
  21.     /**
  22.      * @var SignatureFactory
  23.      */
  24.     private $factory;
  25.     /**
  26.      * @var SignOperation
  27.      */
  28.     private $signOperation;
  29.     /**
  30.      * SendSignCodeSubscriber constructor.
  31.      *
  32.      * @param EntityManagerInterface $manager
  33.      * @param SignatureFactory       $factory
  34.      * @param SignOperation          $signOperation
  35.      */
  36.     public function __construct(EntityManagerInterface $managerSignatureFactory $factorySignOperation $signOperation)
  37.     {
  38.         $this->manager $manager;
  39.         $this->factory $factory;
  40.         $this->signOperation $signOperation;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             KernelEvents::VIEW => ['sendSignatureCode'EventPriorities::POST_VALIDATE],
  49.         ];
  50.     }
  51.     /**
  52.      * @param GetResponseForControllerResultEvent $event
  53.      *
  54.      * @throws \Doctrine\ORM\ORMException
  55.      */
  56.     public function sendSignatureCode(GetResponseForControllerResultEvent $event)
  57.     {
  58.         $request $event->getRequest();
  59.         if ('api_send_sign_codes_post_collection' !== $request->attributes->get('_route')) {
  60.             return;
  61.         }
  62.         /**
  63.  * @var SendSignCode $sendSignCode
  64. */
  65.         $sendSignCode $event->getControllerResult();
  66.         $signature $this->factory->createNew($sendSignCode->operation);
  67.         $signature->setCode(rand(1000099999));
  68.         $this->manager->persist($signature);
  69.         $this->manager->flush();
  70.         $this->signOperation->sendCode($signature);
  71.         $event->setResponse(new JsonResponse(null204));
  72.     }
  73. }