vendor/sylius/sylius/src/Sylius/Bundle/ShippingBundle/Form/Type/ShippingMethodChoiceType.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ShippingBundle\Form\Type;
  12. use Sylius\Component\Registry\ServiceRegistryInterface;
  13. use Sylius\Component\Resource\Repository\RepositoryInterface;
  14. use Sylius\Component\Shipping\Calculator\CalculatorInterface;
  15. use Sylius\Component\Shipping\Model\ShippingMethodInterface;
  16. use Sylius\Component\Shipping\Model\ShippingSubjectInterface;
  17. use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
  18. use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
  19. use Symfony\Component\Form\AbstractType;
  20. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  21. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  22. use Symfony\Component\Form\FormBuilderInterface;
  23. use Symfony\Component\Form\FormInterface;
  24. use Symfony\Component\Form\FormView;
  25. use Symfony\Component\OptionsResolver\Options;
  26. use Symfony\Component\OptionsResolver\OptionsResolver;
  27. final class ShippingMethodChoiceType extends AbstractType
  28. {
  29.     public function __construct(
  30.         private ShippingMethodsResolverInterface $shippingMethodsResolver,
  31.         private ServiceRegistryInterface $calculators,
  32.         private RepositoryInterface $repository
  33.     ) {
  34.     }
  35.     public function buildForm(FormBuilderInterface $builder, array $options): void
  36.     {
  37.         if ($options['multiple']) {
  38.             $builder->addModelTransformer(new CollectionToArrayTransformer());
  39.         }
  40.     }
  41.     public function configureOptions(OptionsResolver $resolver): void
  42.     {
  43.         $resolver
  44.             ->setDefaults([
  45.                 'choices' => function (Options $options) {
  46.                     if (isset($options['subject']) && $this->shippingMethodsResolver->supports($options['subject'])) {
  47.                         return $this->shippingMethodsResolver->getSupportedMethods($options['subject']);
  48.                     }
  49.                     return $this->repository->findAll();
  50.                 },
  51.                 'choice_value' => 'code',
  52.                 'choice_label' => 'name',
  53.                 'choice_translation_domain' => false,
  54.             ])
  55.             ->setDefined([
  56.                 'subject',
  57.             ])
  58.             ->setAllowedTypes('subject'ShippingSubjectInterface::class)
  59.         ;
  60.     }
  61.     /**
  62.      * @psalm-suppress MissingPropertyType
  63.      */
  64.     public function buildView(FormView $viewFormInterface $form, array $options): void
  65.     {
  66.         if (!isset($options['subject'])) {
  67.             return;
  68.         }
  69.         $subject $options['subject'];
  70.         $shippingCosts = [];
  71.         foreach ($view->vars['choices'] as $choiceView) {
  72.             $method $choiceView->data;
  73.             if (!$method instanceof ShippingMethodInterface) {
  74.                 throw new UnexpectedTypeException($methodShippingMethodInterface::class);
  75.             }
  76.             /** @var CalculatorInterface $calculator */
  77.             $calculator $this->calculators->get($method->getCalculator());
  78.             $shippingCosts[$choiceView->value] = $calculator->calculate($subject$method->getConfiguration());
  79.         }
  80.         $view->vars['shipping_costs'] = $shippingCosts;
  81.     }
  82.     public function getParent(): string
  83.     {
  84.         return ChoiceType::class;
  85.     }
  86.     public function getBlockPrefix(): string
  87.     {
  88.         return 'sylius_shipping_method_choice';
  89.     }
  90. }