src/EventSubscriber/OrderSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Orders;
  5. use App\Entity\Rdv;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. final class OrderSubscriber implements EventSubscriberInterface
  12. {
  13.     private $em;
  14.     public function __construct(EntityManagerInterface $em)
  15.     {
  16.         $this->em $em;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             KernelEvents::VIEW => [[
  22.                 'invalidateCart'EventPriorities::POST_WRITE,
  23.             ],[
  24.                 'setCode'EventPriorities::PRE_WRITE,
  25.             ]]
  26.         ];
  27.     }
  28.     public function invalidateCart(ViewEvent $event):void
  29.     {
  30.         $order $event->getControllerResult();
  31.         $method $event->getRequest()->getMethod();
  32.         if (!$order instanceof Orders || Request::METHOD_POST !== $method) {
  33.             return;
  34.         }
  35.         $order->getPanier()->setState(true);
  36.         $this->em->flush();
  37.     }
  38.     public function setCode(ViewEvent $event):void
  39.     {
  40.         $order $event->getControllerResult();
  41.         $method $event->getRequest()->getMethod();
  42.         if (!$order instanceof Orders || Request::METHOD_POST !== $method) {
  43.             return;
  44.         }
  45.         $code time().''.mt_rand();
  46.         $order->setCode($code);
  47.         $order->setTransactionId($code);
  48.         $this->em->flush();
  49.     }
  50. }