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

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\Dto\AddProductBundleToCartDto;
  10. use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface;
  11. use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
  12. use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleToCartCommandFactoryInterface;
  13. use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleToCartDtoFactoryInterface;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use FOS\RestBundle\View\View;
  16. use Sylius\Bundle\OrderBundle\Controller\OrderItemController as BaseOrderItemController;
  17. use Sylius\Bundle\ResourceBundle\Controller;
  18. use Sylius\Component\Core\Repository\OrderRepositoryInterface;
  19. use Sylius\Component\Order\CartActions;
  20. use Sylius\Component\Resource\Factory\FactoryInterface;
  21. use Sylius\Component\Resource\Metadata\MetadataInterface;
  22. use Sylius\Component\Resource\Repository\RepositoryInterface;
  23. use Symfony\Component\Form\FormInterface;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\HttpKernel\Exception\HttpException;
  27. use Symfony\Component\Messenger\MessageBusInterface;
  28. class OrderItemController extends BaseOrderItemController
  29. {
  30.     /** @var MessageBusInterface */
  31.     protected $messageBus;
  32.     /** @var OrderRepositoryInterface */
  33.     protected $orderRepository;
  34.     /** @var AddProductBundleToCartDtoFactoryInterface */
  35.     private $addProductBundleToCartDtoFactory;
  36.     /** @var AddProductBundleToCartCommandFactoryInterface */
  37.     private $addProductBundleToCartCommandFactory;
  38.     public function __construct(
  39.         MetadataInterface $metadata,
  40.         Controller\RequestConfigurationFactoryInterface $requestConfigurationFactory,
  41.         Controller\ViewHandlerInterface $viewHandler,
  42.         RepositoryInterface $repository,
  43.         FactoryInterface $factory,
  44.         Controller\NewResourceFactoryInterface $newResourceFactory,
  45.         EntityManagerInterface $manager,
  46.         Controller\SingleResourceProviderInterface $singleResourceProvider,
  47.         Controller\ResourcesCollectionProviderInterface $resourcesFinder,
  48.         Controller\ResourceFormFactoryInterface $resourceFormFactory,
  49.         Controller\RedirectHandlerInterface $redirectHandler,
  50.         Controller\FlashHelperInterface $flashHelper,
  51.         Controller\AuthorizationCheckerInterface $authorizationChecker,
  52.         Controller\EventDispatcherInterface $eventDispatcher,
  53.         Controller\StateMachineInterface $stateMachine,
  54.         Controller\ResourceUpdateHandlerInterface $resourceUpdateHandler,
  55.         Controller\ResourceDeleteHandlerInterface $resourceDeleteHandler,
  56.         MessageBusInterface $messageBus,
  57.         OrderRepositoryInterface $orderRepository,
  58.         AddProductBundleToCartDtoFactoryInterface $addProductBundleToCartDtoFactory,
  59.         AddProductBundleToCartCommandFactoryInterface $addProductBundleToCartCommandFactory
  60.     ) {
  61.         parent::__construct(
  62.             $metadata,
  63.             $requestConfigurationFactory,
  64.             $viewHandler,
  65.             $repository,
  66.             $factory,
  67.             $newResourceFactory,
  68.             $manager,
  69.             $singleResourceProvider,
  70.             $resourcesFinder,
  71.             $resourceFormFactory,
  72.             $redirectHandler,
  73.             $flashHelper,
  74.             $authorizationChecker,
  75.             $eventDispatcher,
  76.             $stateMachine,
  77.             $resourceUpdateHandler,
  78.             $resourceDeleteHandler
  79.         );
  80.         $this->messageBus $messageBus;
  81.         $this->orderRepository $orderRepository;
  82.         $this->addProductBundleToCartDtoFactory $addProductBundleToCartDtoFactory;
  83.         $this->addProductBundleToCartCommandFactory $addProductBundleToCartCommandFactory;
  84.     }
  85.     public function addProductBundleAction(Request $request): ?Response
  86.     {
  87.         $cart $this->getCurrentCart();
  88.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  89.         $this->isGrantedOr403($configurationCartActions::ADD);
  90.         /** @var OrderItemInterface $orderItem */
  91.         $orderItem $this->newResourceFactory->create($configuration$this->factory);
  92.         $this->getQuantityModifier()->modify($orderItem1);
  93.         /** @var ProductInterface $product */
  94.         $product $orderItem->getProduct();
  95.         assert(null !== $configuration->getFormType());
  96.         $addProductBundleToCartDto $this->addProductBundleToCartDtoFactory->createNew($cart$orderItem$product);
  97.         $form $this->getFormFactory()->create(
  98.             $configuration->getFormType(),
  99.             $addProductBundleToCartDto,
  100.             $configuration->getFormOptions()
  101.         );
  102.         if ($request->isMethod(Request::METHOD_POST) && $form->handleRequest($request)->isValid()) {
  103.             return $this->handleForm($form$configuration$orderItem$request);
  104.         }
  105.         if (!$configuration->isHtmlRequest()) {
  106.             return $this->handleBadAjaxRequestView($configuration$form);
  107.         }
  108.         return $this->render(
  109.             $configuration->getTemplate(CartActions::ADD '.html'),
  110.             [
  111.                 'configuration' => $configuration,
  112.                 $this->metadata->getName() => $orderItem,
  113.                 'form' => $form->createView(),
  114.             ]
  115.         );
  116.     }
  117.     private function handleForm(
  118.         FormInterface $form,
  119.         Controller\RequestConfiguration $configuration,
  120.         OrderItemInterface $orderItem,
  121.         Request $request
  122.     ): ?Response {
  123.         /** @var AddProductBundleToCartDto $addProductBundleToCartDto */
  124.         $addProductBundleToCartDto $form->getData();
  125.         $errors $this->getCartItemErrors($addProductBundleToCartDto->getCartItem());
  126.         if (count($errors)) {
  127.             $form $this->getAddToCartFormWithErrors($errors$form);
  128.             return $this->handleBadAjaxRequestView($configuration$form);
  129.         }
  130.         $event $this->eventDispatcher->dispatchPreEvent(CartActions::ADD$configuration$orderItem);
  131.         if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  132.             throw new HttpException($event->getErrorCode(), $event->getMessage());
  133.         }
  134.         if ($event->isStopped()) {
  135.             $this->flashHelper->addFlashFromEvent($configuration$event);
  136.             return $this->redirectHandler->redirectToIndex($configuration$orderItem);
  137.         }
  138.         $cart $addProductBundleToCartDto->getCart();
  139.         if (null === $cart->getId()) {
  140.             $this->orderRepository->add($cart);
  141.         }
  142.         $addProductBundleToCartCommand $this->addProductBundleToCartCommandFactory->createFromDto($addProductBundleToCartDto);
  143.         $this->messageBus->dispatch($addProductBundleToCartCommand);
  144.         $resourceControllerEvent $this->eventDispatcher->dispatchPostEvent(CartActions::ADD$configuration$orderItem);
  145.         if ($resourceControllerEvent->hasResponse()) {
  146.             return $resourceControllerEvent->getResponse();
  147.         }
  148.         $this->flashHelper->addSuccessFlash($configurationCartActions::ADD$orderItem);
  149.         if ($request->isXmlHttpRequest()) {
  150.             assert(null !== $this->viewHandler);
  151.             $response $this->viewHandler->handle($configurationView::create([], Response::HTTP_CREATED));
  152.         } else {
  153.             $response $this->redirectHandler->redirectToResource($configuration$orderItem);
  154.         }
  155.         return $response;
  156.     }
  157. }