<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\EventSubscriber;
use App\Entity\Order\Order;
use App\Entity\Payment\Payment;
use App\Processor\OrderBackProcessor;
use Doctrine\ORM\EntityManagerInterface;
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderPaymentTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Storage\CartStorageInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Sylius\Component\Order\OrderTransitions;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
final class OrderBackFromMethodPaymentSubscriber implements EventSubscriberInterface
{
private CartContextInterface $cartContext;
private RequestMatcherInterface $requestMatcher;
private OrderRepositoryInterface $orderRepository;
private RouterInterface $router;
private TokenStorageInterface $tokenStorage;
private OrderBackProcessor $orderBackProcessor;
public function __construct(CartContextInterface $cartContext, RequestMatcherInterface $requestMatcher, OrderRepositoryInterface $orderRepository, RouterInterface $router,TokenStorageInterface $tokenStorage, OrderBackProcessor $orderBackProcessor)
{
$this->cartContext = $cartContext;
$this->requestMatcher = $requestMatcher;
$this->orderRepository = $orderRepository;
$this->router = $router;
$this->tokenStorage = $tokenStorage;
$this->orderBackProcessor = $orderBackProcessor;
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$request = $event->getRequest();
if (!$this->requestMatcher->matches($request)) {
return;
}
/** @var OrderInterface $order */
$order = $this->cartContext->getCart();
$orderId= $request->getSession()->get('sylius_order_id', null);
if ($order->isEmpty() || ($orderId && $order->getId() !== $orderId)) {
if ($orderId) {
/** @var Order $orderWaitingPayment */
$orderWaitingPayment = $this->orderRepository->find($orderId);
if ($orderWaitingPayment) {
$date = new \DateTime($orderWaitingPayment->getUpdatedAt()->format('Y-m-d H:i:s'));
$date->modify('+1 day');
if ($this->tokenStorage->getToken()->getUser() !== "anon." || $date > new \DateTime('now')) {
if ($orderWaitingPayment->getCheckoutState() === OrderCheckoutStates::STATE_COMPLETED && $orderWaitingPayment->getPaymentState() === OrderPaymentStates::STATE_AWAITING_PAYMENT) {
$this->orderBackProcessor->process($orderWaitingPayment, $order);
$request->getSession()->remove('sylius_order_id');
$event->setResponse(new RedirectResponse($this->router->generate('sylius_shop_cart_summary')));
}
}
}
}
else {
$request->getSession()->set('sylius_order_id', $order->getId());
}
}
else {
$request->getSession()->set('sylius_order_id', $order->getId());
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}