<?php
declare(strict_types=1);
namespace BitBag\OpenMarketplace\Component\Core\Common\Controller\Resource;
use BitBag\OpenMarketplace\Component\Order\Entity\OrderInterface;
use BitBag\OpenMarketplace\Component\Product\Entity\ProductInterface;
use BitBag\OpenMarketplace\Component\SellerPlan\Checker\SellerPlanCartCheckerInterface;
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
use Sylius\Bundle\OrderBundle\Controller\OrderItemController as BaseOrderItemController;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\CartActions;
use Sylius\Component\Product\Model\ProductVariantInterface;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Translation\DataCollectorTranslator;
final class OrderItemController extends BaseOrderItemController
{
public function addAction(Request $request): Response
{
/** @var OrderInterface $cart */
$cart = $this->getCurrentCart();
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, CartActions::ADD);
/** @var OrderItemInterface $orderItem */
$orderItem = $this->newResourceFactory->create($configuration, $this->factory);
$this->getQuantityModifier()->modify($orderItem, 1);
/** @var string $formType */
$formType = $configuration->getFormType();
$form = $this->getFormFactory()->create(
$formType,
$this->createAddToCartCommand($cart, $orderItem),
$configuration->getFormOptions()
);
$form->handleRequest($request);
/** @var DataCollectorTranslator $translator */
$translator = $this->get('translator');
/** @var ProductInterface $product */
$product = $orderItem->getProduct();
if (
$this->isBasePlan($product)
&& $this->isAlreadyBasePlanInTheCart($request, $cart)
) {
/** @var Session $session */
$session = $request->getSession();
$session->getFlashBag()->add(
'error',
$translator->trans('app.checkout.already_base_plan_in_the_cart')
);
return $this->redirectToRoute('open_marketplace_shop_seller_plan_index');
}
/** @var SubmitButton $addToWishlist */
$addToWishlist = $form->get('addToWishlist');
if ($addToWishlist->isClicked()) {
/** @var AddToCartCommandInterface $addToCartCommand */
$addToCartCommand = $form->getData();
/** @var OrderItemInterface $item */
$item = $addToCartCommand->getCartItem();
/** @var ProductVariantInterface|null $variant */
$variant = $item->getVariant();
/** @var WishlistInterface|null $wishlist */
$wishlist = $form->get('wishlists')->getData();
if (null === $variant) {
throw new NotFoundHttpException('Could not find variant');
}
if (null === $wishlist) {
/** @var Session $session */
$session = $request->getSession();
if (null !== $translator) {
$session->getFlashBag()->add('error', $translator->trans('bitbag_sylius_wishlist_plugin.ui.go_to_wishlist_failure'));
}
return new Response($this->generateUrl('sylius_shop_homepage'));
}
return new Response($this->generateUrl('bitbag_sylius_wishlist_plugin_shop_locale_wishlist_add_product_variant', [
'wishlistId' => $wishlist->getId(),
'variantId' => $variant->getId(),
]));
}
return parent::addAction($request);
}
private function isBasePlan(ProductInterface $product): bool
{
return null !== $product->getSellerPlan() && true === $product->getSellerPlan()->isBasePlan();
}
private function isAlreadyBasePlanInTheCart(
Request $request,
OrderInterface $cart
): bool {
/** @var SellerPlanCartCheckerInterface $sellerPlanCartChecker */
$sellerPlanCartChecker = $this->get(
'bitbag.open_marketplace.component.seller_plan.checker.seller_plan_cart_checker'
);
return $request->isMethod('POST')
&& $sellerPlanCartChecker->isAlreadyBasePlanInTheCart($cart);
}
}