Skip to content

Commit d0a2664

Browse files
committed
MC-15986: Category Filtering
- Handle match filters
1 parent e4f6094 commit d0a2664

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
lines changed

app/code/Magento/CatalogGraphQl/Model/Category/CategoryFilter.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,73 +8,95 @@
88
namespace Magento\CatalogGraphQl\Model\Category;
99

1010
use Magento\Catalog\Api\Data\CategoryInterface;
11-
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1211
use Magento\Catalog\Model\ResourceModel\Category\Collection;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\Exception\InputException;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\ScopeInterface;
16+
use Magento\Search\Model\Query;
1317

1418
/**
1519
* Category filter allows to filter collection using 'id, url_key, name' from search criteria.
1620
*/
1721
class CategoryFilter
1822
{
1923
/**
20-
* @var CollectionFactory
24+
* @var ScopeConfigInterface
2125
*/
22-
private $collectionFactory;
26+
private $scopeConfig;
2327

2428
/**
25-
* @param CollectionFactory $collectionFactory
29+
* @param ScopeConfigInterface $scopeConfig
2630
*/
2731
public function __construct(
28-
CollectionFactory $collectionFactory
32+
ScopeConfigInterface $scopeConfig
2933
) {
30-
$this->collectionFactory = $collectionFactory;
34+
$this->scopeConfig = $scopeConfig;
3135
}
3236

3337
/**
3438
* Filter for filtering the requested categories id's based on url_key, ids, name in the result.
3539
*
3640
* @param array $args
3741
* @param Collection $categoryCollection
42+
* @param StoreInterface $store
43+
* @throws InputException
3844
*/
39-
public function applyFilters(
40-
array $args,
41-
Collection $categoryCollection
42-
): void {
45+
public function applyFilters(array $args, Collection $categoryCollection, StoreInterface $store)
46+
{
4347
$categoryCollection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, ['eq' => 1]);
4448
foreach ($args['filters'] as $field => $cond) {
4549
foreach ($cond as $condType => $value) {
4650
if ($field === 'ids') {
4751
$categoryCollection->addIdFilter($value);
4852
} else {
49-
$this->addAttributeFilter($categoryCollection, $field, $condType, $value);
53+
$this->addAttributeFilter($categoryCollection, $field, $condType, $value, $store);
5054
}
5155
}
5256
}
5357
}
5458

5559
/**
60+
* Add filter to category collection
61+
*
5662
* @param Collection $categoryCollection
5763
* @param string $field
5864
* @param string $condType
5965
* @param string|array $value
66+
* @param StoreInterface $store
67+
* @throws InputException
6068
*/
61-
private function addAttributeFilter($categoryCollection, $field, $condType, $value)
69+
private function addAttributeFilter($categoryCollection, $field, $condType, $value, $store)
6270
{
6371
if ($condType === 'match') {
64-
$this->addMatchFilter($categoryCollection, $field, $value);
72+
$this->addMatchFilter($categoryCollection, $field, $value, $store);
6573
return;
6674
}
6775
$categoryCollection->addAttributeToFilter($field, [$condType => $value]);
6876
}
6977

7078
/**
79+
* Add match filter to collection
7180
*
7281
* @param Collection $categoryCollection
7382
* @param string $field
7483
* @param string $value
84+
* @param StoreInterface $store
85+
* @throws InputException
7586
*/
76-
private function addMatchFilter($categoryCollection, $field, $value)
87+
private function addMatchFilter($categoryCollection, $field, $value, $store)
7788
{
78-
$categoryCollection->addAttributeToFilter($field, ['like' => "%{$value}%"]);
89+
$minQueryLength = $this->scopeConfig->getValue(
90+
Query::XML_PATH_MIN_QUERY_LENGTH,
91+
ScopeInterface::SCOPE_STORE,
92+
$store
93+
);
94+
$searchValue = str_replace('%', '', $value);
95+
$matchLength = strlen($searchValue);
96+
if ($matchLength < $minQueryLength) {
97+
throw new InputException(__('Invalid match filter'));
98+
}
99+
100+
$categoryCollection->addAttributeToFilter($field, ['like' => "%{$searchValue}%"]);
79101
}
80102
}

app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryList.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10-
use Magento\CatalogGraphQl\Model\Resolver\Category\CheckCategoryIsActive;
1110
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
11+
use Magento\Framework\Exception\InputException;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1414
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -42,28 +42,20 @@ class CategoryList implements ResolverInterface
4242
*/
4343
private $extractDataFromCategoryTree;
4444

45-
/**
46-
* @var CheckCategoryIsActive
47-
*/
48-
private $checkCategoryIsActive;
49-
5045
/**
5146
* @param CategoryTree $categoryTree
5247
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
53-
* @param CheckCategoryIsActive $checkCategoryIsActive
5448
* @param CategoryFilter $categoryFilter
5549
* @param CollectionFactory $collectionFactory
5650
*/
5751
public function __construct(
5852
CategoryTree $categoryTree,
5953
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
60-
CheckCategoryIsActive $checkCategoryIsActive,
6154
CategoryFilter $categoryFilter,
6255
CollectionFactory $collectionFactory
6356
) {
6457
$this->categoryTree = $categoryTree;
6558
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
66-
$this->checkCategoryIsActive = $checkCategoryIsActive;
6759
$this->categoryFilter = $categoryFilter;
6860
$this->collectionFactory = $collectionFactory;
6961
}
@@ -76,25 +68,47 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7668
if (isset($value[$field->getName()])) {
7769
return $value[$field->getName()];
7870
}
79-
$categoryCollection = $this->collectionFactory->create();
71+
$store = $context->getExtensionAttributes()->getStore();
8072

73+
$rootCategoryIds = [];
8174
if (!isset($args['filters'])) {
82-
$rootCategoryIds = [(int)$context->getExtensionAttributes()->getStore()->getRootCategoryId()];
75+
$rootCategoryIds[] = (int)$store->getRootCategoryId();
8376
} else {
84-
$this->categoryFilter->applyFilters($args, $categoryCollection);
85-
$rootCategoryIds = [];
77+
$categoryCollection = $this->collectionFactory->create();
78+
try {
79+
$this->categoryFilter->applyFilters($args, $categoryCollection, $store);
80+
} catch (InputException $e) {
81+
return [];
82+
}
83+
8684
foreach ($categoryCollection as $category) {
8785
$rootCategoryIds[] = (int)$category->getId();
8886
}
8987
}
90-
$result = [];
91-
foreach ($rootCategoryIds as $rootCategoryId) {
92-
$categoryTree = $this->categoryTree->getTree($info, $rootCategoryId);
88+
89+
$result = $this->fetchCategories($rootCategoryIds, $info);
90+
return $result;
91+
}
92+
93+
/**
94+
* Fetch category tree data
95+
*
96+
* @param array $categoryIds
97+
* @param ResolveInfo $info
98+
* @return array
99+
* @throws GraphQlNoSuchEntityException
100+
*/
101+
private function fetchCategories(array $categoryIds, ResolveInfo $info)
102+
{
103+
$fetchedCategories = [];
104+
foreach ($categoryIds as $categoryId) {
105+
$categoryTree = $this->categoryTree->getTree($info, $categoryId);
93106
if (empty($categoryTree)) {
94107
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
95108
}
96-
$result[] = current($this->extractDataFromCategoryTree->execute($categoryTree));
109+
$fetchedCategories[] = current($this->extractDataFromCategoryTree->execute($categoryTree));
97110
}
98-
return $result;
111+
112+
return $fetchedCategories;
99113
}
100114
}

0 commit comments

Comments
 (0)