1212
1313final class OrderExtractor implements ExtractorInterface
1414{
15- private array $ queryParameters = [
16- 'searchCriteria[currentPage] ' => 1 ,
17- 'searchCriteria[pageSize] ' => 100 ,
18- ];
19-
2015 public function __construct (
21- private readonly \Psr \Log \LoggerInterface $ logger ,
22- private readonly \Kiboko \Magento \V2_1 \Client |\Kiboko \Magento \V2_2 \Client |\Kiboko \Magento \V2_3 \Client |\Kiboko \Magento \V2_4 \Client $ client ,
23- private readonly int $ pageSize = 100 ,
24- /** @var FilterGroup[] $filters */
25- private readonly array $ filters = [],
16+ private \Psr \Log \LoggerInterface $ logger ,
17+ private \Kiboko \Magento \V2_1 \Client |\Kiboko \Magento \V2_2 \Client |\Kiboko \Magento \V2_3 \Client |\Kiboko \Magento \V2_4 \Client $ client ,
18+ private QueryParameters $ queryParameters ,
19+ private int $ pageSize = 100 ,
2620 ) {
2721 }
2822
29- private function compileQueryParameters (int $ currentPage = 1 ): array
23+ private function walkFilterVariants (int $ currentPage = 1 ): \ Traversable
3024 {
31- $ parameters = $ this -> queryParameters ;
32- $ parameters [ ' searchCriteria[currentPage] ' ] = $ currentPage ;
33- $ parameters [ ' searchCriteria[pageSize] ' ] = $ this -> pageSize ;
34-
35- $ filters = array_map ( fn ( FilterGroup $ item , int $ key ) => $ item -> compileFilters ( $ key ), $ this -> filters , array_keys ( $ this ->filters ));
36-
37- return array_merge ( $ parameters , ... $ filters ) ;
25+ yield from [
26+ ... $ this -> queryParameters -> walkVariants ([]),
27+ ...[
28+ ' searchCriteria[currentPage] ' => $ currentPage ,
29+ ' searchCriteria[pageSize] ' => $ this ->pageSize ,
30+ ],
31+ ] ;
3832 }
3933
40- private function compileQueryLongParameters ( ): array
34+ private function applyPagination ( array $ parameters , int $ currentPage , int $ pageSize ): array
4135 {
42- $ filters = array_map (fn (FilterGroup $ item , int $ key ) => $ item ->compileLongFilters ($ key ), $ this ->filters , array_keys ($ this ->filters ));
43-
44- return array_merge (...$ filters );
45- }
46-
47- private function generateFinalQueryParameters (array $ queryParameters , array $ queryLongParameters ): array
48- {
49- $ finalQueryParameters = [];
50- if (!empty ($ queryLongParameters )) {
51- foreach ($ queryLongParameters as $ key => $ longParameter ) {
52- if (str_contains ($ key , '[value] ' )) {
53- $ queryParameterWithLongFilters = $ queryParameters ;
54- $ searchString = str_replace ('[value] ' , '' , $ key );
55- $ queryParameterWithLongFilters = array_merge (
56- $ queryParameterWithLongFilters ,
57- [$ searchString .'[field] ' => $ queryLongParameters [$ searchString .'[field] ' ]],
58- [$ searchString .'[conditionType] ' => $ queryLongParameters [$ searchString .'[conditionType] ' ]]
59- );
60- foreach ($ longParameter as $ parameterSlicedValue ) {
61- $ queryParameterWithLongFilters = array_merge (
62- $ queryParameterWithLongFilters ,
63- [$ searchString .'[value] ' => implode (', ' , $ parameterSlicedValue )]
64- );
65- $ finalQueryParameters [] = $ queryParameterWithLongFilters ;
66- }
67- }
68- }
69- } else {
70- $ finalQueryParameters [] = $ queryParameters ;
71- }
72-
73- return $ finalQueryParameters ;
36+ return [
37+ ...$ parameters ,
38+ ...[
39+ 'searchCriteria[currentPage] ' => $ currentPage ,
40+ 'searchCriteria[pageSize] ' => $ pageSize ,
41+ ],
42+ ];
7443 }
7544
7645 public function extract (): iterable
7746 {
47+ $ currentPage = null ;
48+ $ pageCount = null ;
7849 try {
79- $ queryParameters = $ this ->compileQueryParameters ();
80- $ queryLongParameters = $ this ->compileQueryLongParameters ();
81- $ finalQueryParameters = $ this ->generateFinalQueryParameters ($ queryParameters , $ queryLongParameters );
82-
83- foreach ($ finalQueryParameters as $ finalQueryParameter ) {
50+ foreach ($ this ->queryParameters ->walkVariants ([]) as $ parameters ) {
51+ $ currentPage = 1 ;
8452 $ response = $ this ->client ->salesOrderRepositoryV1GetListGet (
85- queryParameters: $ finalQueryParameter ,
53+ queryParameters: $ this -> applyPagination ( iterator_to_array ( $ parameters ), $ currentPage , $ this -> pageSize ) ,
8654 );
55+
8756 if (!$ response instanceof \Kiboko \Magento \V2_1 \Model \SalesDataOrderSearchResultInterface
8857 && !$ response instanceof \Kiboko \Magento \V2_2 \Model \SalesDataOrderSearchResultInterface
8958 && !$ response instanceof \Kiboko \Magento \V2_3 \Model \SalesDataOrderSearchResultInterface
@@ -93,17 +62,15 @@ public function extract(): iterable
9362 }
9463
9564 yield $ this ->processResponse ($ response );
65+ }
9666
97- $ currentPage = 1 ;
98- $ pageCount = ceil ($ response ->getTotalCount () / $ this ->pageSize );
99- while ($ currentPage ++ < $ pageCount ) {
100- $ finalQueryParameter ['searchCriteria[currentPage] ' ] = $ currentPage ;
101- $ response = $ this ->client ->salesOrderRepositoryV1GetListGet (
102- queryParameters: $ finalQueryParameter ,
103- );
10467
105- yield $ this ->processResponse ($ response );
106- }
68+ while ($ currentPage ++ < $ pageCount ) {
69+ $ response = $ this ->client ->salesOrderRepositoryV1GetListGet (
70+ queryParameters: iterator_to_array ($ this ->walkFilterVariants ($ currentPage )),
71+ );
72+
73+ yield $ this ->processResponse ($ response );
10774 }
10875 } catch (NetworkExceptionInterface $ exception ) {
10976 $ this ->logger ->alert (
@@ -113,9 +80,9 @@ public function extract(): iterable
11380 'context ' => [
11481 'path ' => 'order ' ,
11582 'method ' => 'get ' ,
116- 'queryParameters ' => $ this ->generateFinalQueryParameters ( $ this -> compileQueryParameters (), $ this -> compileQueryLongParameters () ),
83+ 'queryParameters ' => $ this ->walkFilterVariants ( ),
11784 ],
118- ]
85+ ],
11986 );
12087 yield new RejectionResultBucket (
12188 'There are some network difficulties. We could not properly connect to the Magento API. There is nothing we could no to fix this currently. Please contact the Magento administrator. ' ,
0 commit comments