-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Persoo bundle for product search
- Loading branch information
1 parent
8ec15d1
commit e40970b
Showing
11 changed files
with
290 additions
and
17 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/DependencyInjection/RegisterProductsSearchResultsProvidersCompilerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\DependencyInjection; | ||
|
||
use Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\Exception\ProductSearchResultsProviderPriorityNotSetException; | ||
use Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\ProductSearchResultsProviderResolver; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class RegisterProductsSearchResultsProvidersCompilerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container | ||
*/ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$productSearchResultsProviderResolverDefinition = $container->getDefinition(ProductSearchResultsProviderResolver::class); | ||
$productSearchResultsProvidersDefinitions = $container->findTaggedServiceIds('shopsys.frontend_api.products_search_results_provider'); | ||
|
||
foreach ($productSearchResultsProvidersDefinitions as $serviceId => $tags) { | ||
$priority = null; | ||
|
||
foreach ($tags as $tag) { | ||
if (array_key_exists('priority', $tag)) { | ||
$priority = $tag['priority']; | ||
} | ||
} | ||
|
||
if (!is_int($priority)) { | ||
throw new ProductSearchResultsProviderPriorityNotSetException(sprintf('Service "%s" has not defined required tag priority or its type is not integer.', $serviceId)); | ||
} | ||
|
||
$productSearchResultsProviderResolverDefinition->addMethodCall( | ||
'registerProductSearchResultsProvider', | ||
[ | ||
$serviceId, | ||
$priority, | ||
], | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...lver/Products/Search/Exception/NoProductSearchResultsProviderEnabledOnDomainException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\Exception; | ||
|
||
use Exception; | ||
|
||
class NoProductSearchResultsProviderEnabledOnDomainException extends Exception | ||
{ | ||
/** | ||
* @param int $domainId | ||
*/ | ||
public function __construct(int $domainId) | ||
{ | ||
parent::__construct(sprintf('No product search results provider enabled on domain with id "%d".', $domainId)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...esolver/Products/Search/Exception/ProductSearchResultsProviderPriorityNotSetException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\Exception; | ||
|
||
use Exception; | ||
|
||
class ProductSearchResultsProviderPriorityNotSetException extends Exception | ||
{ | ||
} |
21 changes: 21 additions & 0 deletions
21
...s/Search/Exception/ProductSearchResultsProviderWithSamePriorityAlreadyExistsException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\Exception; | ||
|
||
use Exception; | ||
|
||
class ProductSearchResultsProviderWithSamePriorityAlreadyExistsException extends Exception | ||
{ | ||
/** | ||
* @param string $serviceId | ||
* @param int $priority | ||
*/ | ||
public function __construct(string $serviceId, int $priority) | ||
{ | ||
parent::__construct( | ||
sprintf('Cannot register ProductSearchResultsProvider "%s" with priority "%d" as provider with same priority already exists.', $serviceId, $priority), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\Model\Resolver\Products\Search; | ||
|
||
use Overblog\GraphQLBundle\Definition\Argument; | ||
use Shopsys\FrameworkBundle\Component\Domain\Domain; | ||
use Shopsys\FrontendApiBundle\Component\Validation\PageSizeValidator; | ||
use Shopsys\FrontendApiBundle\Model\Product\Connection\ProductConnection; | ||
use Shopsys\FrontendApiBundle\Model\Product\Filter\ProductFilterFacade; | ||
use Shopsys\FrontendApiBundle\Model\Resolver\AbstractQuery; | ||
|
||
class ProductSearchQuery extends AbstractQuery | ||
{ | ||
/** | ||
* @param \Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\ProductSearchResultsProviderResolver $productSearchResultsProviderResolver | ||
* @param \Shopsys\FrontendApiBundle\Model\Product\Filter\ProductFilterFacade $productFilterFacade | ||
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain | ||
*/ | ||
public function __construct( | ||
protected readonly ProductSearchResultsProviderResolver $productSearchResultsProviderResolver, | ||
protected readonly ProductFilterFacade $productFilterFacade, | ||
protected readonly Domain $domain, | ||
) { | ||
} | ||
|
||
/** | ||
* @param \Overblog\GraphQLBundle\Definition\Argument $argument | ||
* @return \Shopsys\FrontendApiBundle\Model\Product\Connection\ProductConnection | ||
*/ | ||
public function productsSearchQuery(Argument $argument): ProductConnection | ||
{ | ||
PageSizeValidator::checkMaxPageSize($argument); | ||
$this->setDefaultFirstOffsetIfNecessary($argument); | ||
|
||
$productFilterData = $this->productFilterFacade->getValidatedProductFilterDataForAll( | ||
$argument, | ||
); | ||
|
||
$productSearchResultsProvider = $this->productSearchResultsProviderResolver->getProductsSearchResultsProviderByDomainId($this->domain->getId()); | ||
|
||
return $productSearchResultsProvider->getProductsSearchResults($argument, $productFilterData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Model/Resolver/Products/Search/ProductSearchResultsProviderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\Model\Resolver\Products\Search; | ||
|
||
use Overblog\GraphQLBundle\Definition\Argument; | ||
use Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterData; | ||
use Shopsys\FrontendApiBundle\Model\Product\Connection\ProductConnection; | ||
|
||
interface ProductSearchResultsProviderInterface | ||
{ | ||
/** | ||
* @param \Overblog\GraphQLBundle\Definition\Argument $argument | ||
* @param \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterData $productFilterData | ||
* @return \Shopsys\FrontendApiBundle\Model\Product\Connection\ProductConnection | ||
*/ | ||
public function getProductsSearchResults( | ||
Argument $argument, | ||
ProductFilterData $productFilterData, | ||
): ProductConnection; | ||
|
||
/** | ||
* @param int $domainId | ||
* @return bool | ||
*/ | ||
public function isEnabledOnDomain(int $domainId): bool; | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Model/Resolver/Products/Search/ProductSearchResultsProviderResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shopsys\FrontendApiBundle\Model\Resolver\Products\Search; | ||
|
||
use Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\Exception\NoProductSearchResultsProviderEnabledOnDomainException; | ||
use Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\Exception\ProductSearchResultsProviderWithSamePriorityAlreadyExistsException; | ||
use Webmozart\Assert\Assert; | ||
|
||
class ProductSearchResultsProviderResolver | ||
{ | ||
/** | ||
* @var array<int, string> | ||
*/ | ||
protected array $productSearchResultsProvidersServiceIdByPriority = []; | ||
|
||
/** | ||
* @param \Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\ProductSearchResultsProviderInterface[] $productSearchResultsProviders | ||
*/ | ||
public function __construct( | ||
protected readonly iterable $productSearchResultsProviders, | ||
) { | ||
} | ||
|
||
/** | ||
* @param int $domainId | ||
* @return \Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\ProductSearchResultsProviderInterface | ||
*/ | ||
public function getProductsSearchResultsProviderByDomainId( | ||
int $domainId, | ||
): ProductSearchResultsProviderInterface { | ||
Assert::allIsInstanceOf($this->productSearchResultsProviders, ProductSearchResultsProviderInterface::class); | ||
|
||
foreach ($this->getProductsSearchResultsProvidersOrderedByPriority() as $productSearchResultsProvider) { | ||
if ($productSearchResultsProvider->isEnabledOnDomain($domainId)) { | ||
return $productSearchResultsProvider; | ||
} | ||
} | ||
|
||
throw new NoProductSearchResultsProviderEnabledOnDomainException($domainId); | ||
} | ||
|
||
/** | ||
* @return \Shopsys\FrontendApiBundle\Model\Resolver\Products\Search\ProductSearchResultsProviderInterface[] | ||
*/ | ||
protected function getProductsSearchResultsProvidersOrderedByPriority(): array | ||
{ | ||
krsort($this->productSearchResultsProvidersServiceIdByPriority, SORT_NUMERIC); | ||
|
||
$productSearchResultsProvidersOrderedByPriority = []; | ||
|
||
foreach ($this->productSearchResultsProvidersServiceIdByPriority as $serviceId) { | ||
foreach ($this->productSearchResultsProviders as $productSearchResultsProvider) { | ||
if ($productSearchResultsProvider instanceof $serviceId) { | ||
$productSearchResultsProvidersOrderedByPriority[] = $productSearchResultsProvider; | ||
} | ||
} | ||
} | ||
|
||
return $productSearchResultsProvidersOrderedByPriority; | ||
} | ||
|
||
/** | ||
* @param string $serviceId | ||
* @param int $priority | ||
*/ | ||
public function registerProductSearchResultsProvider(string $serviceId, int $priority): void | ||
{ | ||
if (array_key_exists($priority, $this->productSearchResultsProvidersServiceIdByPriority)) { | ||
throw new ProductSearchResultsProviderWithSamePriorityAlreadyExistsException($serviceId, $priority); | ||
} | ||
|
||
$this->productSearchResultsProvidersServiceIdByPriority[$priority] = $serviceId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters