/**
  * {@inheritdoc}
  */
 public function save(BasketInterface $basket)
 {
     if ($basket->getCustomerId()) {
         $this->basketManager->save($basket);
     } else {
         $this->session->set(BasketSessionFactory::SESSION_BASE_NAME . 'new', $basket);
     }
 }
Beispiel #2
0
 /**
  * Write a basket element, this method is used by both POST and PUT action methods.
  *
  * @param int     $basketId  A Sonata ecommerce basket identifier
  * @param Request $request   A Symfony Request service
  * @param int     $elementId A Sonata ecommerce basket element identifier
  *
  * @return \FOS\RestBundle\View\View|FormInterface
  */
 protected function handleWriteBasketElement($basketId, $request, $elementId = null)
 {
     $basket = $this->getBasket($basketId);
     $this->basketBuilder->build($basket);
     $productId = $request->request->get('productId');
     $product = $this->getProduct($productId);
     $productPool = $basket->getProductPool();
     $code = $productPool->getProductCode($product);
     $productDefinition = $productPool->getProduct($code);
     $basketElement = $elementId ? $this->getBasketElement($elementId) : $this->basketElementManager->create();
     $basketElement->setProductDefinition($productDefinition);
     $form = $this->formFactory->createNamed(null, 'sonata_basket_api_form_basket_element', $basketElement, array('csrf_protection' => false));
     $form->bind($request);
     if ($form->isValid()) {
         $basketElement = $form->getData();
         if ($elementId) {
             $this->basketElementManager->save($basketElement);
         } else {
             $basket->addBasketElement($basketElement);
             $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;
 }