src/Component/Core/Common/Controller/Resource/OrderItemController.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\Core\Common\Controller\Resource;
  4. use BitBag\OpenMarketplace\Component\Order\Entity\OrderInterface;
  5. use BitBag\OpenMarketplace\Component\Product\Entity\ProductInterface;
  6. use BitBag\OpenMarketplace\Component\SellerPlan\Checker\SellerPlanCartCheckerInterface;
  7. use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
  8. use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
  9. use Sylius\Bundle\OrderBundle\Controller\OrderItemController as BaseOrderItemController;
  10. use Sylius\Component\Core\Model\OrderItemInterface;
  11. use Sylius\Component\Order\CartActions;
  12. use Sylius\Component\Product\Model\ProductVariantInterface;
  13. use Symfony\Component\Form\SubmitButton;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpFoundation\Session\Session;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. use Symfony\Component\Translation\DataCollectorTranslator;
  19. final class OrderItemController extends BaseOrderItemController
  20. {
  21.     public function addAction(Request $request): Response
  22.     {
  23.         /** @var OrderInterface $cart */
  24.         $cart $this->getCurrentCart();
  25.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  26.         $this->isGrantedOr403($configurationCartActions::ADD);
  27.         /** @var OrderItemInterface $orderItem */
  28.         $orderItem $this->newResourceFactory->create($configuration$this->factory);
  29.         $this->getQuantityModifier()->modify($orderItem1);
  30.         /** @var string $formType */
  31.         $formType $configuration->getFormType();
  32.         $form $this->getFormFactory()->create(
  33.             $formType,
  34.             $this->createAddToCartCommand($cart$orderItem),
  35.             $configuration->getFormOptions()
  36.         );
  37.         $form->handleRequest($request);
  38.         /** @var DataCollectorTranslator $translator */
  39.         $translator $this->get('translator');
  40.         /** @var ProductInterface $product */
  41.         $product $orderItem->getProduct();
  42.         if (
  43.             $this->isProductSellerPlan($product)
  44.             && $this->isCartValid($request$cart)
  45.         ) {
  46.             /** @var Session $session */
  47.             $session $request->getSession();
  48.             $session->getFlashBag()->add(
  49.                 'error',
  50.                 $translator->trans('app.checkout.already_seller_plan_in_the_cart')
  51.             );
  52.             return $this->redirectToRoute('open_marketplace_shop_seller_plan_index');
  53.         }
  54.         /** @var SubmitButton $addToWishlist */
  55.         $addToWishlist $form->get('addToWishlist');
  56.         if ($addToWishlist->isClicked()) {
  57.             /** @var AddToCartCommandInterface $addToCartCommand */
  58.             $addToCartCommand $form->getData();
  59.             /** @var OrderItemInterface $item */
  60.             $item $addToCartCommand->getCartItem();
  61.             /** @var ProductVariantInterface|null $variant */
  62.             $variant $item->getVariant();
  63.             /** @var WishlistInterface|null $wishlist */
  64.             $wishlist $form->get('wishlists')->getData();
  65.             if (null === $variant) {
  66.                 throw new NotFoundHttpException('Could not find variant');
  67.             }
  68.             if (null === $wishlist) {
  69.                 /** @var Session $session */
  70.                 $session $request->getSession();
  71.                 if (null !== $translator) {
  72.                     $session->getFlashBag()->add('error'$translator->trans('bitbag_sylius_wishlist_plugin.ui.go_to_wishlist_failure'));
  73.                 }
  74.                 return new Response($this->generateUrl('sylius_shop_homepage'));
  75.             }
  76.             return new Response($this->generateUrl('bitbag_sylius_wishlist_plugin_shop_locale_wishlist_add_product_variant', [
  77.                 'wishlistId' => $wishlist->getId(),
  78.                 'variantId' => $variant->getId(),
  79.             ]));
  80.         }
  81.         return parent::addAction($request);
  82.     }
  83.     private function isProductSellerPlan(ProductInterface $product): bool
  84.     {
  85.         return null !== $product->getSellerPlan();
  86.     }
  87.     private function isCartValid(
  88.         Request $request,
  89.         OrderInterface $cart
  90.     ): bool {
  91.         /** @var SellerPlanCartCheckerInterface $sellerPlanCartChecker */
  92.         $sellerPlanCartChecker $this->get(
  93.             'bitbag.open_marketplace.component.seller_plan.checker.seller_plan_cart_checker'
  94.         );
  95.         return $request->isMethod('POST')
  96.             && $sellerPlanCartChecker->isAlreadySellerPlanInTheCart($cart);
  97.     }
  98. }