Skip to content

Commit

Permalink
frontend API: removed searching in products based on brand, category …
Browse files Browse the repository at this point in the history
…or flag query

- YAGNI violation
  • Loading branch information
TomasLudvik committed Jan 18, 2024
1 parent cd86156 commit ce4ab1e
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 56 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',
],
]);
}
}
6 changes: 2 additions & 4 deletions src/Model/Product/Filter/ProductFilterFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,16 @@ public function getProductFilterConfigForBrand(Brand $brand): ProductFilterConfi

/**
* @param \Shopsys\FrameworkBundle\Model\Category\Category $category
* @param string $searchText
* @return \Shopsys\FrameworkBundle\Model\Product\Filter\ProductFilterConfig
*/
public function getProductFilterConfigForCategory(Category $category, string $searchText = ''): ProductFilterConfig
public function getProductFilterConfigForCategory(Category $category): ProductFilterConfig
{
$cacheKey = 'category_' . $category->getId() . '_search_' . $searchText;
$cacheKey = 'category_' . $category->getId();

if (!array_key_exists($cacheKey, $this->productFilterConfigCache)) {
$this->productFilterConfigCache[$cacheKey] = $this->productFilterConfigFactory->createForCategory(
$this->domain->getLocale(),
$category,
$searchText,
);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Model/Product/ProductFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public function getSellableByUuid(string $uuid, int $domainId, PricingGroup $pri
* @param string $search
* @return int
*/
public function getFilteredProductsCountOnCurrentDomain(ProductFilterData $productFilterData, string $search): int
{
public function getFilteredProductsCountOnCurrentDomain(
ProductFilterData $productFilterData,
string $search = '',
): int {
$filterQuery = $this->filterQueryFactory->createListableWithProductFilter($productFilterData);

if ($search !== '') {
Expand All @@ -66,7 +68,7 @@ public function getFilteredProductsOnCurrentDomain(
int $offset,
string $orderingModeId,
ProductFilterData $productFilterData,
string $search,
string $search = '',
): array {
$filterQuery = $this->filterQueryFactory->createWithProductFilterData(
$productFilterData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ BrandDecorator:
type: "ProductConnection"
description: "Paginated and ordered products of brand"
argsBuilder:
builder: "PaginatorArgumentsBuilder"
builder: "ProductPaginatorArgumentsBuilder"
config:
orderingModeType: 'ProductOrderingModeEnum'
resolve: '@=query("productsByBrandQuery", args, value)'
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ CategoryDecorator:
type: "ProductConnection"
description: "Paginated and ordered products of category"
argsBuilder:
builder: "PaginatorArgumentsBuilder"
builder: "ProductPaginatorArgumentsBuilder"
config:
orderingModeType: 'ProductOrderingModeEnum'
resolve: '@=query("productsByCategoryQuery", args, value)'
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/graphql-types/QueryDecorator.types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ QueryDecorator:
products:
type: "ProductConnection"
argsBuilder:
builder: "PaginatorArgumentsBuilder"
builder: "ProductPaginatorArgumentsBuilder"
config:
orderingModeType: 'ProductOrderingModeEnum'
resolve: "@=query('productsQuery', args)"
Expand All @@ -35,7 +35,7 @@ QueryDecorator:
productsSearch:
type: "ProductConnection!"
argsBuilder:
builder: "PaginatorArgumentsBuilder"
builder: "ProductSearchPaginatorArgumentsBuilder"
config:
orderingModeType: 'ProductOrderingModeEnum'
resolve: "@=query('productsSearchQuery', args)"
Expand Down

0 comments on commit ce4ab1e

Please sign in to comment.