/** * {@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; }
/** * 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); }