/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $basket = $builder->getData(); if (!$basket instanceof BasketInterface) { throw new \RunTimeException('Please provide a BasketInterface instance'); } $addresses = $this->addressManager->findBy(array('customer' => $basket->getCustomer()->getId(), 'type' => AddressInterface::TYPE_BILLING)); /* * TODO: implement billing address choice $builder->add('billingAddress', 'entity', array( 'class' => $this->addressManager->getClass(), 'choices' => $addresses, 'expanded' => true, )); */ $address = $basket->getBillingAddress() ?: current($addresses); $basket->setBillingAddress($address ?: null); $methods = $this->paymentSelector->getAvailableMethods($basket, $basket->getDeliveryAddress()); $choices = array(); foreach ($methods as $method) { $choices[$method->getCode()] = $method->getName(); } reset($methods); $method = $basket->getPaymentMethod() ?: current($methods); $basket->setPaymentMethod($method ?: null); $sub = $builder->create('paymentMethod', 'choice', array('expanded' => true, 'choice_list' => new SimpleChoiceList($choices))); $sub->addViewTransformer(new PaymentMethodTransformer($this->paymentPool), true); $builder->add($sub); }
/** * Build a basket * * @param \Sonata\Component\Basket\BasketInterface $basket * * @throws \RuntimeException */ public function build(BasketInterface $basket) { $basket->setProductPool($this->productPool); foreach ($basket->getBasketElements() as $basketElement) { if ($basketElement->getProduct() === null) { // restore information if ($basketElement->getProductCode() == null) { throw new \RuntimeException('The product code is empty'); } $productDefinition = $this->productPool->getProduct($basketElement->getProductCode()); $basketElement->setProductDefinition($productDefinition); } } // load the delivery address $deliveryAddressId = $basket->getDeliveryAddressId(); if ($deliveryAddressId) { $address = $this->addressManager->findOneBy(array('id' => $deliveryAddressId)); $basket->setDeliveryAddress($address); } $deliveryMethodCode = $basket->getDeliveryMethodCode(); if ($deliveryMethodCode) { $basket->setDeliveryMethod($this->deliveryPool->getMethod($deliveryMethodCode)); } // load the payment address $billingAddressId = $basket->getBillingAddressId(); if ($billingAddressId) { $address = $this->addressManager->findOneBy(array('id' => $billingAddressId)); $basket->setBillingAddress($address); } // load the payment method $paymentMethodCode = $basket->getPaymentMethodCode(); if ($paymentMethodCode) { $basket->setPaymentMethod($this->paymentPool->getMethod($paymentMethodCode)); } }
/** * Retrieves address with id $id or throws an exception if it doesn't exist * * @param integer $id * * @return AddressInterface * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ protected function getAddress($id) { $address = $this->addressManager->findOneBy(array('id' => $id)); if (null === $address) { throw new NotFoundHttpException(sprintf('Address (%d) not found', $id)); } return $address; }
/** * Write an address, this method is used by both POST and PUT action methods * * @param Request $request Symfony request * @param integer|null $id An Address identifier * * @return \FOS\RestBundle\View\View|FormInterface */ protected function handleWriteAddress($request, $id = null) { $address = $id ? $this->getAddress($id) : null; $form = $this->formFactory->createNamed(null, 'sonata_customer_api_form_address', $address, array('csrf_protection' => false)); $form->bind($request); if ($form->isValid()) { $address = $form->getData(); $this->addressManager->save($address); $view = \FOS\RestBundle\View\View::create($address); $serializationContext = SerializationContext::create(); $serializationContext->setGroups(array('sonata_api_read')); $serializationContext->enableMaxDepthChecks(); $view->setSerializationContext($serializationContext); return $view; } return $form; }