Example #1
0
 /**
  * {@inheritdoc}
  */
 public function calculate(ShippingSubjectInterface $subject)
 {
     if (null === ($method = $subject->getMethod())) {
         throw new UndefinedShippingMethodException('Cannot calculate charge for shipping subject without defined shipping method.');
     }
     $calculator = $this->registry->getCalculator($method->getCalculator());
     return $calculator->calculate($subject, $method->getConfiguration());
 }
 /**
  * {@inheritdoc}
  */
 public function buildView(FormView $view, FormInterface $form, array $options)
 {
     $subject = $options['subject'];
     $shippingCosts = array();
     foreach ($view->vars['choices'] as $choiceView) {
         $method = $choiceView->data;
         if (!$method instanceof ShippingMethodInterface) {
             throw new UnexpectedTypeException($method, 'ShippingMethodInterface');
         }
         $calculator = $this->calculators->getCalculator($method->getCalculator());
         $shippingCosts[$choiceView->value] = $calculator->calculate($subject, $method->getConfiguration());
     }
     $view->vars['shipping_costs'] = $shippingCosts;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $builder->addEventSubscriber(new BuildShippingMethodFormListener($this->calculatorRegistry, $builder->getFormFactory()))->add('translations', 'a2lix_translationsForms', array('form_type' => 'sylius_shipping_method_translation', 'label' => 'sylius.form.shipping_method.name'))->add('enabled', 'checkbox', array('required' => false, 'label' => 'sylius.form.shipping_method.enabled'))->add('category', 'sylius_shipping_category_choice', array('required' => false, 'label' => 'sylius.form.shipping_method.category'))->add('categoryRequirement', 'choice', array('choices' => ShippingMethod::getCategoryRequirementLabels(), 'multiple' => false, 'expanded' => true, 'label' => 'sylius.form.shipping_method.category_requirement'))->add('calculator', 'sylius_shipping_calculator_choice', array('label' => 'sylius.form.shipping_method.calculator'));
     $prototypes = array();
     $prototypes['rules'] = array();
     foreach ($this->checkerRegistry->getCheckers() as $type => $checker) {
         $prototypes['rules'][$type] = $builder->create('__name__', $checker->getConfigurationFormType())->getForm();
     }
     $prototypes['calculators'] = array();
     foreach ($this->calculatorRegistry->getCalculators() as $name => $calculator) {
         if (!$calculator->isConfigurable()) {
             continue;
         }
         $prototypes['calculators'][$name] = $builder->create('configuration', $calculator->getConfigurationFormType())->getForm();
     }
     $builder->setAttribute('prototypes', $prototypes);
 }
 /**
  * Add the calculator configuration fields.
  *
  * @param FormInterface $form
  * @param string        $calculatorName
  * @param array         $data
  */
 protected function addConfigurationFields(FormInterface $form, $calculatorName, array $data = array())
 {
     $calculator = $this->calculatorRegistry->getCalculator($calculatorName);
     if (!$calculator->isConfigurable()) {
         return;
     }
     $configurationField = $this->factory->createNamed('configuration', $calculator->getConfigurationFormType(), $data, array('auto_initialize' => false));
     $form->add($configurationField);
 }