src/EventSubscriber/CheckAwaitingOrderSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Entity\Order\Order;
  5. use App\Processor\OrderBackProcessor;
  6. use App\Repository\Order\OrderRepository;
  7. use Sylius\Bundle\AdminBundle\SectionResolver\AdminSection;
  8. use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
  9. use Sylius\Component\Channel\Context\ChannelContextInterface;
  10. use Sylius\Component\Customer\Context\CustomerContextInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. final class CheckAwaitingOrderSubscriber implements EventSubscriberInterface
  15. {
  16.     public function __construct(
  17.         private CustomerContextInterface $customerContext,
  18.         private OrderRepository $orderRepository,
  19.         private ChannelContextInterface $channelContext,
  20.         private OrderBackProcessor $orderBackProcessor,
  21.         private SectionProviderInterface $sectionProvider
  22.     ) {
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             KernelEvents::REQUEST => [['onKernelRequest'1]],
  28.         ];
  29.     }
  30.     public function onKernelRequest(RequestEvent $event): void
  31.     {
  32.         if (!$event->isMainRequest()) {
  33.             return;
  34.         }
  35.         if ($this->sectionProvider->getSection() instanceof AdminSection) {
  36.             return;
  37.         }
  38.         $url $event->getRequest()->getRequestUri();
  39.         if (str_starts_with($url'/order') ||
  40.             str_starts_with($url'/_wdt/') ||
  41.             str_starts_with($url'/payment/') ||
  42.             str_contains($url'/checkout/select-payment') ||
  43.             str_starts_with($url'/fr_FR/payment/') ||
  44.             str_starts_with($url'/pay-with-paypal/') ||
  45.             str_starts_with($url'/fr_FR/pay-with-paypal/') ||
  46.             str_starts_with($url'/create-pay-pal-order/') ||
  47.             str_starts_with($url'/fr_FR/create-pay-pal-order/') ||
  48.             str_starts_with($url'/complete-pay-pal-order/') ||
  49.             str_starts_with($url'/fr_FR/complete-pay-pal-order/') ||
  50.             str_starts_with($url'/paypal-webhook/api/') ||
  51.             str_starts_with($url'/fr_FR/paypal-webhook/') ||
  52.             str_starts_with($url'/fr_FR/order/') ||
  53.             str_starts_with($url'/pay/') ||
  54.             str_starts_with($url'/fr_FR/pay')) {
  55.             return;
  56.         }
  57.         $customer $this->customerContext->getCustomer();
  58.         if (null === $customer) {
  59.             return;
  60.         }
  61.         $awaitingOrders $this->orderRepository->findAllAwaitingOrderByChannelAndCustomer($this->channelContext->getChannel(), $customer);
  62.         $paymentMethods = ['Paypal''cb''system_pay'];
  63.         /** @var Order $awaitingOrder */
  64.         foreach ($awaitingOrders as $awaitingOrder) {
  65.             // vĂ©rifier que c'est une commande cb ou paypal
  66.             if (in_array($awaitingOrder->getLastPayment()->getMethod()->getCode(), $paymentMethods)) {
  67.                 $this->orderBackProcessor->process($awaitingOrder);
  68.             }
  69.         }
  70.     }
  71. }