Sylius\Bundle\OrderBundle\Controller\OrderItemController::addAction PHP Method

addAction() public method

public addAction ( Request $request ) : Response
$request Symfony\Component\HttpFoundation\Request
return Symfony\Component\HttpFoundation\Response
    public function addAction(Request $request)
    {
        $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);
        $form = $this->getFormFactory()->create($configuration->getFormType(), $this->createAddToCartCommand($cart, $orderItem), $configuration->getFormOptions());
        if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
            /** @var AddToCartCommandInterface $addCartItemCommand */
            $addToCartCommand = $form->getData();
            $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);
            }
            $this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
            $cartManager = $this->getCartManager();
            $cartManager->persist($cart);
            $cartManager->flush();
            $this->eventDispatcher->dispatchPostEvent(CartActions::ADD, $configuration, $orderItem);
            $this->flashHelper->addSuccessFlash($configuration, CartActions::ADD, $orderItem);
            if ($request->isXmlHttpRequest()) {
                return $this->viewHandler->handle($configuration, View::create([], Response::HTTP_CREATED));
            }
            return $this->redirectHandler->redirectToResource($configuration, $orderItem);
        }
        if (!$configuration->isHtmlRequest()) {
            return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
        }
        $view = View::create()->setData(['configuration' => $configuration, $this->metadata->getName() => $orderItem, 'form' => $form->createView()])->setTemplate($configuration->getTemplate(CartActions::ADD . '.html'));
        return $this->viewHandler->handle($configuration, $view);
    }