|
7 | 7 |
|
8 | 8 | namespace Magento\CatalogGraphQl\Model\Category;
|
9 | 9 |
|
10 |
| -use Magento\Catalog\Api\CategoryListInterface; |
11 |
| -use Magento\Catalog\Api\Data\CategoryInterface; |
12 |
| -use Magento\Framework\App\Config\ScopeConfigInterface; |
| 10 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 11 | +use Magento\Catalog\Api\Data\CategorySearchResultsInterface; |
| 12 | +use Magento\Catalog\Api\Data\CategorySearchResultsInterfaceFactory; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; |
| 14 | +use Magento\CatalogGraphQl\Model\Resolver\Categories\DataProvider\Category\CollectionProcessorInterface; |
| 15 | +use Magento\CatalogGraphQl\Model\Category\Filter\SearchCriteria; |
| 16 | +use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface; |
13 | 17 | use Magento\Framework\Exception\InputException;
|
14 | 18 | use Magento\Framework\GraphQl\Exception\GraphQlInputException;
|
15 |
| -use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Filter; |
16 |
| -use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Sort; |
17 |
| -use Magento\Search\Model\Query; |
| 19 | +use Magento\GraphQl\Model\Query\ContextInterface; |
18 | 20 | use Magento\Store\Api\Data\StoreInterface;
|
19 |
| -use Magento\Store\Model\ScopeInterface; |
20 |
| -use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder; |
21 | 21 |
|
22 | 22 | /**
|
23 | 23 | * Category filter allows filtering category results by attributes.
|
24 | 24 | */
|
25 | 25 | class CategoryFilter
|
26 | 26 | {
|
27 | 27 | /**
|
28 |
| - * @var string |
| 28 | + * @var CollectionFactory |
29 | 29 | */
|
30 |
| - private const SPECIAL_CHARACTERS = '-+~/\\<>\'":*$#@()!,.?`=%&^'; |
| 30 | + private $categoryCollectionFactory; |
31 | 31 |
|
32 | 32 | /**
|
33 |
| - * @var ScopeConfigInterface |
| 33 | + * @var CollectionProcessorInterface |
34 | 34 | */
|
35 |
| - private $scopeConfig; |
| 35 | + private $collectionProcessor; |
36 | 36 |
|
37 | 37 | /**
|
38 |
| - * @var CategoryListInterface |
| 38 | + * @var JoinProcessorInterface |
39 | 39 | */
|
40 |
| - private $categoryList; |
| 40 | + private $extensionAttributesJoinProcessor; |
41 | 41 |
|
42 | 42 | /**
|
43 |
| - * @var Builder |
| 43 | + * @var CategorySearchResultsInterfaceFactory |
44 | 44 | */
|
45 |
| - private $searchCriteriaBuilder; |
| 45 | + private $categorySearchResultsFactory; |
46 | 46 |
|
47 | 47 | /**
|
48 |
| - * @param ScopeConfigInterface $scopeConfig |
49 |
| - * @param CategoryListInterface $categoryList |
50 |
| - * @param Builder $searchCriteriaBuilder |
| 48 | + * @var CategoryRepositoryInterface |
| 49 | + */ |
| 50 | + private $categoryRepository; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var SearchCriteria |
| 54 | + */ |
| 55 | + private $searchCriteria; |
| 56 | + |
| 57 | + /** |
| 58 | + * @param CollectionFactory $categoryCollectionFactory |
| 59 | + * @param CollectionProcessorInterface $collectionProcessor |
| 60 | + * @param JoinProcessorInterface $extensionAttributesJoinProcessor |
| 61 | + * @param CategorySearchResultsInterfaceFactory $categorySearchResultsFactory |
| 62 | + * @param CategoryRepositoryInterface $categoryRepository |
| 63 | + * @param SearchCriteria $searchCriteria |
51 | 64 | */
|
52 | 65 | public function __construct(
|
53 |
| - ScopeConfigInterface $scopeConfig, |
54 |
| - CategoryListInterface $categoryList, |
55 |
| - Builder $searchCriteriaBuilder |
| 66 | + CollectionFactory $categoryCollectionFactory, |
| 67 | + CollectionProcessorInterface $collectionProcessor, |
| 68 | + JoinProcessorInterface $extensionAttributesJoinProcessor, |
| 69 | + CategorySearchResultsInterfaceFactory $categorySearchResultsFactory, |
| 70 | + CategoryRepositoryInterface $categoryRepository, |
| 71 | + SearchCriteria $searchCriteria |
56 | 72 | ) {
|
57 |
| - $this->scopeConfig = $scopeConfig; |
58 |
| - $this->categoryList = $categoryList; |
59 |
| - $this->searchCriteriaBuilder = $searchCriteriaBuilder; |
| 73 | + $this->categoryCollectionFactory = $categoryCollectionFactory; |
| 74 | + $this->collectionProcessor = $collectionProcessor; |
| 75 | + $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor; |
| 76 | + $this->categorySearchResultsFactory = $categorySearchResultsFactory; |
| 77 | + $this->categoryRepository = $categoryRepository; |
| 78 | + $this->searchCriteria = $searchCriteria; |
60 | 79 | }
|
61 | 80 |
|
62 | 81 | /**
|
63 | 82 | * Search for categories
|
64 | 83 | *
|
65 | 84 | * @param array $criteria
|
66 | 85 | * @param StoreInterface $store
|
| 86 | + * @param array $attributeNames |
| 87 | + * @param ContextInterface $context |
67 | 88 | * @return int[]
|
68 | 89 | * @throws InputException
|
69 | 90 | */
|
70 |
| - public function getResult(array $criteria, StoreInterface $store) |
| 91 | + public function getResult(array $criteria, StoreInterface $store, array $attributeNames, ContextInterface $context) |
71 | 92 | {
|
72 |
| - $categoryIds = []; |
73 |
| - $criteria[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($criteria['filters'], $store); |
74 |
| - $criteria[Filter::ARGUMENT_NAME][CategoryInterface::KEY_IS_ACTIVE] = ['eq' => 1]; |
75 |
| - $criteria[Sort::ARGUMENT_NAME][CategoryInterface::KEY_POSITION] = ['ASC']; |
76 |
| - $searchCriteria = $this->searchCriteriaBuilder->build('categoryList', $criteria); |
77 |
| - $pageSize = $criteria['pageSize'] ?? 20; |
78 |
| - $currentPage = $criteria['currentPage'] ?? 1; |
79 |
| - $searchCriteria->setPageSize($pageSize)->setCurrentPage($currentPage); |
| 93 | + $searchCriteria = $this->searchCriteria->buildCriteria($criteria, $store); |
| 94 | + $collection = $this->categoryCollectionFactory->create(); |
| 95 | + $this->extensionAttributesJoinProcessor->process($collection); |
| 96 | + $this->collectionProcessor->process($collection, $searchCriteria, $attributeNames, $context); |
| 97 | + |
| 98 | + /** @var CategorySearchResultsInterface $searchResult */ |
| 99 | + $categories = $this->categorySearchResultsFactory->create(); |
| 100 | + $categories->setSearchCriteria($searchCriteria); |
| 101 | + $categories->setItems($collection->getItems()); |
| 102 | + $categories->setTotalCount($collection->getSize()); |
80 | 103 |
|
81 |
| - $categories = $this->categoryList->getList($searchCriteria); |
| 104 | + $categoryIds = []; |
82 | 105 | foreach ($categories->getItems() as $category) {
|
83 | 106 | $categoryIds[] = (int)$category->getId();
|
84 | 107 | }
|
@@ -106,35 +129,4 @@ public function getResult(array $criteria, StoreInterface $store)
|
106 | 129 | ]
|
107 | 130 | ];
|
108 | 131 | }
|
109 |
| - |
110 |
| - /** |
111 |
| - * Format match filters to behave like fuzzy match |
112 |
| - * |
113 |
| - * @param array $filters |
114 |
| - * @param StoreInterface $store |
115 |
| - * @return array |
116 |
| - * @throws InputException |
117 |
| - */ |
118 |
| - private function formatMatchFilters(array $filters, StoreInterface $store): array |
119 |
| - { |
120 |
| - $minQueryLength = $this->scopeConfig->getValue( |
121 |
| - Query::XML_PATH_MIN_QUERY_LENGTH, |
122 |
| - ScopeInterface::SCOPE_STORE, |
123 |
| - $store |
124 |
| - ); |
125 |
| - |
126 |
| - foreach ($filters as $filter => $condition) { |
127 |
| - $conditionType = current(array_keys($condition)); |
128 |
| - if ($conditionType === 'match') { |
129 |
| - $searchValue = trim(str_replace(self::SPECIAL_CHARACTERS, '', $condition[$conditionType])); |
130 |
| - $matchLength = strlen($searchValue); |
131 |
| - if ($matchLength < $minQueryLength) { |
132 |
| - throw new InputException(__('Invalid match filter. Minimum length is %1.', $minQueryLength)); |
133 |
| - } |
134 |
| - unset($filters[$filter]['match']); |
135 |
| - $filters[$filter]['like'] = '%' . $searchValue . '%'; |
136 |
| - } |
137 |
| - } |
138 |
| - return $filters; |
139 |
| - } |
140 | 132 | }
|
0 commit comments