vendor/sylius/sylius/src/Sylius/Bundle/OrderBundle/Controller/OrderController.php line 58

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\OrderBundle\Controller;
  12. use FOS\RestBundle\View\View;
  13. use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
  14. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  15. use Sylius\Component\Order\Context\CartContextInterface;
  16. use Sylius\Component\Order\Model\OrderInterface;
  17. use Sylius\Component\Order\Repository\OrderRepositoryInterface;
  18. use Sylius\Component\Order\SyliusCartEvents;
  19. use Sylius\Component\Resource\ResourceActions;
  20. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  21. use Symfony\Component\EventDispatcher\GenericEvent;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpKernel\Exception\HttpException;
  25. class OrderController extends ResourceController
  26. {
  27.     public function summaryAction(Request $request): Response
  28.     {
  29.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  30.         $cart $this->getCurrentCart();
  31.         if (null !== $cart->getId()) {
  32.             $cart $this->getOrderRepository()->findCartById($cart->getId());
  33.         }
  34.         if (!$configuration->isHtmlRequest()) {
  35.             return $this->viewHandler->handle($configurationView::create($cart));
  36.         }
  37.         $form $this->resourceFormFactory->create($configuration$cart);
  38.         $view View::create()
  39.             ->setTemplate($configuration->getTemplate('summary.html'))
  40.             ->setData([
  41.                 'cart' => $cart,
  42.                 'form' => $form->createView(),
  43.             ])
  44.         ;
  45.         return $this->viewHandler->handle($configuration$view);
  46.     }
  47.     public function widgetAction(Request $request): Response
  48.     {
  49.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  50.         $cart $this->getCurrentCart();
  51.         if (!$configuration->isHtmlRequest()) {
  52.             return $this->viewHandler->handle($configurationView::create($cart));
  53.         }
  54.         $view View::create()
  55.             ->setTemplate($configuration->getTemplate('summary.html'))
  56.             ->setData(['cart' => $cart])
  57.         ;
  58.         return $this->viewHandler->handle($configuration$view);
  59.     }
  60.     public function saveAction(Request $request): Response
  61.     {
  62.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  63.         $this->isGrantedOr403($configurationResourceActions::UPDATE);
  64.         $resource $this->getCurrentCart();
  65.         $form $this->resourceFormFactory->create($configuration$resource);
  66.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true) && $form->handleRequest($request)->isValid()) {
  67.             $resource $form->getData();
  68.             $event $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE$configuration$resource);
  69.             if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  70.                 throw new HttpException($event->getErrorCode(), $event->getMessage());
  71.             }
  72.             if ($event->isStopped()) {
  73.                 $this->flashHelper->addFlashFromEvent($configuration$event);
  74.                 return $this->redirectHandler->redirectToResource($configuration$resource);
  75.             }
  76.             if ($configuration->hasStateMachine()) {
  77.                 $this->stateMachine->apply($configuration$resource);
  78.             }
  79.             $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE$configuration$resource);
  80.             $this->getEventDispatcher()->dispatch(SyliusCartEvents::CART_CHANGE, new GenericEvent($resource));
  81.             $this->manager->flush();
  82.             if (!$configuration->isHtmlRequest()) {
  83.                 return $this->viewHandler->handle($configurationView::create(nullResponse::HTTP_NO_CONTENT));
  84.             }
  85.             $this->flashHelper->addSuccessFlash($configurationResourceActions::UPDATE$resource);
  86.             return $this->redirectHandler->redirectToResource($configuration$resource);
  87.         }
  88.         if (!$configuration->isHtmlRequest()) {
  89.             return $this->viewHandler->handle($configurationView::create($formResponse::HTTP_BAD_REQUEST));
  90.         }
  91.         $view View::create()
  92.             ->setData([
  93.                 'configuration' => $configuration,
  94.                 $this->metadata->getName() => $resource,
  95.                 'form' => $form->createView(),
  96.                 'cart' => $resource,
  97.             ])
  98.             ->setTemplate($configuration->getTemplate(ResourceActions::UPDATE '.html'))
  99.         ;
  100.         return $this->viewHandler->handle($configuration$view);
  101.     }
  102.     public function clearAction(Request $request): Response
  103.     {
  104.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  105.         $this->isGrantedOr403($configurationResourceActions::DELETE);
  106.         $resource $this->getCurrentCart();
  107.         if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid((string) $resource->getId(), $request->get('_csrf_token'))) {
  108.             throw new HttpException(Response::HTTP_FORBIDDEN'Invalid csrf token.');
  109.         }
  110.         $event $this->eventDispatcher->dispatchPreEvent(ResourceActions::DELETE$configuration$resource);
  111.         if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  112.             throw new HttpException($event->getErrorCode(), $event->getMessage());
  113.         }
  114.         if ($event->isStopped()) {
  115.             $this->flashHelper->addFlashFromEvent($configuration$event);
  116.             return $this->redirectHandler->redirectToIndex($configuration$resource);
  117.         }
  118.         $this->repository->remove($resource);
  119.         $this->eventDispatcher->dispatchPostEvent(ResourceActions::DELETE$configuration$resource);
  120.         if (!$configuration->isHtmlRequest()) {
  121.             return $this->viewHandler->handle($configurationView::create(nullResponse::HTTP_NO_CONTENT));
  122.         }
  123.         $this->flashHelper->addSuccessFlash($configurationResourceActions::DELETE$resource);
  124.         return $this->redirectHandler->redirectToIndex($configuration$resource);
  125.     }
  126.     protected function redirectToCartSummary(RequestConfiguration $configuration): Response
  127.     {
  128.         if (null === $configuration->getParameters()->get('redirect')) {
  129.             return $this->redirectHandler->redirectToRoute($configuration$this->getCartSummaryRoute());
  130.         }
  131.         return $this->redirectHandler->redirectToRoute($configuration$configuration->getParameters()->get('redirect'));
  132.     }
  133.     protected function getCartSummaryRoute(): string
  134.     {
  135.         return 'sylius_cart_summary';
  136.     }
  137.     protected function getCurrentCart(): OrderInterface
  138.     {
  139.         return $this->getContext()->getCart();
  140.     }
  141.     protected function getContext(): CartContextInterface
  142.     {
  143.         return $this->get('sylius.context.cart');
  144.     }
  145.     protected function getOrderRepository(): OrderRepositoryInterface
  146.     {
  147.         return $this->get('sylius.repository.order');
  148.     }
  149.     protected function getEventDispatcher(): EventDispatcherInterface
  150.     {
  151.         return $this->container->get('event_dispatcher');
  152.     }
  153. }