vendor/bitbag/product-bundle-plugin/src/Controller/OrderItemController.php line 96

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file was created by developers working at BitBag
  4.  * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5.  * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\SyliusProductBundlePlugin\Controller;
  9. use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand;
  10. use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface;
  11. use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use FOS\RestBundle\View\View;
  14. use Sylius\Bundle\OrderBundle\Controller\OrderItemController as BaseOrderItemController;
  15. use Sylius\Bundle\ResourceBundle\Controller;
  16. use Sylius\Component\Order\CartActions;
  17. use Sylius\Component\Resource\Factory\FactoryInterface;
  18. use Sylius\Component\Resource\Metadata\MetadataInterface;
  19. use Sylius\Component\Resource\Repository\RepositoryInterface;
  20. use Symfony\Component\Form\FormInterface;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\Exception\HttpException;
  24. use Symfony\Component\Messenger\MessageBusInterface;
  25. class OrderItemController extends BaseOrderItemController
  26. {
  27.     /** @var MessageBusInterface */
  28.     protected $messageBus;
  29.     public function __construct(
  30.         MetadataInterface $metadata,
  31.         Controller\RequestConfigurationFactoryInterface $requestConfigurationFactory,
  32.         Controller\ViewHandlerInterface $viewHandler,
  33.         RepositoryInterface $repository,
  34.         FactoryInterface $factory,
  35.         Controller\NewResourceFactoryInterface $newResourceFactory,
  36.         EntityManagerInterface $manager,
  37.         Controller\SingleResourceProviderInterface $singleResourceProvider,
  38.         Controller\ResourcesCollectionProviderInterface $resourcesFinder,
  39.         Controller\ResourceFormFactoryInterface $resourceFormFactory,
  40.         Controller\RedirectHandlerInterface $redirectHandler,
  41.         Controller\FlashHelperInterface $flashHelper,
  42.         Controller\AuthorizationCheckerInterface $authorizationChecker,
  43.         Controller\EventDispatcherInterface $eventDispatcher,
  44.         Controller\StateMachineInterface $stateMachine,
  45.         Controller\ResourceUpdateHandlerInterface $resourceUpdateHandler,
  46.         Controller\ResourceDeleteHandlerInterface $resourceDeleteHandler,
  47.         MessageBusInterface $messageBus
  48.     ) {
  49.         parent::__construct(
  50.             $metadata,
  51.             $requestConfigurationFactory,
  52.             $viewHandler,
  53.             $repository,
  54.             $factory,
  55.             $newResourceFactory,
  56.             $manager,
  57.             $singleResourceProvider,
  58.             $resourcesFinder,
  59.             $resourceFormFactory,
  60.             $redirectHandler,
  61.             $flashHelper,
  62.             $authorizationChecker,
  63.             $eventDispatcher,
  64.             $stateMachine,
  65.             $resourceUpdateHandler,
  66.             $resourceDeleteHandler
  67.         );
  68.         $this->messageBus $messageBus;
  69.     }
  70.     public function addProductBundleAction(Request $request): ?Response
  71.     {
  72.         $cart $this->getCurrentCart();
  73.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  74.         $this->isGrantedOr403($configurationCartActions::ADD);
  75.         /** @var OrderItemInterface $orderItem */
  76.         $orderItem $this->newResourceFactory->create($configuration$this->factory);
  77.         $this->getQuantityModifier()->modify($orderItem1);
  78.         /** @var ProductInterface $product */
  79.         $product $orderItem->getProduct();
  80.         $form $this->getFormFactory()->create(
  81.             $configuration->getFormType(),
  82.             new AddProductBundleToCartCommand($cart$orderItem$product),
  83.             $configuration->getFormOptions()
  84.         );
  85.         if ($request->isMethod(Request::METHOD_POST) && $form->handleRequest($request)->isValid()) {
  86.             return $this->handleForm($form$configuration$orderItem$request);
  87.         }
  88.         if (!$configuration->isHtmlRequest()) {
  89.             return $this->handleBadAjaxRequestView($configuration$form);
  90.         }
  91.         return $this->render(
  92.             $configuration->getTemplate(CartActions::ADD '.html'), [
  93.                 'configuration' => $configuration,
  94.                 $this->metadata->getName() => $orderItem,
  95.                 'form' => $form->createView(),
  96.             ]
  97.         );
  98.     }
  99.     private function handleForm(
  100.         FormInterface $form,
  101.         Controller\RequestConfiguration $configuration,
  102.         OrderItemInterface $orderItem,
  103.         Request $request): ?Response
  104.     {
  105.         /** @var AddProductBundleToCartCommand $addProductBundleToCartCommand */
  106.         $addProductBundleToCartCommand $form->getData();
  107.         $errors $this->getCartItemErrors($addProductBundleToCartCommand->getCartItem());
  108.         if (count($errors)) {
  109.             $form $this->getAddToCartFormWithErrors($errors$form);
  110.             return $this->handleBadAjaxRequestView($configuration$form);
  111.         }
  112.         $event $this->eventDispatcher->dispatchPreEvent(CartActions::ADD$configuration$orderItem);
  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$orderItem);
  119.         }
  120.         $this->messageBus->dispatch($addProductBundleToCartCommand);
  121.         $resourceControllerEvent $this->eventDispatcher->dispatchPostEvent(CartActions::ADD$configuration$orderItem);
  122.         if ($resourceControllerEvent->hasResponse()) {
  123.             return $resourceControllerEvent->getResponse();
  124.         }
  125.         $this->flashHelper->addSuccessFlash($configurationCartActions::ADD$orderItem);
  126.         if ($request->isXmlHttpRequest()) {
  127.             $response $this->viewHandler->handle($configurationView::create([], Response::HTTP_CREATED));
  128.         } else {
  129.             $response $this->redirectHandler->redirectToResource($configuration$orderItem);
  130.         }
  131.         return $response;
  132.     }
  133. }