9
9
10
10
use GraphQL \Language \AST \FieldNode ;
11
11
use GraphQL \Language \AST \NodeKind ;
12
+
13
+ use Magento \Store \Api \Data \StoreInterface ;
12
14
use Magento \Catalog \Api \Data \CategoryInterface ;
13
15
use Magento \Catalog \Model \Category ;
14
16
use Magento \Catalog \Model \ResourceModel \Category \Collection ;
15
17
use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory ;
18
+ use Magento \GraphQl \Model \Query \ContextInterface ;
16
19
use Magento \CatalogGraphQl \Model \AttributesJoiner ;
17
20
use Magento \CatalogGraphQl \Model \Category \DepthCalculator ;
18
21
use Magento \CatalogGraphQl \Model \Category \LevelCalculator ;
22
+ use Magento \CatalogGraphQl \Model \Resolver \Categories \DataProvider \Category \CollectionProcessorInterface ;
23
+ use Magento \CatalogGraphQl \Model \Category \Filter \SearchCriteria ;
19
24
use Magento \Framework \EntityManager \MetadataPool ;
20
25
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
26
+ use Magento \Framework \Exception \LocalizedException ;
27
+ use \Exception ;
28
+ use \Iterator ;
21
29
22
30
/**
23
31
* Category tree data provider
@@ -54,25 +62,41 @@ class CategoryTree
54
62
*/
55
63
private $ metadata ;
56
64
65
+ /**
66
+ * @var CollectionProcessorInterface
67
+ */
68
+ private $ collectionProcessor ;
69
+
70
+ /**
71
+ * @var SearchCriteria
72
+ */
73
+ private $ searchCriteria ;
74
+
57
75
/**
58
76
* @param CollectionFactory $collectionFactory
59
77
* @param AttributesJoiner $attributesJoiner
60
78
* @param DepthCalculator $depthCalculator
61
79
* @param LevelCalculator $levelCalculator
62
80
* @param MetadataPool $metadata
81
+ * @param CollectionProcessorInterface $collectionProcessor
82
+ * @param SearchCriteria $searchCriteria
63
83
*/
64
84
public function __construct (
65
85
CollectionFactory $ collectionFactory ,
66
86
AttributesJoiner $ attributesJoiner ,
67
87
DepthCalculator $ depthCalculator ,
68
88
LevelCalculator $ levelCalculator ,
69
- MetadataPool $ metadata
89
+ MetadataPool $ metadata ,
90
+ CollectionProcessorInterface $ collectionProcessor ,
91
+ SearchCriteria $ searchCriteria
70
92
) {
71
93
$ this ->collectionFactory = $ collectionFactory ;
72
94
$ this ->attributesJoiner = $ attributesJoiner ;
73
95
$ this ->depthCalculator = $ depthCalculator ;
74
96
$ this ->levelCalculator = $ levelCalculator ;
75
97
$ this ->metadata = $ metadata ;
98
+ $ this ->collectionProcessor = $ collectionProcessor ;
99
+ $ this ->searchCriteria = $ searchCriteria ;
76
100
}
77
101
78
102
/**
@@ -81,11 +105,27 @@ public function __construct(
81
105
* @param ResolveInfo $resolveInfo
82
106
* @param int $rootCategoryId
83
107
* @param int $storeId
84
- * @return \Iterator
108
+ * @return Iterator
109
+ * @throws LocalizedException
110
+ * @throws Exception
85
111
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
86
112
*/
87
- public function getTree (ResolveInfo $ resolveInfo , int $ rootCategoryId , int $ storeId ): \ Iterator
113
+ public function getTree (ResolveInfo $ resolveInfo , int $ rootCategoryId , int $ storeId ): Iterator
88
114
{
115
+ $ collection = $ this ->getCollection ($ resolveInfo , $ rootCategoryId );
116
+ return $ collection ->getIterator ();
117
+ }
118
+
119
+ /**
120
+ * Return prepared collection
121
+ *
122
+ * @param ResolveInfo $resolveInfo
123
+ * @param int $rootCategoryId
124
+ * @return Collection
125
+ * @throws LocalizedException
126
+ * @throws Exception
127
+ */
128
+ private function getCollection (ResolveInfo $ resolveInfo , int $ rootCategoryId ) : Collection {
89
129
$ categoryQuery = $ resolveInfo ->fieldNodes [0 ];
90
130
$ collection = $ this ->collectionFactory ->create ();
91
131
$ this ->joinAttributesRecursively ($ collection , $ categoryQuery , $ resolveInfo );
@@ -119,7 +159,7 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId, int $stor
119
159
$ rootCategoryId
120
160
);
121
161
122
- return $ collection-> getIterator () ;
162
+ return $ collection ;
123
163
}
124
164
125
165
/**
@@ -150,4 +190,31 @@ private function joinAttributesRecursively(
150
190
$ this ->joinAttributesRecursively ($ collection , $ node , $ resolveInfo );
151
191
}
152
192
}
193
+
194
+ /**
195
+ * Returns categories tree starting from parent $rootCategoryId with filtration
196
+ *
197
+ * @param ResolveInfo $resolveInfo
198
+ * @param int $rootCategoryId
199
+ * @param array $criteria
200
+ * @param StoreInterface $store
201
+ * @param array $attributeNames
202
+ * @param ContextInterface $context
203
+ * @return Iterator
204
+ * @throws LocalizedException
205
+ * @throws Exception
206
+ */
207
+ public function getFilteredTree (
208
+ ResolveInfo $ resolveInfo ,
209
+ int $ rootCategoryId ,
210
+ array $ criteria ,
211
+ StoreInterface $ store ,
212
+ array $ attributeNames ,
213
+ ContextInterface $ context
214
+ ): Iterator {
215
+ $ searchCriteria = $ this ->searchCriteria ->buildCriteria ($ criteria , $ store );
216
+ $ collection = $ this ->getCollection ($ resolveInfo , $ rootCategoryId );
217
+ $ this ->collectionProcessor ->process ($ collection , $ searchCriteria , $ attributeNames , $ context );
218
+ return $ collection ->getIterator ();
219
+ }
153
220
}
0 commit comments