Skip to content

Commit c0a80ee

Browse files
committed
refacto extractors, add withGroups and withFilters methods
1 parent 730e90b commit c0a80ee

File tree

6 files changed

+140
-141
lines changed

6 files changed

+140
-141
lines changed

src/CustomerExtractor.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@ public function __construct(
2323

2424
private function walkFilterVariants(int $currentPage = 1): \Traversable
2525
{
26-
$parameters = [
27-
...$this->queryParameters,
26+
yield from [
27+
...$this->queryParameters->walkVariants([]),
2828
...[
2929
'searchCriteria[currentPage]' => $currentPage,
3030
'searchCriteria[pageSize]' => $this->pageSize,
3131
],
3232
];
33-
34-
$filters = array_map(fn (FilterGroup $item, int $key) => $item->compileFilters($key), $this->filters, array_keys($this->filters));
35-
36-
return array_merge($parameters, ...$filters);
3733
}
3834

3935
private function applyPagination(array $parameters, int $currentPage, int $pageSize): array
@@ -42,7 +38,7 @@ private function applyPagination(array $parameters, int $currentPage, int $pageS
4238
...$parameters,
4339
...[
4440
'searchCriteria[currentPage]' => $currentPage,
45-
'searchCriteria[pageSize]' => $this->pageSize,
41+
'searchCriteria[pageSize]' => $pageSize,
4642
],
4743
];
4844
}
@@ -55,7 +51,7 @@ public function extract(): iterable
5551
foreach ($this->queryParameters->walkVariants([]) as $parameters) {
5652
$currentPage = 1;
5753
$response = $this->client->customerCustomerRepositoryV1GetListGet(
58-
queryParameters: $parameters,
54+
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
5955
);
6056

6157
if (!$response instanceof \Kiboko\Magento\V2_1\Model\CustomerDataCustomerSearchResultsInterface
@@ -72,7 +68,7 @@ public function extract(): iterable
7268

7369
while ($currentPage++ < $pageCount) {
7470
$response = $this->client->customerCustomerRepositoryV1GetListGet(
75-
queryParameters: $this->walkFilterVariants($currentPage),
71+
queryParameters: iterator_to_array($this->walkFilterVariants($currentPage)),
7672
);
7773

7874
yield $this->processResponse($response);

src/FilterGroup.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ public function withFilter(FilterInterface $filter): self
1818
return $this;
1919
}
2020

21+
public function withFilters(FilterInterface ...$filter): self
22+
{
23+
foreach ($filter as $item) {
24+
$this->filters[] = $item;
25+
}
26+
27+
return $this;
28+
}
29+
2130
/**
2231
* @return \Traversable<int, array>
2332
*/

src/InvoiceExtractor.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,62 @@
1212

1313
final class InvoiceExtractor 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));
25+
yield from [
26+
...$this->queryParameters->walkVariants([]),
27+
...[
28+
'searchCriteria[currentPage]' => $currentPage,
29+
'searchCriteria[pageSize]' => $this->pageSize,
30+
],
31+
];
32+
}
3633

37-
return array_merge($parameters, ...$filters);
34+
private function applyPagination(array $parameters, int $currentPage, int $pageSize): array
35+
{
36+
return [
37+
...$parameters,
38+
...[
39+
'searchCriteria[currentPage]' => $currentPage,
40+
'searchCriteria[pageSize]' => $pageSize,
41+
],
42+
];
3843
}
3944

4045
public function extract(): iterable
4146
{
47+
$currentPage = null;
48+
$pageCount = null;
4249
try {
43-
$response = $this->client->salesInvoiceRepositoryV1GetListGet(
44-
queryParameters: $this->compileQueryParameters(),
45-
);
50+
foreach ($this->queryParameters->walkVariants([]) as $parameters) {
51+
$currentPage = 1;
52+
$response = $this->client->salesInvoiceRepositoryV1GetListGet(
53+
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
54+
);
4655

47-
if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataInvoiceSearchResultInterface
48-
&& !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataInvoiceSearchResultInterface
49-
&& !$response instanceof \Kiboko\Magento\V2_3\Model\SalesDataInvoiceSearchResultInterface
50-
&& !$response instanceof \Kiboko\Magento\V2_4\Model\SalesDataInvoiceSearchResultInterface
51-
) {
52-
return;
56+
if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataInvoiceSearchResultInterface
57+
&& !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataInvoiceSearchResultInterface
58+
&& !$response instanceof \Kiboko\Magento\V2_3\Model\SalesDataInvoiceSearchResultInterface
59+
&& !$response instanceof \Kiboko\Magento\V2_4\Model\SalesDataInvoiceSearchResultInterface
60+
) {
61+
return;
62+
}
63+
64+
yield $this->processResponse($response);
5365
}
5466

55-
yield $this->processResponse($response);
5667

57-
$currentPage = 1;
58-
$pageCount = ceil($response->getTotalCount() / $this->pageSize);
5968
while ($currentPage++ < $pageCount) {
6069
$response = $this->client->salesInvoiceRepositoryV1GetListGet(
61-
queryParameters: $this->compileQueryParameters($currentPage),
70+
queryParameters: iterator_to_array($this->walkFilterVariants($currentPage)),
6271
);
6372

6473
yield $this->processResponse($response);
@@ -71,9 +80,9 @@ public function extract(): iterable
7180
'context' => [
7281
'path' => 'invoice',
7382
'method' => 'get',
74-
'queryParameters' => $this->compileQueryParameters(),
83+
'queryParameters' => $this->walkFilterVariants(),
7584
],
76-
]
85+
],
7786
);
7887
yield new RejectionResultBucket(
7988
'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.',

src/OrderExtractor.php

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,78 +12,47 @@
1212

1313
final 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

Comments
 (0)