From 0f3caf0a11c0c7873284c8c6b8fd72e0c1380bb9 Mon Sep 17 00:00:00 2001 From: haukestenzel Date: Fri, 17 Nov 2023 09:03:28 +0100 Subject: [PATCH 1/3] no-task Add query expander plugins to ProductFirstSpiritApiBusinessFactory and ProductReader classes, and update ProductFirstSpiritApiDependencyProvider to provide the plugins. --- .../ProductFirstSpiritApiBusinessFactory.php | 9 +++++++ .../Business/Reader/ProductReader.php | 27 ++++++++++++++++--- .../Plugin/QueryExpanderPluginInterface.php | 15 +++++++++++ ...roductFirstSpiritApiDependencyProvider.php | 17 ++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/ValanticSpryker/Zed/ProductFirstSpiritApi/Communication/Dependency/Plugin/QueryExpanderPluginInterface.php diff --git a/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/ProductFirstSpiritApiBusinessFactory.php b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/ProductFirstSpiritApiBusinessFactory.php index 7703ab6..82f2b70 100644 --- a/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/ProductFirstSpiritApiBusinessFactory.php +++ b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/ProductFirstSpiritApiBusinessFactory.php @@ -29,6 +29,7 @@ public function createProductReader(): ProductReader $this->getLocaleFacade(), $this->createProductFirstSpiritApiBusinessMapper(), $this->getConfig(), + $this->getQueryExpanderPlugins(), ); } @@ -63,4 +64,12 @@ public function getLocaleFacade(): LocaleFacadeInterface { return $this->getProvidedDependency(ProductFirstSpiritApiDependencyProvider::LOCALE_FACADE); } + + /** + * @return array<\ValanticSpryker\Zed\ProductFirstSpiritApi\Communication\Dependency\Plugin\QueryExpanderPluginInterface> + */ + private function getQueryExpanderPlugins(): array + { + return $this->getProvidedDependency(ProductFirstSpiritApiDependencyProvider::PLUGINS_QUERY_EXPANDER); + } } diff --git a/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/Reader/ProductReader.php b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/Reader/ProductReader.php index ea8047e..9b32bae 100644 --- a/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/Reader/ProductReader.php +++ b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/Reader/ProductReader.php @@ -64,13 +64,15 @@ class ProductReader * @param \Spryker\Zed\Locale\Business\LocaleFacadeInterface $localeFacade * @param \ValanticSpryker\Zed\ProductFirstSpiritApi\Business\Mapper\ProductFirstSpiritApiBusinessMapperInterface $businessMapper * @param \ValanticSpryker\Zed\FirstSpiritApi\FirstSpiritApiConfig $firstSpiritApiConfig + * @param array<\ValanticSpryker\Zed\ProductFirstSpiritApi\Communication\Dependency\Plugin\QueryExpanderPluginInterface> $queryDataExpanderPlugins */ public function __construct( ProductStorageClientInterface $productStorageClient, CatalogClientInterface $catalogClient, LocaleFacadeInterface $localeFacade, ProductFirstSpiritApiBusinessMapperInterface $businessMapper, - FirstSpiritApiConfig $firstSpiritApiConfig + FirstSpiritApiConfig $firstSpiritApiConfig, + array $queryDataExpanderPlugins = [] ) { $this->productStorageClient = $productStorageClient; $this->catalogClient = $catalogClient; @@ -121,8 +123,7 @@ public function getAllProducts(FirstSpiritApiRequestTransfer $apiRequestTransfer $collectionTransfer = new FirstSpiritApiCollectionTransfer(); $queryData = $apiRequestTransfer->getQueryData(); - $queryData[self::KEY_PRODUCTS_PER_PAGE] = $this->firstSpiritApiConfig->getPagingSize(); - $queryData = $this->replaceOldArrayKeyWithNewArrayKey($queryData, self::CATEGORY_ID, self::CATEGORY); + $queryData = $this->adjustQueryData($queryData); $searchString = $this->getRequestParameter($queryData, ProductFirstSpiritApiConfig::QUERY_STRING_PARAMETER); $searchResult = $this->catalogClient->catalogSearch($searchString, $queryData); @@ -187,4 +188,24 @@ private function getLocaleFromRequestParameters(array $parameters): string return $locales[$lang] ?? $currentLocale; } + + /** + * @param $queryData + * + * @return array + */ + protected function adjustQueryData($queryData): array + { + $queryData[self::KEY_PRODUCTS_PER_PAGE] = $this->firstSpiritApiConfig->getPagingSize(); + $queryData = $this->replaceOldArrayKeyWithNewArrayKey($queryData, self::CATEGORY_ID, self::CATEGORY); + + $this->extendQueryData($queryData); + + return $queryData; + } + + private function extendQueryData(array $queryData) + { + + } } diff --git a/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Communication/Dependency/Plugin/QueryExpanderPluginInterface.php b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Communication/Dependency/Plugin/QueryExpanderPluginInterface.php new file mode 100644 index 0000000..5846287 --- /dev/null +++ b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Communication/Dependency/Plugin/QueryExpanderPluginInterface.php @@ -0,0 +1,15 @@ +addProductStorageClient($container); $this->addCatalogClient($container); $this->addLocaleFacade($container); + $this->addQueryExpanderPlugins($container); return $container; } @@ -111,4 +113,19 @@ static function (Container $container): LocaleFacadeInterface { }, ); } + + private function addQueryExpanderPlugins($container): void + { + $container->set(static::PLUGINS_QUERY_EXPANDER, function () { + return $this->getQueryExpanderPlugins(); + }); + } + + /** + * @return array<\ValanticSpryker\Zed\ProductFirstSpiritApi\Communication\Dependency\Plugin\QueryExpanderPluginInterface> + */ + private function getQueryExpanderPlugins(): array + { + return []; + } } From 5663c17be62165b7806dab1f581c5a237b5150b0 Mon Sep 17 00:00:00 2001 From: haukestenzel Date: Fri, 17 Nov 2023 09:06:15 +0100 Subject: [PATCH 2/3] no-task Add type hinting and return type declaration to the `extendQueryData` method and iterate over the `queryDataExpanderPlugins` array to expand the query data. --- .../Business/Reader/ProductReader.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/Reader/ProductReader.php b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/Reader/ProductReader.php index 9b32bae..d89a7b2 100644 --- a/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/Reader/ProductReader.php +++ b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/Business/Reader/ProductReader.php @@ -72,7 +72,7 @@ public function __construct( LocaleFacadeInterface $localeFacade, ProductFirstSpiritApiBusinessMapperInterface $businessMapper, FirstSpiritApiConfig $firstSpiritApiConfig, - array $queryDataExpanderPlugins = [] + private array $queryDataExpanderPlugins = [] ) { $this->productStorageClient = $productStorageClient; $this->catalogClient = $catalogClient; @@ -204,8 +204,17 @@ protected function adjustQueryData($queryData): array return $queryData; } - private function extendQueryData(array $queryData) + /** + * @param array $queryData + * + * @return array + */ + private function extendQueryData(array $queryData): array { + foreach ($this->queryDataExpanderPlugins as $queryExpanderPlugin) { + $queryData = $queryExpanderPlugin->expandQueryData($queryData); + } + return $queryData; } } From 55ea5f943eb172f91265429c786382c15ba2b712 Mon Sep 17 00:00:00 2001 From: haukestenzel Date: Fri, 17 Nov 2023 09:48:12 +0100 Subject: [PATCH 3/3] spry-2561 Add query expander plugins to the ProductFirstSpiritApiDependencyProvider class. --- .../ProductFirstSpiritApiDependencyProvider.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ValanticSpryker/Zed/ProductFirstSpiritApi/ProductFirstSpiritApiDependencyProvider.php b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/ProductFirstSpiritApiDependencyProvider.php index c956294..1ded8e8 100644 --- a/src/ValanticSpryker/Zed/ProductFirstSpiritApi/ProductFirstSpiritApiDependencyProvider.php +++ b/src/ValanticSpryker/Zed/ProductFirstSpiritApi/ProductFirstSpiritApiDependencyProvider.php @@ -114,7 +114,12 @@ static function (Container $container): LocaleFacadeInterface { ); } - private function addQueryExpanderPlugins($container): void + /** + * @param \Spryker\Zed\Kernel\Container $container + * + * @return void + */ + private function addQueryExpanderPlugins(Container $container): void { $container->set(static::PLUGINS_QUERY_EXPANDER, function () { return $this->getQueryExpanderPlugins();