Skip to content

Commit

Permalink
Merge pull request #2 from valantic-CEC-Deutschland-GmbH/feature/spry…
Browse files Browse the repository at this point in the history
…-2561

Add plugin stack to extend the catalog search for products
  • Loading branch information
HaukeBST authored Nov 17, 2023
2 parents c65c50d + 55ea5f9 commit c46ec5f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function createProductReader(): ProductReader
$this->getLocaleFacade(),
$this->createProductFirstSpiritApiBusinessMapper(),
$this->getConfig(),
$this->getQueryExpanderPlugins(),
);
}

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
private array $queryDataExpanderPlugins = []
) {
$this->productStorageClient = $productStorageClient;
$this->catalogClient = $catalogClient;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -187,4 +188,33 @@ 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;
}

/**
* @param array $queryData
*
* @return array
*/
private function extendQueryData(array $queryData): array
{
foreach ($this->queryDataExpanderPlugins as $queryExpanderPlugin) {
$queryData = $queryExpanderPlugin->expandQueryData($queryData);
}

return $queryData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types = 1);

namespace ValanticSpryker\Zed\ProductFirstSpiritApi\Communication\Dependency\Plugin;

interface QueryExpanderPluginInterface
{
/**
* @param array $queryData
*
* @return array
*/
public function expandQueryData(array $queryData): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ProductFirstSpiritApiDependencyProvider extends AbstractBundleDependencyPr
* @var string
*/
public const LOCALE_FACADE = 'LOCALE_FACADE';
public const PLUGINS_QUERY_EXPANDER = 'PLUGINS_QUERY_EXPANDER';

/**
* @param \Spryker\Zed\Kernel\Container $container
Expand All @@ -48,6 +49,7 @@ public function provideBusinessLayerDependencies(Container $container): Containe
$this->addProductStorageClient($container);
$this->addCatalogClient($container);
$this->addLocaleFacade($container);
$this->addQueryExpanderPlugins($container);

return $container;
}
Expand Down Expand Up @@ -111,4 +113,24 @@ static function (Container $container): LocaleFacadeInterface {
},
);
}

/**
* @param \Spryker\Zed\Kernel\Container $container
*
* @return void
*/
private function addQueryExpanderPlugins(Container $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 [];
}
}

0 comments on commit c46ec5f

Please sign in to comment.