<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use App\Entity\Order\Order;
use App\Processor\OrderBackProcessor;
use App\Repository\Order\OrderRepository;
use Sylius\Bundle\AdminBundle\SectionResolver\AdminSection;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Customer\Context\CustomerContextInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final class CheckAwaitingOrderSubscriber implements EventSubscriberInterface
{
public function __construct(
private CustomerContextInterface $customerContext,
private OrderRepository $orderRepository,
private ChannelContextInterface $channelContext,
private OrderBackProcessor $orderBackProcessor,
private SectionProviderInterface $sectionProvider
) {
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [['onKernelRequest', 1]],
];
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
if ($this->sectionProvider->getSection() instanceof AdminSection) {
return;
}
$url = $event->getRequest()->getRequestUri();
if (str_starts_with($url, '/order') ||
str_starts_with($url, '/_wdt/') ||
str_starts_with($url, '/payment/') ||
str_contains($url, '/checkout/select-payment') ||
str_starts_with($url, '/fr_FR/payment/') ||
str_starts_with($url, '/pay-with-paypal/') ||
str_starts_with($url, '/fr_FR/pay-with-paypal/') ||
str_starts_with($url, '/create-pay-pal-order/') ||
str_starts_with($url, '/fr_FR/create-pay-pal-order/') ||
str_starts_with($url, '/complete-pay-pal-order/') ||
str_starts_with($url, '/fr_FR/complete-pay-pal-order/') ||
str_starts_with($url, '/paypal-webhook/api/') ||
str_starts_with($url, '/fr_FR/paypal-webhook/') ||
str_starts_with($url, '/fr_FR/order/') ||
str_starts_with($url, '/pay/') ||
str_starts_with($url, '/fr_FR/pay')) {
return;
}
$customer = $this->customerContext->getCustomer();
if (null === $customer) {
return;
}
$awaitingOrders = $this->orderRepository->findAllAwaitingOrderByChannelAndCustomer($this->channelContext->getChannel(), $customer);
$paymentMethods = ['Paypal', 'cb', 'system_pay'];
/** @var Order $awaitingOrder */
foreach ($awaitingOrders as $awaitingOrder) {
// vérifier que c'est une commande cb ou paypal
if (in_array($awaitingOrder->getLastPayment()->getMethod()->getCode(), $paymentMethods)) {
$this->orderBackProcessor->process($awaitingOrder);
}
}
}
}