src/EventSubscriber/OrderBackFromMethodPaymentSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber;
  12. use App\Entity\Order\Order;
  13. use App\Entity\Payment\Payment;
  14. use App\Processor\OrderBackProcessor;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
  17. use Sylius\Component\Core\Model\OrderInterface;
  18. use Sylius\Component\Core\OrderCheckoutStates;
  19. use Sylius\Component\Core\OrderCheckoutTransitions;
  20. use Sylius\Component\Core\OrderPaymentStates;
  21. use Sylius\Component\Core\OrderPaymentTransitions;
  22. use Sylius\Component\Core\Repository\OrderRepositoryInterface;
  23. use Sylius\Component\Core\Storage\CartStorageInterface;
  24. use Sylius\Component\Order\Context\CartContextInterface;
  25. use Sylius\Component\Order\OrderTransitions;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use Symfony\Component\HttpFoundation\RedirectResponse;
  28. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  29. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  30. use Symfony\Component\HttpKernel\Event\RequestEvent;
  31. use Symfony\Component\HttpKernel\KernelEvents;
  32. use Symfony\Component\Routing\RouterInterface;
  33. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  34. final class OrderBackFromMethodPaymentSubscriber implements EventSubscriberInterface
  35. {
  36.     private CartContextInterface $cartContext;
  37.     private RequestMatcherInterface $requestMatcher;
  38.     private OrderRepositoryInterface $orderRepository;
  39.     private RouterInterface $router;
  40.     private TokenStorageInterface $tokenStorage;
  41.     private OrderBackProcessor $orderBackProcessor;
  42.     public function __construct(CartContextInterface $cartContextRequestMatcherInterface $requestMatcherOrderRepositoryInterface $orderRepositoryRouterInterface $router,TokenStorageInterface $tokenStorageOrderBackProcessor $orderBackProcessor)
  43.     {
  44.         $this->cartContext $cartContext;
  45.         $this->requestMatcher $requestMatcher;
  46.         $this->orderRepository $orderRepository;
  47.         $this->router $router;
  48.         $this->tokenStorage $tokenStorage;
  49.         $this->orderBackProcessor $orderBackProcessor;
  50.     }
  51.     public function onKernelRequest(RequestEvent $event): void
  52.     {
  53.         if (!$event->isMainRequest()) {
  54.             return;
  55.         }
  56.         $request $event->getRequest();
  57.         if (!$this->requestMatcher->matches($request)) {
  58.             return;
  59.         }
  60.         /** @var OrderInterface $order */
  61.         $order $this->cartContext->getCart();
  62.         $orderId$request->getSession()->get('sylius_order_id'null);
  63.         if ($order->isEmpty() || ($orderId && $order->getId() !== $orderId))  {
  64.             if ($orderId) {
  65.                 /** @var Order $orderWaitingPayment */
  66.                 $orderWaitingPayment $this->orderRepository->find($orderId);
  67.                 if ($orderWaitingPayment) {
  68.                     $date = new \DateTime($orderWaitingPayment->getUpdatedAt()->format('Y-m-d H:i:s'));
  69.                     $date->modify('+1 day');
  70.                     if ($this->tokenStorage->getToken()->getUser() !== "anon." || $date > new \DateTime('now')) {
  71.                         if ($orderWaitingPayment->getCheckoutState() === OrderCheckoutStates::STATE_COMPLETED &&  $orderWaitingPayment->getPaymentState() === OrderPaymentStates::STATE_AWAITING_PAYMENT) {
  72.                             $this->orderBackProcessor->process($orderWaitingPayment$order);
  73.                             $request->getSession()->remove('sylius_order_id');
  74.                             $event->setResponse(new RedirectResponse($this->router->generate('sylius_shop_cart_summary')));
  75.                         }
  76.                     }
  77.                 }
  78.             }
  79.             else {
  80.                 $request->getSession()->set('sylius_order_id'$order->getId());
  81.             }
  82.         }
  83.         else {
  84.             $request->getSession()->set('sylius_order_id'$order->getId());
  85.         }
  86.     }
  87.     public static function getSubscribedEvents(): array
  88.     {
  89.         return [
  90.             KernelEvents::REQUEST => 'onKernelRequest',
  91.         ];
  92.     }
  93. }