Skip to content

Commit

Permalink
[shopsys] added Persoo bundle for product search (#2983)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik authored Jan 24, 2024
2 parents d238091 + e391c7d commit 355946d
Show file tree
Hide file tree
Showing 32 changed files with 665 additions and 187 deletions.
32 changes: 32 additions & 0 deletions src/Component/Arguments/AbstractPaginatorArgumentsBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrontendApiBundle\Component\Arguments;

use Overblog\GraphQLBundle\Definition\Builder\MappingInterface;

class AbstractPaginatorArgumentsBuilder implements MappingInterface
{
/**
* @param array $config
* @return array
*/
public function toMappingDefinition(array $config): array
{
return [
'after' => [
'type' => 'String',
],
'first' => [
'type' => 'Int',
],
'before' => [
'type' => 'String',
],
'last' => [
'type' => 'Int',
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Shopsys\FrontendApiBundle\Component\Arguments;

use Overblog\GraphQLBundle\Definition\Builder\MappingInterface;
use Shopsys\FrontendApiBundle\Component\Arguments\Exception\MandatoryArgumentMissingException;

class PaginatorArgumentsBuilder implements MappingInterface
class AbstractProductPaginatorArgumentsBuilder extends AbstractPaginatorArgumentsBuilder
{
protected const CONFIG_ORDER_TYPE_KEY = 'orderingModeType';

Expand All @@ -19,38 +18,17 @@ public function toMappingDefinition(array $config): array
{
$this->checkMandatoryFields($config);

return [
'after' => [
'type' => 'String',
],
'first' => [
'type' => 'Int',
],
'before' => [
'type' => 'String',
],
'last' => [
'type' => 'Int',
],
$mappingDefinition = parent::toMappingDefinition($config);

return array_merge($mappingDefinition, [
'orderingMode' => [
'type' => $config[static::CONFIG_ORDER_TYPE_KEY],
],
'filter' => [
'type' => 'ProductFilter',
'validation' => 'cascade',
],
'search' => [
'type' => 'String',
],
'categorySlug' => [
'type' => 'String',
],
'brandSlug' => [
'type' => 'String',
],
'flagSlug' => [
'type' => 'String',
],
];
]);
}

/**
Expand Down
22 changes: 5 additions & 17 deletions src/Component/Arguments/BlogArticlePaginatorArgumentsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,21 @@

namespace Shopsys\FrontendApiBundle\Component\Arguments;

use Overblog\GraphQLBundle\Definition\Builder\MappingInterface;

class BlogArticlePaginatorArgumentsBuilder implements MappingInterface
class BlogArticlePaginatorArgumentsBuilder extends AbstractPaginatorArgumentsBuilder
{
/**
* @param array $config
* @return array
*/
public function toMappingDefinition(array $config): array
{
return [
'after' => [
'type' => 'String',
],
'first' => [
'type' => 'Int',
],
'before' => [
'type' => 'String',
],
'last' => [
'type' => 'Int',
],
$mappingDefinition = parent::toMappingDefinition($config);

return array_merge($mappingDefinition, [
'onlyHomepageArticles' => [
'type' => 'Boolean',
'defaultValue' => false,
],
];
]);
}
}
31 changes: 31 additions & 0 deletions src/Component/Arguments/ProductPaginatorArgumentsBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrontendApiBundle\Component\Arguments;

class ProductPaginatorArgumentsBuilder extends AbstractProductPaginatorArgumentsBuilder
{
/**
* @param array $config
* @return array
*/
public function toMappingDefinition(array $config): array
{
$this->checkMandatoryFields($config);

$mappingDefinition = parent::toMappingDefinition($config);

return array_merge($mappingDefinition, [
'categorySlug' => [
'type' => 'String',
],
'brandSlug' => [
'type' => 'String',
],
'flagSlug' => [
'type' => 'String',
],
]);
}
}
25 changes: 25 additions & 0 deletions src/Component/Arguments/ProductSearchPaginatorArgumentsBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrontendApiBundle\Component\Arguments;

class ProductSearchPaginatorArgumentsBuilder extends AbstractProductPaginatorArgumentsBuilder
{
/**
* @param array $config
* @return array
*/
public function toMappingDefinition(array $config): array
{
$this->checkMandatoryFields($config);

$mappingDefinition = parent::toMappingDefinition($config);

return array_merge($mappingDefinition, [
'search' => [
'type' => 'String',
],
]);
}
}
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,
],
);
}
}
}
14 changes: 0 additions & 14 deletions src/Model/Blog/Article/BlogArticlesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

class BlogArticlesQuery extends AbstractQuery
{
protected const DEFAULT_FIRST_LIMIT = 10;

/**
* @param \Shopsys\FrameworkBundle\Model\Blog\Article\Elasticsearch\BlogArticleElasticsearchFacade $blogArticleElasticsearchFacade
*/
Expand Down Expand Up @@ -57,16 +55,4 @@ public function blogArticleByCategoryQuery(Argument $argument, BlogCategory $blo

return $paginator->auto($argument, $this->blogArticleElasticsearchFacade->getByBlogCategoryTotalCount($blogCategory));
}

/**
* @param \Overblog\GraphQLBundle\Definition\Argument $argument
*/
protected function setDefaultFirstOffsetIfNecessary(Argument $argument): void
{
if ($argument->offsetExists('first') === false
&& $argument->offsetExists('last') === false
) {
$argument->offsetSet('first', static::DEFAULT_FIRST_LIMIT);
}
}
}
38 changes: 26 additions & 12 deletions src/Model/Product/Connection/ProductConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,31 @@

namespace Shopsys\FrontendApiBundle\Model\Product\Connection;

use Closure;
use Overblog\GraphQLBundle\Relay\Connection\Output\Connection;
use Overblog\GraphQLBundle\Relay\Connection\PageInfoInterface;
use Shopsys\FrameworkBundle\Model\Product\Listing\ProductListOrderingConfig;
use Shopsys\FrontendApiBundle\Model\Product\Filter\ProductFilterOptions;

class ProductConnection extends Connection
{
/**
* @var callable
*/
protected $productFilterOptionsClosure;

/**
* @param \Overblog\GraphQLBundle\Relay\Connection\EdgeInterface[] $edges
* @param \Overblog\GraphQLBundle\Relay\Connection\PageInfoInterface|null $pageInfo
* @param int $totalCount
* @param callable $productFilterOptionsClosure
* @param \Closure $productFilterOptionsClosure
* @param string|null $orderingMode
* @param int|null $totalCount
* @param string $defaultOrderingMode
*/
public function __construct(
array $edges,
?PageInfoInterface $pageInfo,
int $totalCount,
callable $productFilterOptionsClosure,
protected readonly Closure $productFilterOptionsClosure,
protected readonly ?string $orderingMode = null,
protected $totalCount = null,
protected readonly string $defaultOrderingMode = ProductListOrderingConfig::ORDER_BY_PRIORITY,
) {
parent::__construct($edges, $pageInfo);

$this->totalCount = $totalCount;
$this->productFilterOptionsClosure = $productFilterOptionsClosure;
}

/**
Expand All @@ -40,4 +38,20 @@ public function getProductFilterOptions(): ProductFilterOptions
{
return ($this->productFilterOptionsClosure)();
}

/**
* @return string|null
*/
public function getOrderingMode(): ?string
{
return $this->orderingMode;
}

/**
* @return string
*/
public function getDefaultOrderingMode(): string
{
return $this->defaultOrderingMode;
}
}
Loading

0 comments on commit 355946d

Please sign in to comment.