Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $basket = $builder->getData();
     if (!$basket instanceof BasketInterface) {
         throw new \RuntimeException('Please provide a BasketInterface instance');
     }
     $methods = $this->deliverySelector->getAvailableMethods($basket, $basket->getDeliveryAddress());
     if (count($methods) === 0) {
         throw new UndeliverableCountryException($basket->getDeliveryAddress());
     }
     $choices = array();
     foreach ($methods as $method) {
         $choices[$method->getCode()] = $method->getName();
     }
     reset($methods);
     $method = $basket->getDeliveryMethod() ?: current($methods);
     $basket->setDeliveryMethod($method ?: null);
     $sub = $builder->create('deliveryMethod', 'choice', array('expanded' => true, 'choice_list' => new SimpleChoiceList($choices)));
     $sub->addViewTransformer(new DeliveryMethodTransformer($this->deliveryPool), true);
     $builder->add($sub);
 }
 /**
  * Returns if address can deliver the given basket
  *
  * @param AddressInterface $address A Sonata e-commerce address instance
  * @param BasketInterface  $basket  A Sonata e-commerce basket instance
  *
  * @return boolean
  */
 public function isAddressDeliverable(AddressInterface $address, BasketInterface $basket)
 {
     $methods = $this->deliverySelector->getAvailableMethods($basket, $address);
     return count($methods) > 0 ? true : false;
 }