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

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($configuration, View::create($cart));
  36. }
  37. $form = $this->resourceFormFactory->create($configuration, $cart);
  38. return $this->render(
  39. $configuration->getTemplate('summary.html'),
  40. [
  41. 'cart' => $cart,
  42. 'form' => $form->createView(),
  43. ],
  44. );
  45. }
  46. public function widgetAction(Request $request): Response
  47. {
  48. $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
  49. $cart = $this->getCurrentCart();
  50. if (!$configuration->isHtmlRequest()) {
  51. return $this->viewHandler->handle($configuration, View::create($cart));
  52. }
  53. return $this->render(
  54. $configuration->getTemplate('summary.html'),
  55. [
  56. 'cart' => $cart,
  57. ],
  58. );
  59. }
  60. public function saveAction(Request $request): Response
  61. {
  62. $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
  63. $this->isGrantedOr403($configuration, ResourceActions::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)->isSubmitted() && $form->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(new GenericEvent($resource), SyliusCartEvents::CART_CHANGE);
  81. $this->manager->flush();
  82. if (!$configuration->isHtmlRequest()) {
  83. return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
  84. }
  85. $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource);
  86. return $this->redirectHandler->redirectToResource($configuration, $resource);
  87. }
  88. if (!$configuration->isHtmlRequest()) {
  89. return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
  90. }
  91. return $this->render(
  92. $configuration->getTemplate(ResourceActions::UPDATE . '.html'),
  93. [
  94. 'configuration' => $configuration,
  95. $this->metadata->getName() => $resource,
  96. 'form' => $form->createView(),
  97. 'cart' => $resource,
  98. ],
  99. );
  100. }
  101. /**
  102. * @psalm-suppress DeprecatedMethod
  103. */
  104. public function clearAction(Request $request): Response
  105. {
  106. $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
  107. $this->isGrantedOr403($configuration, ResourceActions::DELETE);
  108. $resource = $this->getCurrentCart();
  109. if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid((string) $resource->getId(), $this->getParameterFromRequest($request, '_csrf_token'))) {
  110. throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
  111. }
  112. $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource);
  113. if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  114. throw new HttpException($event->getErrorCode(), $event->getMessage());
  115. }
  116. if ($event->isStopped()) {
  117. $this->flashHelper->addFlashFromEvent($configuration, $event);
  118. return $this->redirectHandler->redirectToIndex($configuration, $resource);
  119. }
  120. $this->repository->remove($resource);
  121. $this->eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource);
  122. if (!$configuration->isHtmlRequest()) {
  123. return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
  124. }
  125. $this->flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource);
  126. return $this->redirectHandler->redirectToIndex($configuration, $resource);
  127. }
  128. protected function redirectToCartSummary(RequestConfiguration $configuration): Response
  129. {
  130. if (null === $configuration->getParameters()->get('redirect')) {
  131. return $this->redirectHandler->redirectToRoute($configuration, $this->getCartSummaryRoute());
  132. }
  133. return $this->redirectHandler->redirectToRoute($configuration, $configuration->getParameters()->get('redirect'));
  134. }
  135. protected function getCartSummaryRoute(): string
  136. {
  137. return 'sylius_cart_summary';
  138. }
  139. protected function getCurrentCart(): OrderInterface
  140. {
  141. return $this->getContext()->getCart();
  142. }
  143. protected function getContext(): CartContextInterface
  144. {
  145. return $this->get('sylius.context.cart');
  146. }
  147. protected function getOrderRepository(): OrderRepositoryInterface
  148. {
  149. return $this->get('sylius.repository.order');
  150. }
  151. protected function getEventDispatcher(): EventDispatcherInterface
  152. {
  153. return $this->container->get('event_dispatcher');
  154. }
  155. /**
  156. * @return mixed
  157. *
  158. * @deprecated This function will be removed in Sylius 2.0, since Symfony 5.4, use explicit input sources instead
  159. * based on Symfony\Component\HttpFoundation\Request::get
  160. */
  161. private function getParameterFromRequest(Request $request, string $key)
  162. {
  163. if ($request !== $result = $request->attributes->get($key, $request)) {
  164. return $result;
  165. }
  166. if ($request->query->has($key)) {
  167. return $request->query->all()[$key];
  168. }
  169. if ($request->request->has($key)) {
  170. return $request->request->all()[$key];
  171. }
  172. return null;
  173. }
  174. }