vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Checkout/CheckoutResolver.php line 52

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 Sylius\Bundle\CoreBundle\Checkout;
  12. use SM\Factory\FactoryInterface;
  13. use Sylius\Component\Core\Model\OrderInterface;
  14. use Sylius\Component\Order\Context\CartContextInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  19. use Symfony\Component\HttpKernel\Event\RequestEvent;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. final class CheckoutResolver implements EventSubscriberInterface
  22. {
  23.     /** @var CartContextInterface */
  24.     private $cartContext;
  25.     /** @var CheckoutStateUrlGeneratorInterface */
  26.     private $urlGenerator;
  27.     /** @var RequestMatcherInterface */
  28.     private $requestMatcher;
  29.     /** @var FactoryInterface */
  30.     private $stateMachineFactory;
  31.     public function __construct(
  32.         CartContextInterface $cartContext,
  33.         CheckoutStateUrlGeneratorInterface $urlGenerator,
  34.         RequestMatcherInterface $requestMatcher,
  35.         FactoryInterface $stateMachineFactory
  36.     ) {
  37.         $this->cartContext $cartContext;
  38.         $this->urlGenerator $urlGenerator;
  39.         $this->requestMatcher $requestMatcher;
  40.         $this->stateMachineFactory $stateMachineFactory;
  41.     }
  42.     public function onKernelRequest(RequestEvent $event): void
  43.     {
  44.         if (!$event->isMasterRequest()) {
  45.             return;
  46.         }
  47.         $request $event->getRequest();
  48.         if (!$this->requestMatcher->matches($request)) {
  49.             return;
  50.         }
  51.         /** @var OrderInterface $order */
  52.         $order $this->cartContext->getCart();
  53.         if ($order->isEmpty()) {
  54.             $event->setResponse(new RedirectResponse($this->urlGenerator->generateForCart()));
  55.         }
  56.         $stateMachine $this->stateMachineFactory->get($order$this->getRequestedGraph($request));
  57.         if ($stateMachine->can($this->getRequestedTransition($request))) {
  58.             return;
  59.         }
  60.         $event->setResponse(new RedirectResponse($this->urlGenerator->generateForOrderCheckoutState($order)));
  61.     }
  62.     public static function getSubscribedEvents(): array
  63.     {
  64.         return [
  65.             KernelEvents::REQUEST => 'onKernelRequest',
  66.         ];
  67.     }
  68.     private function getRequestedGraph(Request $request): string
  69.     {
  70.         return $request->attributes->get('_sylius')['state_machine']['graph'];
  71.     }
  72.     private function getRequestedTransition(Request $request): string
  73.     {
  74.         return $request->attributes->get('_sylius')['state_machine']['transition'];
  75.     }
  76. }