99
1010use GraphQL \Language \AST \FieldNode ;
1111use GraphQL \Language \AST \NodeKind ;
12+
13+ use Magento \Store \Api \Data \StoreInterface ;
1214use Magento \Catalog \Api \Data \CategoryInterface ;
1315use Magento \Catalog \Model \Category ;
1416use Magento \Catalog \Model \ResourceModel \Category \Collection ;
1517use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory ;
18+ use Magento \GraphQl \Model \Query \ContextInterface ;
1619use Magento \CatalogGraphQl \Model \AttributesJoiner ;
1720use Magento \CatalogGraphQl \Model \Category \DepthCalculator ;
1821use Magento \CatalogGraphQl \Model \Category \LevelCalculator ;
22+ use Magento \CatalogGraphQl \Model \Resolver \Categories \DataProvider \Category \CollectionProcessorInterface ;
23+ use Magento \CatalogGraphQl \Model \Category \Filter \SearchCriteria ;
1924use Magento \Framework \EntityManager \MetadataPool ;
2025use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
26+ use Magento \Framework \Exception \LocalizedException ;
27+ use \Exception ;
28+ use \Iterator ;
2129
2230/**
2331 * Category tree data provider
@@ -54,25 +62,41 @@ class CategoryTree
5462 */
5563 private $ metadata ;
5664
65+ /**
66+ * @var CollectionProcessorInterface
67+ */
68+ private $ collectionProcessor ;
69+
70+ /**
71+ * @var SearchCriteria
72+ */
73+ private $ searchCriteria ;
74+
5775 /**
5876 * @param CollectionFactory $collectionFactory
5977 * @param AttributesJoiner $attributesJoiner
6078 * @param DepthCalculator $depthCalculator
6179 * @param LevelCalculator $levelCalculator
6280 * @param MetadataPool $metadata
81+ * @param CollectionProcessorInterface $collectionProcessor
82+ * @param SearchCriteria $searchCriteria
6383 */
6484 public function __construct (
6585 CollectionFactory $ collectionFactory ,
6686 AttributesJoiner $ attributesJoiner ,
6787 DepthCalculator $ depthCalculator ,
6888 LevelCalculator $ levelCalculator ,
69- MetadataPool $ metadata
89+ MetadataPool $ metadata ,
90+ CollectionProcessorInterface $ collectionProcessor ,
91+ SearchCriteria $ searchCriteria
7092 ) {
7193 $ this ->collectionFactory = $ collectionFactory ;
7294 $ this ->attributesJoiner = $ attributesJoiner ;
7395 $ this ->depthCalculator = $ depthCalculator ;
7496 $ this ->levelCalculator = $ levelCalculator ;
7597 $ this ->metadata = $ metadata ;
98+ $ this ->collectionProcessor = $ collectionProcessor ;
99+ $ this ->searchCriteria = $ searchCriteria ;
76100 }
77101
78102 /**
@@ -81,11 +105,27 @@ public function __construct(
81105 * @param ResolveInfo $resolveInfo
82106 * @param int $rootCategoryId
83107 * @param int $storeId
84- * @return \Iterator
108+ * @return Iterator
109+ * @throws LocalizedException
110+ * @throws Exception
85111 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
86112 */
87- public function getTree (ResolveInfo $ resolveInfo , int $ rootCategoryId , int $ storeId ): \ Iterator
113+ public function getTree (ResolveInfo $ resolveInfo , int $ rootCategoryId , int $ storeId ): Iterator
88114 {
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 {
89129 $ categoryQuery = $ resolveInfo ->fieldNodes [0 ];
90130 $ collection = $ this ->collectionFactory ->create ();
91131 $ this ->joinAttributesRecursively ($ collection , $ categoryQuery , $ resolveInfo );
@@ -119,7 +159,7 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId, int $stor
119159 $ rootCategoryId
120160 );
121161
122- return $ collection-> getIterator () ;
162+ return $ collection ;
123163 }
124164
125165 /**
@@ -150,4 +190,31 @@ private function joinAttributesRecursively(
150190 $ this ->joinAttributesRecursively ($ collection , $ node , $ resolveInfo );
151191 }
152192 }
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+ }
153220}
0 commit comments