Sonata\BasketBundle\Controller\Api\BasketController::handleWriteBasket PHP Method

handleWriteBasket() protected method

Write a basket, this method is used by both POST and PUT action methods.
protected handleWriteBasket ( Request $request, integer | null $id = null ) : FOS\RestBundle\View\View | Symfony\Component\Form\FormInterface
$request Symfony\Component\HttpFoundation\Request Symfony request
$id integer | null A basket identifier
return FOS\RestBundle\View\View | Symfony\Component\Form\FormInterface
    protected function handleWriteBasket($request, $id = null)
    {
        $basket = $id ? $this->getBasket($id) : null;
        $form = $this->formFactory->createNamed(null, 'sonata_basket_api_form_basket', $basket, array('csrf_protection' => false));
        $form->bind($request);
        if ($form->isValid()) {
            $basket = $form->getData();
            if ($basket->getCustomerId()) {
                $this->checkExistingCustomerBasket($basket->getCustomerId());
            }
            $this->basketManager->save($basket);
            $view = \FOS\RestBundle\View\View::create($basket);
            $serializationContext = SerializationContext::create();
            $serializationContext->setGroups(array('sonata_api_read'));
            $serializationContext->enableMaxDepthChecks();
            $view->setSerializationContext($serializationContext);
            return $view;
        }
        return $form;
    }