Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin stack to extend the catalog search for products #2

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [];
}
}
Loading