12
12
13
13
final class OrderExtractor implements ExtractorInterface
14
14
{
15
- private array $ queryParameters = [
16
- 'searchCriteria[currentPage] ' => 1 ,
17
- 'searchCriteria[pageSize] ' => 100 ,
18
- ];
19
-
20
15
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 ,
26
20
) {
27
21
}
28
22
29
- private function compileQueryParameters (int $ currentPage = 1 ): array
23
+ private function walkFilterVariants (int $ currentPage = 1 ): \ Traversable
30
24
{
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
+ ] ;
38
32
}
39
33
40
- private function compileQueryLongParameters ( ): array
34
+ private function applyPagination ( array $ parameters , int $ currentPage , int $ pageSize ): array
41
35
{
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
+ ];
74
43
}
75
44
76
45
public function extract (): iterable
77
46
{
47
+ $ currentPage = null ;
48
+ $ pageCount = null ;
78
49
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 ;
84
52
$ response = $ this ->client ->salesOrderRepositoryV1GetListGet (
85
- queryParameters: $ finalQueryParameter ,
53
+ queryParameters: $ this -> applyPagination ( iterator_to_array ( $ parameters ), $ currentPage , $ this -> pageSize ) ,
86
54
);
55
+
87
56
if (!$ response instanceof \Kiboko \Magento \V2_1 \Model \SalesDataOrderSearchResultInterface
88
57
&& !$ response instanceof \Kiboko \Magento \V2_2 \Model \SalesDataOrderSearchResultInterface
89
58
&& !$ response instanceof \Kiboko \Magento \V2_3 \Model \SalesDataOrderSearchResultInterface
@@ -93,25 +62,32 @@ public function extract(): iterable
93
62
}
94
63
95
64
yield $ this ->processResponse ($ response );
65
+ }
96
66
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
- );
104
67
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 );
107
74
}
108
75
} catch (NetworkExceptionInterface $ exception ) {
109
- $ this ->logger ->alert ($ exception ->getMessage (), ['exception ' => $ exception ]);
110
- yield new RejectionResultBucket ([
111
- 'path ' => 'order ' ,
112
- 'method ' => 'get ' ,
113
- 'queryParameters ' => $ this ->generateFinalQueryParameters ($ this ->compileQueryParameters (), $ this ->compileQueryLongParameters ()),
114
- ]);
76
+ $ this ->logger ->alert (
77
+ $ exception ->getMessage (),
78
+ [
79
+ 'exception ' => $ exception ,
80
+ 'context ' => [
81
+ 'path ' => 'order ' ,
82
+ 'method ' => 'get ' ,
83
+ 'queryParameters ' => $ this ->walkFilterVariants (),
84
+ ],
85
+ ],
86
+ );
87
+ yield new RejectionResultBucket (
88
+ '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. ' ,
89
+ $ exception ,
90
+ );
115
91
} catch (\Exception $ exception ) {
116
92
$ this ->logger ->critical ($ exception ->getMessage (), ['exception ' => $ exception ]);
117
93
}
0 commit comments