From 8d97ce0cfd57f79d0e379e35c669ab25675f6cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gracjan=20J=C3=B3zefczyk?= Date: Wed, 22 May 2024 09:26:13 +0200 Subject: [PATCH 1/3] OP-225: Refactor Option Taxon Builder --- src/PropertyBuilder/OptionTaxonsBuilder.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/PropertyBuilder/OptionTaxonsBuilder.php b/src/PropertyBuilder/OptionTaxonsBuilder.php index 4dd9b092..08a7a711 100644 --- a/src/PropertyBuilder/OptionTaxonsBuilder.php +++ b/src/PropertyBuilder/OptionTaxonsBuilder.php @@ -15,7 +15,6 @@ use BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapperInterface; use BitBag\SyliusElasticsearchPlugin\Repository\ProductVariantRepositoryInterface; use FOS\ElasticaBundle\Event\PostTransformEvent; -use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Product\Model\ProductOptionInterface; use Sylius\Component\Product\Model\ProductOptionValueInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; @@ -57,23 +56,20 @@ public function consumeEvent(PostTransformEvent $event): void } $document = $event->getDocument(); - $optionValues = $this->productOptionValueRepository->findAll(); + $optionValues = $this->productOptionValueRepository->findBy(['option' => $documentProductOption]); $taxons = []; /** @var ProductOptionValueInterface $optionValue */ foreach ($optionValues as $optionValue) { $option = $optionValue->getOption(); - $productVariant = $this->productVariantRepository->findOneByOptionValue($optionValue); + $productVariants = $this->productVariantRepository->findByOptionValue($optionValue); - if (null === $productVariant) { - continue; - } - - /** @var ProductInterface $product */ - $product = $productVariant->getProduct(); + foreach ($productVariants as $productVariant) { + $product = $productVariant->getProduct(); - if ($documentProductOption === $option && $product->isEnabled()) { - $taxons = array_merge($taxons, $this->productTaxonsMapper->mapToUniqueCodes($product)); + if ($documentProductOption === $option && $product->isEnabled()) { + $taxons = array_merge($taxons, $this->productTaxonsMapper->mapToUniqueCodes($product)); + } } } From c07f46c8c882ce576812297c69c894cb702643bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gracjan=20J=C3=B3zefczyk?= Date: Wed, 22 May 2024 09:26:43 +0200 Subject: [PATCH 2/3] OP-225: Add repository method --- src/Repository/ProductVariantRepository.php | 10 ++++++++++ src/Repository/ProductVariantRepositoryInterface.php | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/Repository/ProductVariantRepository.php b/src/Repository/ProductVariantRepository.php index 9fe016d1..7e4d3249 100644 --- a/src/Repository/ProductVariantRepository.php +++ b/src/Repository/ProductVariantRepository.php @@ -36,4 +36,14 @@ public function findOneByOptionValue(ProductOptionValueInterface $productOptionV ->getOneOrNullResult() ; } + + public function findByOptionValue(ProductOptionValueInterface $productOptionValue): array + { + return $this->baseProductVariantRepository->createQueryBuilder('o') + ->where(':optionValue MEMBER OF o.optionValues') + ->setParameter('optionValue', $productOptionValue) + ->getQuery() + ->getResult() + ; + } } diff --git a/src/Repository/ProductVariantRepositoryInterface.php b/src/Repository/ProductVariantRepositoryInterface.php index 464bf4ce..9f42ebe9 100644 --- a/src/Repository/ProductVariantRepositoryInterface.php +++ b/src/Repository/ProductVariantRepositoryInterface.php @@ -18,4 +18,6 @@ interface ProductVariantRepositoryInterface { public function findOneByOptionValue(ProductOptionValueInterface $productOptionValue): ?ProductVariantInterface; + + public function findByOptionValue(ProductOptionValueInterface $productOptionValue): array; } From 5945fcb61f4dfa59dcdaabdb0af1982a325e40a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gracjan=20J=C3=B3zefczyk?= Date: Wed, 22 May 2024 09:27:41 +0200 Subject: [PATCH 3/3] OP-225: Populate taxon options --- src/EventListener/ResourceIndexListener.php | 25 +++++++++++++------ .../config/services/event_listener.xml | 5 ++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/EventListener/ResourceIndexListener.php b/src/EventListener/ResourceIndexListener.php index 64ca853a..7fac2671 100644 --- a/src/EventListener/ResourceIndexListener.php +++ b/src/EventListener/ResourceIndexListener.php @@ -13,8 +13,10 @@ namespace BitBag\SyliusElasticsearchPlugin\EventListener; use BitBag\SyliusElasticsearchPlugin\Refresher\ResourceRefresherInterface; -use Sylius\Component\Core\Model\Product; +use Sylius\Component\Core\Model\ProductInterface; +use Sylius\Component\Core\Model\ProductVariantInterface; use Sylius\Component\Product\Model\ProductAttribute; +use Sylius\Component\Product\Model\ProductOption; use Sylius\Component\Resource\Model\ResourceInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -28,14 +30,18 @@ final class ResourceIndexListener implements ResourceIndexListenerInterface private RepositoryInterface $attributeRepository; + private RepositoryInterface $optionRepository; + public function __construct( ResourceRefresherInterface $resourceRefresher, array $persistersMap, - RepositoryInterface $attributeRepository + RepositoryInterface $attributeRepository, + RepositoryInterface $optionRepository ) { $this->resourceRefresher = $resourceRefresher; $this->persistersMap = $persistersMap; $this->attributeRepository = $attributeRepository; + $this->optionRepository = $optionRepository; } public function updateIndex(GenericEvent $event): void @@ -54,11 +60,16 @@ public function updateIndex(GenericEvent $event): void $this->resourceRefresher->refresh($resource, $config[self::SERVICE_ID_KEY]); } - if ($resource instanceof Product - && ProductAttribute::class === $config[self::MODEL_KEY] - ) { - foreach ($this->attributeRepository->findAll() as $attribute) { - $this->resourceRefresher->refresh($attribute, $config[self::SERVICE_ID_KEY]); + if ($resource instanceof ProductInterface || $resource instanceof ProductVariantInterface) { + if (ProductAttribute::class === $config[self::MODEL_KEY]) { + foreach ($this->attributeRepository->findAll() as $attribute) { + $this->resourceRefresher->refresh($attribute, $config[self::SERVICE_ID_KEY]); + } + } + if (ProductOption::class === $config[self::MODEL_KEY]) { + foreach ($this->optionRepository->findAll() as $option) { + $this->resourceRefresher->refresh($option, $config[self::SERVICE_ID_KEY]); + } } } } diff --git a/src/Resources/config/services/event_listener.xml b/src/Resources/config/services/event_listener.xml index 1c84b5bd..875a0a33 100644 --- a/src/Resources/config/services/event_listener.xml +++ b/src/Resources/config/services/event_listener.xml @@ -20,14 +20,19 @@ + + + + +