Skip to content

Commit 5adecfa

Browse files
committed
Add logical cursor for long query parameters
1 parent 6fe5ec6 commit 5adecfa

File tree

2 files changed

+94
-18
lines changed

2 files changed

+94
-18
lines changed

src/FilterGroup.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
class FilterGroup
88
{
99
private array $filters = [];
10+
private array $longFilters = [];
11+
private int $offset = 0;
12+
private int $lenght = 200;
1013

1114
public function withFilter(Filter $filter): self
1215
{
@@ -19,6 +22,20 @@ public function withFilter(Filter $filter): self
1922
return $this;
2023
}
2124

25+
public function withLongFilter(Filter $filter, int $offset = 0, int $lenght = 200): self
26+
{
27+
$this->longFilters[] = [
28+
'field' => $filter->field,
29+
'value' => $filter->value,
30+
'condition_type' => $filter->conditionType,
31+
];
32+
33+
$this->offset = $offset;
34+
$this->lenght = $lenght;
35+
36+
return $this;
37+
}
38+
2239
public function withFilters(Filter ...$filters): self
2340
{
2441
array_walk($filters, fn (Filter $filter) => $this->filters[] = [
@@ -30,6 +47,25 @@ public function withFilters(Filter ...$filters): self
3047
return $this;
3148
}
3249

50+
private function sliceLongFilter(string $value): iterable
51+
{
52+
$iterator = new \ArrayIterator(explode(',', $value));
53+
while($this->offset < iterator_count($iterator)) {
54+
$filteredValue = array_slice(iterator_to_array($iterator), $this->offset, $this->lenght);
55+
$this->offset += $this->lenght;
56+
yield $filteredValue;
57+
}
58+
}
59+
60+
public function compileLongFilters(int $groupIndex = 0)
61+
{
62+
return array_merge(...array_map(fn (array $item, int $key) => [
63+
sprintf('searchCriteria[filterGroups][%s][filters][%s][field]', $groupIndex, $key) => $item['field'],
64+
sprintf('searchCriteria[filterGroups][%s][filters][%s][value]', $groupIndex, $key) => iterator_to_array($this->sliceLongFilter($item['value'])),
65+
sprintf('searchCriteria[filterGroups][%s][filters][%s][conditionType]', $groupIndex, $key) => $item['condition_type'],
66+
], $this->longFilters, array_keys($this->longFilters)));
67+
}
68+
3369
public function compileFilters(int $groupIndex = 0): array
3470
{
3571
return array_merge(...array_map(fn (array $item, int $key) => [

src/OrderExtractor.php

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(
2626
) {
2727
}
2828

29-
private function compileQueryParameters(int $currentPage = 1): array
29+
private function compileQueryParameters(int $currentPage = 1)
3030
{
3131
$parameters = $this->queryParameters;
3232
$parameters['searchCriteria[currentPage]'] = $currentPage;
@@ -37,31 +37,71 @@ private function compileQueryParameters(int $currentPage = 1): array
3737
return array_merge($parameters, ...$filters);
3838
}
3939

40-
public function extract(): iterable
40+
private function compileQueryLongParameters()
4141
{
42-
try {
43-
$response = $this->client->salesOrderRepositoryV1GetListGet(
44-
queryParameters: $this->compileQueryParameters(),
45-
);
46-
47-
if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataOrderSearchResultInterface
48-
&& !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataOrderSearchResultInterface
49-
&& !$response instanceof \Kiboko\Magento\V2_3\Model\SalesDataOrderSearchResultInterface
50-
&& !$response instanceof \Kiboko\Magento\V2_4\Model\SalesDataOrderSearchResultInterface
51-
) {
52-
return;
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+
}
5368
}
69+
} else {
70+
$finalQueryParameters[] = $queryParameters;
71+
}
72+
return $finalQueryParameters;
73+
}
5474

55-
yield $this->processResponse($response);
75+
public function extract(): iterable
76+
{
77+
try {
78+
$queryParameters = $this->compileQueryParameters();
79+
$queryLongParameters = $this->compileQueryLongParameters();
80+
$finalQueryParameters = $this->generateFinalQueryParameters($queryParameters, $queryLongParameters);
5681

57-
$currentPage = 1;
58-
$pageCount = ceil($response->getTotalCount() / $this->pageSize);
59-
while ($currentPage++ < $pageCount) {
82+
foreach($finalQueryParameters as $finalQueryParameter) {
6083
$response = $this->client->salesOrderRepositoryV1GetListGet(
61-
queryParameters: $this->compileQueryParameters($currentPage),
84+
queryParameters: $finalQueryParameter,
6285
);
86+
if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataOrderSearchResultInterface
87+
&& !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataOrderSearchResultInterface
88+
&& !$response instanceof \Kiboko\Magento\V2_3\Model\SalesDataOrderSearchResultInterface
89+
&& !$response instanceof \Kiboko\Magento\V2_4\Model\SalesDataOrderSearchResultInterface
90+
) {
91+
return;
92+
}
6393

6494
yield $this->processResponse($response);
95+
96+
$currentPage = 1;
97+
$pageCount = ceil($response->getTotalCount() / $this->pageSize);
98+
while ($currentPage++ < $pageCount) {
99+
$response = $this->client->salesOrderRepositoryV1GetListGet(
100+
queryParameters: $this->compileQueryParameters($currentPage),
101+
);
102+
103+
yield $this->processResponse($response);
104+
}
65105
}
66106
} catch (NetworkExceptionInterface $exception) {
67107
$this->logger->alert($exception->getMessage(), ['exception' => $exception]);

0 commit comments

Comments
 (0)