From ea5c0144723b61690f4c2c8a1876f6f329d4c641 Mon Sep 17 00:00:00 2001 From: Adrien LUCAS Date: Mon, 23 Aug 2021 10:25:02 +0200 Subject: [PATCH] Introduce ProductFilterInterface::getChannel to allow cleaner extension --- src/Filter/ProductFilter.php | 15 +++++++++++++-- src/Filter/ProductFilterInterface.php | 6 ++++-- .../CreateConfigurableProductEntitiesTask.php | 19 +++++++++---------- .../CreateSimpleProductEntitiesTask.php | 18 ++++++++---------- src/Task/Product/RetrieveProductsTask.php | 2 +- .../RetrieveProductModelsTask.php | 2 +- 6 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/Filter/ProductFilter.php b/src/Filter/ProductFilter.php index 9a5b4a6d..48dbd5fd 100644 --- a/src/Filter/ProductFilter.php +++ b/src/Filter/ProductFilter.php @@ -43,7 +43,7 @@ public function __construct(EntityRepository $productFiltersRulesRepository, Syl $this->syliusAkeneoLocaleCodeProvider = $syliusAkeneoLocaleCodeProvider; } - public function getProductModelFilters(): array + public function getModelQueryParameters(): array { /** @var ProductFiltersRules $productFilterRules */ $productFilterRules = $this->productFiltersRulesRepository->findOneBy([]); @@ -75,7 +75,7 @@ public function getProductModelFilters(): array return $queryParameters; } - public function getProductFilters(): array + public function getQueryParameters(): array { /** @var ProductFiltersRules $productFilterRules */ $productFilterRules = $this->productFiltersRulesRepository->findOneBy([]); @@ -108,6 +108,17 @@ public function getProductFilters(): array return $queryParameters; } + public function getChannel(): ?string + { + /** @var ProductFiltersRules $productFilterRules */ + $productFilterRules = $this->productFiltersRulesRepository->findOneBy([]); + if (!$productFilterRules instanceof ProductFiltersRules) { + return null; + } + + return $productFilterRules->getChannel(); + } + private function getStatus(ProductFiltersRules $productFilterRules, SearchBuilder $queryParameters): SearchBuilder { $status = $productFilterRules->getStatus(); diff --git a/src/Filter/ProductFilterInterface.php b/src/Filter/ProductFilterInterface.php index 3eacc9e5..a990f747 100644 --- a/src/Filter/ProductFilterInterface.php +++ b/src/Filter/ProductFilterInterface.php @@ -6,7 +6,9 @@ interface ProductFilterInterface { - public function getProductModelFilters(): array; + public function getModelQueryParameters(): array; - public function getProductFilters(): array; + public function getQueryParameters(): array; + + public function getChannel(): ?string; } diff --git a/src/Task/Product/CreateConfigurableProductEntitiesTask.php b/src/Task/Product/CreateConfigurableProductEntitiesTask.php index e374de41..15ff0af5 100644 --- a/src/Task/Product/CreateConfigurableProductEntitiesTask.php +++ b/src/Task/Product/CreateConfigurableProductEntitiesTask.php @@ -17,12 +17,12 @@ use Sylius\Component\Resource\Factory\FactoryInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; -use Synolia\SyliusAkeneoPlugin\Entity\ProductFiltersRules; use Synolia\SyliusAkeneoPlugin\Entity\ProductGroup; use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\AfterProcessingProductVariantEvent; use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\BeforeProcessingProductVariantEvent; use Synolia\SyliusAkeneoPlugin\Exceptions\NoProductFiltersConfigurationException; use Synolia\SyliusAkeneoPlugin\Exceptions\Processor\MissingAkeneoAttributeProcessorException; +use Synolia\SyliusAkeneoPlugin\Filter\ProductFilterInterface; use Synolia\SyliusAkeneoPlugin\Logger\Messages; use Synolia\SyliusAkeneoPlugin\Manager\ProductOptionManager; use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface; @@ -31,7 +31,6 @@ use Synolia\SyliusAkeneoPlugin\Provider\AkeneoAttributeProcessorProviderInterface; use Synolia\SyliusAkeneoPlugin\Provider\AkeneoTaskProvider; use Synolia\SyliusAkeneoPlugin\Repository\ChannelRepository; -use Synolia\SyliusAkeneoPlugin\Repository\ProductFiltersRulesRepository; use Synolia\SyliusAkeneoPlugin\Repository\ProductGroupRepository; use Synolia\SyliusAkeneoPlugin\Task\AkeneoTaskInterface; use Synolia\SyliusAkeneoPlugin\Task\AttributeOption\CreateUpdateDeleteTask; @@ -71,8 +70,8 @@ final class CreateConfigurableProductEntitiesTask extends AbstractCreateProductE /** @var string */ private $type; - /** @var \Synolia\SyliusAkeneoPlugin\Repository\ProductFiltersRulesRepository */ - private $productFiltersRulesRepository; + /** @var \Synolia\SyliusAkeneoPlugin\Filter\ProductFilterInterface */ + private $productFilter; /** @var string */ private $scope; @@ -105,7 +104,7 @@ public function __construct( FactoryInterface $productOptionValueFactory, RepositoryInterface $productVariantTranslationRepository, FactoryInterface $productVariantTranslationFactory, - ProductFiltersRulesRepository $productFiltersRulesRepository, + ProductFilterInterface $productFilter, AkeneoAttributeProcessorProviderInterface $akeneoAttributeProcessorProvider, EventDispatcherInterface $dispatcher ) { @@ -130,7 +129,7 @@ public function __construct( $this->productOptionValueFactory = $productOptionValueFactory; $this->productVariantTranslationRepository = $productVariantTranslationRepository; $this->productVariantTranslationFactory = $productVariantTranslationFactory; - $this->productFiltersRulesRepository = $productFiltersRulesRepository; + $this->productFilter = $productFilter; $this->akeneoAttributeProcessorProvider = $akeneoAttributeProcessorProvider; $this->dispatcher = $dispatcher; } @@ -146,12 +145,12 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte $this->logger->debug(self::class); $this->type = 'ConfigurableProduct'; $this->logger->notice(Messages::createOrUpdate($this->type)); - /** @var \Synolia\SyliusAkeneoPlugin\Entity\ProductFiltersRules $filters */ - $filters = $this->productFiltersRulesRepository->findOneBy([]); - if (!$filters instanceof ProductFiltersRules) { + + $scope = $this->productFilter->getChannel(); + if ($scope === null) { throw new NoProductFiltersConfigurationException('Product filters must be configured before importing product attributes.'); } - $this->scope = $filters->getChannel(); + $this->scope = $scope; $processedCount = 0; $totalItemsCount = $this->countTotalProducts(false); diff --git a/src/Task/Product/CreateSimpleProductEntitiesTask.php b/src/Task/Product/CreateSimpleProductEntitiesTask.php index 30e33f15..0d1020fc 100644 --- a/src/Task/Product/CreateSimpleProductEntitiesTask.php +++ b/src/Task/Product/CreateSimpleProductEntitiesTask.php @@ -15,13 +15,13 @@ use Sylius\Component\Resource\Repository\RepositoryInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Synolia\SyliusAkeneoPlugin\Entity\ProductConfiguration; -use Synolia\SyliusAkeneoPlugin\Entity\ProductFiltersRules; use Synolia\SyliusAkeneoPlugin\Event\Product\AfterProcessingProductEvent; use Synolia\SyliusAkeneoPlugin\Event\Product\BeforeProcessingProductEvent; use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\AfterProcessingProductVariantEvent; use Synolia\SyliusAkeneoPlugin\Event\ProductVariant\BeforeProcessingProductVariantEvent; use Synolia\SyliusAkeneoPlugin\Exceptions\Attribute\MissingLocaleTranslationException; use Synolia\SyliusAkeneoPlugin\Exceptions\NoProductFiltersConfigurationException; +use Synolia\SyliusAkeneoPlugin\Filter\ProductFilterInterface; use Synolia\SyliusAkeneoPlugin\Logger\Messages; use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface; use Synolia\SyliusAkeneoPlugin\Payload\Product\ProductCategoriesPayload; @@ -31,7 +31,6 @@ use Synolia\SyliusAkeneoPlugin\Provider\AkeneoAttributeDataProviderInterface; use Synolia\SyliusAkeneoPlugin\Provider\AkeneoTaskProvider; use Synolia\SyliusAkeneoPlugin\Repository\ChannelRepository; -use Synolia\SyliusAkeneoPlugin\Repository\ProductFiltersRulesRepository; use Synolia\SyliusAkeneoPlugin\Service\SyliusAkeneoLocaleCodeProvider; use Synolia\SyliusAkeneoPlugin\Task\AkeneoTaskInterface; @@ -67,8 +66,8 @@ final class CreateSimpleProductEntitiesTask extends AbstractCreateProductEntitie /** @var \Sylius\Component\Product\Generator\SlugGeneratorInterface */ private $productSlugGenerator; - /** @var \Synolia\SyliusAkeneoPlugin\Repository\ProductFiltersRulesRepository */ - private $productFiltersRulesRepository; + /** @var \Synolia\SyliusAkeneoPlugin\Filter\ProductFilterInterface */ + private $productFilter; /** @var \Synolia\SyliusAkeneoPlugin\Service\SyliusAkeneoLocaleCodeProvider */ private $syliusAkeneoLocaleCodeProvider; @@ -98,7 +97,7 @@ public function __construct( EntityManagerInterface $entityManager, AkeneoTaskProvider $taskProvider, LoggerInterface $akeneoLogger, - ProductFiltersRulesRepository $productFiltersRulesRepository, + ProductFilterInterface $productFilter, RepositoryInterface $productTranslationRepository, FactoryInterface $productTranslationFactory, SlugGeneratorInterface $productSlugGenerator, @@ -121,7 +120,7 @@ public function __construct( $this->productFactory = $productFactory; $this->taskProvider = $taskProvider; - $this->productFiltersRulesRepository = $productFiltersRulesRepository; + $this->productFilter = $productFilter; $this->productTranslationRepository = $productTranslationRepository; $this->productTranslationFactory = $productTranslationFactory; $this->productSlugGenerator = $productSlugGenerator; @@ -142,12 +141,11 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte $this->logger->notice(Messages::createOrUpdate($this->type)); $this->productConfiguration = $this->productConfigurationRepository->findOneBy([]); - /** @var \Synolia\SyliusAkeneoPlugin\Entity\ProductFiltersRules $filters */ - $filters = $this->productFiltersRulesRepository->findOneBy([]); - if (!$filters instanceof ProductFiltersRules) { + $scope = $this->productFilter->getChannel(); + if ($scope === null) { throw new NoProductFiltersConfigurationException('Product filters must be configured before importing product attributes.'); } - $this->scope = $filters->getChannel(); + $this->scope = $scope; $processedCount = 0; $totalItemsCount = $this->countTotalProducts(true); diff --git a/src/Task/Product/RetrieveProductsTask.php b/src/Task/Product/RetrieveProductsTask.php index 77387bac..5bf37388 100644 --- a/src/Task/Product/RetrieveProductsTask.php +++ b/src/Task/Product/RetrieveProductsTask.php @@ -53,7 +53,7 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte $this->logger->debug(self::class); $this->logger->notice(Messages::retrieveFromAPI($payload->getType())); - $queryParameters = $this->productFilter->getProductFilters(); + $queryParameters = $this->productFilter->getQueryParameters(); $queryParameters['pagination_type'] = 'search_after'; /** @var \Akeneo\Pim\ApiClient\Pagination\PageInterface|null $resources */ diff --git a/src/Task/ProductModel/RetrieveProductModelsTask.php b/src/Task/ProductModel/RetrieveProductModelsTask.php index 6495eba5..1eaa4ed7 100644 --- a/src/Task/ProductModel/RetrieveProductModelsTask.php +++ b/src/Task/ProductModel/RetrieveProductModelsTask.php @@ -50,7 +50,7 @@ public function __construct( */ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInterface { - $queryParameters = $this->productFilter->getProductModelFilters(); + $queryParameters = $this->productFilter->getModelQueryParameters(); $this->logger->debug(self::class); $this->logger->notice(Messages::retrieveFromAPI($payload->getType()));