From 2a4e1bebfea618bc7eddc0df2ff18584acf960f0 Mon Sep 17 00:00:00 2001 From: jota Date: Fri, 22 Dec 2017 11:22:59 +0100 Subject: [PATCH 1/3] Added LazyCustomerLoader for OrderType of SyliusAdminApiBundle --- .../Form/ChoiceList/LazyCustomerLoader.php | 67 +++++++++++++++++++ .../AdminApiBundle/Form/Type/OrderType.php | 11 ++- .../Resources/config/services/form.xml | 1 + 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php diff --git a/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php b/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php new file mode 100644 index 00000000000..b2f6cff1141 --- /dev/null +++ b/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php @@ -0,0 +1,67 @@ +customerRepository = $customerRepository; + } + + /** + * {@inheritdoc} + */ + public function loadChoiceList($value = null) + { + return new ArrayChoiceList([], $value); + } + + /** + * {@inheritdoc} + */ + public function loadChoicesForValues(array $values, $value = null) + { + $self = $this; + + $mapEmailToCustomer = function($email) use ($self){ + return $self->customerRepository->findOneBy(['email' => $email]); + }; + + return array_map($mapEmailToCustomer, $values); + } + + /** + * {@inheritdoc} + */ + public function loadValuesForChoices(array $choices, $value = null) + { + $mapCustomerToEmail = function($customer){ + + }; + } +} \ No newline at end of file diff --git a/src/Sylius/Bundle/AdminApiBundle/Form/Type/OrderType.php b/src/Sylius/Bundle/AdminApiBundle/Form/Type/OrderType.php index b904e2bdfa5..3c76e361527 100644 --- a/src/Sylius/Bundle/AdminApiBundle/Form/Type/OrderType.php +++ b/src/Sylius/Bundle/AdminApiBundle/Form/Type/OrderType.php @@ -13,6 +13,7 @@ namespace Sylius\Bundle\AdminApiBundle\Form\Type; +use Sylius\Bundle\AdminApiBundle\Form\ChoiceList\LazyCustomerLoader; use Sylius\Bundle\ChannelBundle\Form\Type\ChannelChoiceType; use Sylius\Bundle\CustomerBundle\Form\Type\CustomerChoiceType; use Sylius\Bundle\LocaleBundle\Form\Type\LocaleChoiceType; @@ -32,14 +33,20 @@ final class OrderType extends AbstractResourceType /** @var RepositoryInterface */ private $localeRepository; + /** + * @var RepositoryInterface + */ + private $customerRepository; + /** * {@inheritdoc} */ - public function __construct(string $dataClass, array $validationGroups = [], RepositoryInterface $localeRepository) + public function __construct(string $dataClass, array $validationGroups = [], RepositoryInterface $localeRepository, RepositoryInterface $customerRepository) { parent::__construct($dataClass, $validationGroups); $this->localeRepository = $localeRepository; + $this->customerRepository = $customerRepository; } /** @@ -52,6 +59,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'constraints' => [ new NotBlank(['groups' => ['sylius']]), ], + 'choices' => [], + 'choice_loader' => new LazyCustomerLoader($this->customerRepository), ]) ->add('localeCode', LocaleChoiceType::class, [ 'constraints' => [ diff --git a/src/Sylius/Bundle/AdminApiBundle/Resources/config/services/form.xml b/src/Sylius/Bundle/AdminApiBundle/Resources/config/services/form.xml index b825fd15d02..35180b092e9 100644 --- a/src/Sylius/Bundle/AdminApiBundle/Resources/config/services/form.xml +++ b/src/Sylius/Bundle/AdminApiBundle/Resources/config/services/form.xml @@ -38,6 +38,7 @@ %sylius.model.order.class% %sylius.form.type.api_order.validation_groups% + From f9567d6cbcf3f78399e10502ae725becebd9a3a3 Mon Sep 17 00:00:00 2001 From: jota Date: Fri, 22 Dec 2017 15:07:17 +0100 Subject: [PATCH 2/3] Fixed LazyCustomerLoader::loadValuesForChoices implementation --- .../AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php b/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php index b2f6cff1141..e1cb6f42ba3 100644 --- a/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php +++ b/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php @@ -61,7 +61,13 @@ public function loadChoicesForValues(array $values, $value = null) public function loadValuesForChoices(array $choices, $value = null) { $mapCustomerToEmail = function($customer){ + if($customer == null){ + return null; + } + return $customer->getEmail(); }; + + return array_map($mapCustomerToEmail, $choices); } } \ No newline at end of file From 7e8d1fef8c33b91b2c5da13e8c00dd02a9c8a356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Chru=C5=9Bciel?= Date: Wed, 18 Dec 2019 08:56:46 +0100 Subject: [PATCH 3/3] [API] Refactoring of lazy customer loader --- .../Form/ChoiceList/LazyCustomerLoader.php | 73 ------------------- .../ChoiceList/Loader/LazyCustomerLoader.php | 46 ++++++++++++ .../AdminApiBundle/Form/Type/OrderType.php | 27 ++++--- .../Resources/config/services/form.xml | 6 +- .../Loader/LazyCustomerLoaderSpec.php | 63 ++++++++++++++++ 5 files changed, 131 insertions(+), 84 deletions(-) delete mode 100644 src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php create mode 100644 src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/Loader/LazyCustomerLoader.php create mode 100644 src/Sylius/Bundle/AdminApiBundle/spec/Form/ChoiceList/Loader/LazyCustomerLoaderSpec.php diff --git a/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php b/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php deleted file mode 100644 index e1cb6f42ba3..00000000000 --- a/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/LazyCustomerLoader.php +++ /dev/null @@ -1,73 +0,0 @@ -customerRepository = $customerRepository; - } - - /** - * {@inheritdoc} - */ - public function loadChoiceList($value = null) - { - return new ArrayChoiceList([], $value); - } - - /** - * {@inheritdoc} - */ - public function loadChoicesForValues(array $values, $value = null) - { - $self = $this; - - $mapEmailToCustomer = function($email) use ($self){ - return $self->customerRepository->findOneBy(['email' => $email]); - }; - - return array_map($mapEmailToCustomer, $values); - } - - /** - * {@inheritdoc} - */ - public function loadValuesForChoices(array $choices, $value = null) - { - $mapCustomerToEmail = function($customer){ - if($customer == null){ - return null; - } - - return $customer->getEmail(); - }; - - return array_map($mapCustomerToEmail, $choices); - } -} \ No newline at end of file diff --git a/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/Loader/LazyCustomerLoader.php b/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/Loader/LazyCustomerLoader.php new file mode 100644 index 00000000000..ab3b887d6df --- /dev/null +++ b/src/Sylius/Bundle/AdminApiBundle/Form/ChoiceList/Loader/LazyCustomerLoader.php @@ -0,0 +1,46 @@ +customerRepository = $customerRepository; + } + + public function loadChoiceList($value = null): ChoiceListInterface + { + return new ArrayChoiceList([], $value); + } + + public function loadChoicesForValues(array $values, $value = null): array + { + return $this->customerRepository->findBy(['email' => $values]); + } + + public function loadValuesForChoices(array $choices, $value = null): array + { + /** Intentionally left blank, as in the only usage of this loader is in the context of api, where we don't need to load choices */ + return []; + } +} diff --git a/src/Sylius/Bundle/AdminApiBundle/Form/Type/OrderType.php b/src/Sylius/Bundle/AdminApiBundle/Form/Type/OrderType.php index 3c76e361527..5fc0ac067de 100644 --- a/src/Sylius/Bundle/AdminApiBundle/Form/Type/OrderType.php +++ b/src/Sylius/Bundle/AdminApiBundle/Form/Type/OrderType.php @@ -13,7 +13,6 @@ namespace Sylius\Bundle\AdminApiBundle\Form\Type; -use Sylius\Bundle\AdminApiBundle\Form\ChoiceList\LazyCustomerLoader; use Sylius\Bundle\ChannelBundle\Form\Type\ChannelChoiceType; use Sylius\Bundle\CustomerBundle\Form\Type\CustomerChoiceType; use Sylius\Bundle\LocaleBundle\Form\Type\LocaleChoiceType; @@ -22,6 +21,7 @@ use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; +use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; @@ -33,20 +33,27 @@ final class OrderType extends AbstractResourceType /** @var RepositoryInterface */ private $localeRepository; - /** - * @var RepositoryInterface - */ - private $customerRepository; + /** @var ?ChoiceLoaderInterface */ + private $customerChoiceLoader; /** * {@inheritdoc} */ - public function __construct(string $dataClass, array $validationGroups = [], RepositoryInterface $localeRepository, RepositoryInterface $customerRepository) - { + public function __construct( + string $dataClass, + array $validationGroups = [], + RepositoryInterface $localeRepository, + ?ChoiceLoaderInterface $customerChoiceLoader = null + ) { parent::__construct($dataClass, $validationGroups); $this->localeRepository = $localeRepository; - $this->customerRepository = $customerRepository; + + if ($customerChoiceLoader === null) { + @trigger_error(sprintf('Not passing a $customerChoiceLoader to %s constructor is deprecated since Sylius 1.5 and will be removed in Sylius 2.0.', self::class), \E_USER_DEPRECATED); + } + + $this->customerChoiceLoader = $customerChoiceLoader; } /** @@ -59,8 +66,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'constraints' => [ new NotBlank(['groups' => ['sylius']]), ], - 'choices' => [], - 'choice_loader' => new LazyCustomerLoader($this->customerRepository), + 'choices' => [], /** Intentionally left blank, as in the only usage of this loader is in the context of api, where we don't need to load choices */ + 'choice_loader' => $this->customerChoiceLoader, ]) ->add('localeCode', LocaleChoiceType::class, [ 'constraints' => [ diff --git a/src/Sylius/Bundle/AdminApiBundle/Resources/config/services/form.xml b/src/Sylius/Bundle/AdminApiBundle/Resources/config/services/form.xml index 35180b092e9..945867d8e45 100644 --- a/src/Sylius/Bundle/AdminApiBundle/Resources/config/services/form.xml +++ b/src/Sylius/Bundle/AdminApiBundle/Resources/config/services/form.xml @@ -38,7 +38,7 @@ %sylius.model.order.class% %sylius.form.type.api_order.validation_groups% - + @@ -74,5 +74,9 @@ + + + + diff --git a/src/Sylius/Bundle/AdminApiBundle/spec/Form/ChoiceList/Loader/LazyCustomerLoaderSpec.php b/src/Sylius/Bundle/AdminApiBundle/spec/Form/ChoiceList/Loader/LazyCustomerLoaderSpec.php new file mode 100644 index 00000000000..e39360f49e6 --- /dev/null +++ b/src/Sylius/Bundle/AdminApiBundle/spec/Form/ChoiceList/Loader/LazyCustomerLoaderSpec.php @@ -0,0 +1,63 @@ +beConstructedWith($customerRepository); + } + + function it_is_choice_loader(): void + { + $this->shouldImplement(ChoiceLoaderInterface::class); + } + + function it_loads_customers_by_email( + CustomerRepositoryInterface $customerRepository, + CustomerInterface $firstCustomer, + CustomerInterface $secondCustomer + ): void { + $customerRepository + ->findBy(['email' => ['first@example.com', 'second@example.com']]) + ->willReturn([$firstCustomer, $secondCustomer]) + ; + + $this + ->loadChoicesForValues(['first@example.com', 'second@example.com']) + ->shouldReturn([$firstCustomer, $secondCustomer]) + ; + } + + function it_does_not_load_any_choices_available(): void { + $this->loadValuesForChoices([])->shouldReturn([]); + } + + function it_provides_empty_array_choice_list(): void { + $this->loadChoiceList()->shouldBeAnInstanceOf(ArrayChoiceList::class); + } +}