Skip to content

Commit 8557f04

Browse files
committed
refacto extractors, add withGroups and withFilters methods
1 parent fa3110f commit 8557f04

File tree

6 files changed

+185
-143
lines changed

6 files changed

+185
-143
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: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,88 @@
88
use Kiboko\Component\Bucket\RejectionResultBucket;
99
use Kiboko\Contract\Bucket\ResultBucketInterface;
1010
use Kiboko\Contract\Pipeline\ExtractorInterface;
11+
use Psr\Http\Client\NetworkExceptionInterface;
1112

1213
final class InvoiceExtractor implements ExtractorInterface
1314
{
14-
private array $queryParameters = [
15-
'searchCriteria[currentPage]' => 1,
16-
'searchCriteria[pageSize]' => 100,
17-
];
18-
1915
public function __construct(
20-
private readonly \Psr\Log\LoggerInterface $logger,
21-
private readonly \Kiboko\Magento\V2_1\Client|\Kiboko\Magento\V2_2\Client|\Kiboko\Magento\V2_3\Client|\Kiboko\Magento\V2_4\Client $client,
22-
private readonly int $pageSize = 100,
23-
/** @var FilterGroup[] $filters */
24-
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,
2520
) {
2621
}
2722

28-
private function compileQueryParameters(int $currentPage = 1): array
23+
private function walkFilterVariants(int $currentPage = 1): \Traversable
2924
{
30-
$parameters = $this->queryParameters;
31-
$parameters['searchCriteria[currentPage]'] = $currentPage;
32-
$parameters['searchCriteria[pageSize]'] = $this->pageSize;
33-
34-
$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+
}
3533

36-
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+
];
3743
}
3844

3945
public function extract(): iterable
4046
{
47+
$currentPage = null;
48+
$pageCount = null;
4149
try {
42-
$response = $this->client->salesInvoiceRepositoryV1GetListGet(
43-
queryParameters: $this->compileQueryParameters(),
44-
);
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+
);
55+
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+
}
4563

46-
if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataInvoiceSearchResultInterface
47-
&& !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataInvoiceSearchResultInterface
48-
&& !$response instanceof \Kiboko\Magento\V2_3\Model\SalesDataInvoiceSearchResultInterface
49-
&& !$response instanceof \Kiboko\Magento\V2_4\Model\SalesDataInvoiceSearchResultInterface
50-
) {
51-
return;
64+
yield $this->processResponse($response);
5265
}
5366

54-
yield $this->processResponse($response);
5567

56-
$currentPage = 1;
57-
$pageCount = ceil($response->getTotalCount() / $this->pageSize);
5868
while ($currentPage++ < $pageCount) {
5969
$response = $this->client->salesInvoiceRepositoryV1GetListGet(
60-
queryParameters: $this->compileQueryParameters($currentPage),
70+
queryParameters: iterator_to_array($this->walkFilterVariants($currentPage)),
6171
);
6272

6373
yield $this->processResponse($response);
6474
}
75+
} catch (NetworkExceptionInterface $exception) {
76+
$this->logger->alert(
77+
$exception->getMessage(),
78+
[
79+
'exception' => $exception,
80+
'context' => [
81+
'path' => 'invoice',
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+
);
6591
} catch (\Exception $exception) {
66-
$this->logger->alert($exception->getMessage(), ['exception' => $exception]);
92+
$this->logger->critical($exception->getMessage(), ['exception' => $exception]);
6793
}
6894
}
6995

src/OrderExtractor.php

Lines changed: 48 additions & 72 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,25 +62,32 @@ 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) {
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+
);
11591
} catch (\Exception $exception) {
11692
$this->logger->critical($exception->getMessage(), ['exception' => $exception]);
11793
}

0 commit comments

Comments
 (0)