<?php
declare(strict_types=1);
namespace App\Controller;
// use Sylius\Component\Order\Model\OrderItem;
use App\Entity\Order\OrderItem;
use App\Entity\Product\Product;
use FOS\RestBundle\View\View;
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
use Sylius\Bundle\OrderBundle\Controller\OrderItemController;
use Sylius\Component\Core\Model\ProductVariant;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Order\CartActions;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Contracts\Translation\TranslatorInterface;
use function count;
use function gettype;
class CustomOrderItemController extends OrderItemController
{
public function addAction(Request $request): Response
{
$cart = $this->getCurrentCart();
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, CartActions::ADD);
/** @var OrderItem $orderItem */
$orderItem = $this->newResourceFactory->create($configuration, $this->factory);
/** @var OrderItemQuantityModifierInterface $orderItemQuantityModifierInterface */
$orderItemQuantityModifierInterface = $this->getQuantityModifier();
$orderItemQuantityModifierInterface->modify($orderItem, 1);
$formType = $configuration->getFormType();
if ($formType === null) {
$jsonResponse['message'] = 'cannot get formType';
return new JsonResponse($jsonResponse);
}
$form = $this->getFormFactory()->create(
$formType,
$this->createAddToCartCommand($cart, $orderItem),
$configuration->getFormOptions(),
);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
/** @var AddToCartCommandInterface $addToCartCommand */
$addToCartCommand = $form->getData();
$errors = $this->getCartItemErrors($addToCartCommand->getCartItem());
if (0 < count($errors)) {
$form = $this->getAddToCartFormWithErrors($errors, $form);
return $this->handleBadAjaxRequestView($configuration, $form);
}
$event = $this->eventDispatcher->dispatchPreEvent(CartActions::ADD, $configuration, $orderItem);
if ($event->isStopped() && ! $configuration->isHtmlRequest()) {
throw new HttpException($event->getErrorCode(), $event->getMessage());
}
if ($event->isStopped()) {
$this->flashHelper->addFlashFromEvent($configuration, $event);
return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
}
$itemsInCart = $cart->getItems();
$allreadyInCart = false;
/** @var OrderItem $cartOrderItem */
$cartOrderItem = $addToCartCommand->getCartItem();
if ($cartOrderItem !== null) {
/** @var ProductVariant $productVarriant */
$productVarriant = $cartOrderItem->getVariant();
/** @var Product $product */
$product = $cartOrderItem->getProduct();
$toAdd = $product->getId();
//vérification s'il s'agit d'un pack ou histoire
// $type = $cartOrderItem->getProduct();
$catalogue = false;
/** @var TaxonInterface $taxon */
foreach ($product->getTaxons() as $taxon) {
if ($taxon->getCode() === 'storyteller') {
continue;
}
$catalogue = true;
}
if ($catalogue === true) {
/** @var OrderItem $item */
foreach ($itemsInCart as $item) {
if ($item->getProduct() !== null) {
if ($item->getProduct()->getId() !== $toAdd) {
continue;
}
}
$allreadyInCart = true;
}
}
}
if ($allreadyInCart === false) {
$this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
$cartManager = $this->getCartManager();
$cartManager->persist($cart);
$cartManager->flush();
$resourceControllerEvent = $this->eventDispatcher->dispatchPostEvent(CartActions::ADD, $configuration, $orderItem);
if ($resourceControllerEvent->hasResponse()) {
$jsonResponse['message'] = 'Error';
return $resourceControllerEvent->getResponse() ?? new JsonResponse($jsonResponse);
}
$this->flashHelper->addSuccessFlash($configuration, CartActions::ADD, $orderItem);
} else {
$this->addFlash('error', $this->getTranslator()->trans('sylius.pages.content.item_already_in_cart'));
}
if ($request->isXmlHttpRequest()) {
if ($this->viewHandler !== null) {
return $this->viewHandler->handle($configuration, View::create([], Response::HTTP_CREATED));
}
}
return $this->redirectHandler->redirectToResource($configuration, $orderItem);
}
if (! $configuration->isHtmlRequest()) {
return $this->handleBadAjaxRequestView($configuration, $form);
}
if (gettype($configuration->getTemplate(CartActions::ADD . '.html')) === 'string') {
return $this->render(
$configuration->getTemplate(CartActions::ADD . '.html'),
[
'configuration' => $configuration,
$this->metadata->getName() => $orderItem,
'form' => $form->createView(),
],
);
}
$jsonResponse['message'] = 'Error';
return new JsonResponse($jsonResponse);
}
private function getTranslator(): TranslatorInterface
{
/** @var TranslatorInterface $translator */
$translator = $this->get('translator');
return $translator;
}
}