Skip to content

Commit afc78d5

Browse files
committed
manage page count, fix extractor to use api pagination
1 parent c0a80ee commit afc78d5

File tree

8 files changed

+45
-44
lines changed

8 files changed

+45
-44
lines changed

src/CustomerExtractor.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Kiboko\Component\Bucket\AcceptanceResultBucket;
88
use Kiboko\Component\Bucket\RejectionResultBucket;
9-
use Kiboko\Component\Flow\Magento2\Filter\FilterInterface;
109
use Kiboko\Contract\Bucket\ResultBucketInterface;
1110
use Kiboko\Contract\Pipeline\ExtractorInterface;
1211
use Psr\Http\Client\NetworkExceptionInterface;
@@ -53,6 +52,7 @@ public function extract(): iterable
5352
$response = $this->client->customerCustomerRepositoryV1GetListGet(
5453
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
5554
);
55+
$pageCount = (int) ceil($response->getTotalCount() / $this->pageSize);
5656

5757
if (!$response instanceof \Kiboko\Magento\V2_1\Model\CustomerDataCustomerSearchResultsInterface
5858
&& !$response instanceof \Kiboko\Magento\V2_2\Model\CustomerDataCustomerSearchResultsInterface
@@ -63,15 +63,14 @@ public function extract(): iterable
6363
}
6464

6565
yield $this->processResponse($response);
66-
}
67-
6866

69-
while ($currentPage++ < $pageCount) {
70-
$response = $this->client->customerCustomerRepositoryV1GetListGet(
71-
queryParameters: iterator_to_array($this->walkFilterVariants($currentPage)),
72-
);
67+
while ($currentPage++ < $pageCount) {
68+
$response = $this->client->customerCustomerRepositoryV1GetListGet(
69+
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
70+
);
7371

74-
yield $this->processResponse($response);
72+
yield $this->processResponse($response);
73+
}
7574
}
7675
} catch (NetworkExceptionInterface $exception) {
7776
$this->logger->alert(

src/Filter/ArrayFilter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ public function __construct(
1111
public string $conditionType,
1212
public array $value,
1313
private readonly int $threshold = 200
14-
) {}
14+
) {
15+
}
1516

1617
/**
1718
* @return \Traversable<int, {field: string, value: string, conditionType: string}>
1819
*/
1920
public function getIterator(): \Traversable
2021
{
21-
$length = count($this->value);
22+
$length = \count($this->value);
2223
for ($offset = 0; $offset < $length; $offset += $this->threshold) {
2324
yield [
2425
'field' => $this->field,
25-
'value' => implode(',', array_slice($this->value, $offset, $this->threshold, false)),
26+
'value' => implode(',', \array_slice($this->value, $offset, $this->threshold, false)),
2627
'conditionType' => $this->conditionType,
2728
];
2829
}

src/Filter/ScalarFilter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ final class ScalarFilter implements FilterInterface, \IteratorAggregate
99
public function __construct(
1010
public string $field,
1111
public string $conditionType,
12-
public bool|int|float|string|\DateTimeInterface $value,
13-
) {}
12+
public bool|\DateTimeInterface|float|int|string $value,
13+
) {
14+
}
1415

1516
public function getIterator(): \Traversable
1617
{

src/FilterGroup.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function withFilters(FilterInterface ...$filter): self
3232
*/
3333
public function walkFilters(array $parameters, int $groupIndex = 0): \Traversable
3434
{
35-
if (count($this->filters) < 1) {
35+
if (\count($this->filters) < 1) {
3636
return;
3737
}
3838

@@ -48,33 +48,33 @@ private function buildFilters(array $parameters, int $groupIndex, int $filterInd
4848
sprintf('searchCriteria[filterGroups][%s][filters][%s][field]', $groupIndex, $filterIndex) => $current['field'],
4949
sprintf('searchCriteria[filterGroups][%s][filters][%s][value]', $groupIndex, $filterIndex) => $current['value'],
5050
sprintf('searchCriteria[filterGroups][%s][filters][%s][conditionType]', $groupIndex, $filterIndex) => $current['conditionType'],
51-
]
51+
],
5252
];
5353

54-
if (count($next) >= 1) {
54+
if (\count($next) >= 1) {
5555
yield from $this->buildFilters($childParameters, $groupIndex, $filterIndex + 1, ...$next);
5656
} else {
5757
yield $childParameters;
5858
}
5959
}
6060
}
6161

62-
public function greaterThan(string $field, int|float|string|\DateTimeInterface $value): self
62+
public function greaterThan(string $field, \DateTimeInterface|float|int|string $value): self
6363
{
6464
return $this->withFilter(new ScalarFilter($field, 'gt', $value));
6565
}
6666

67-
public function lowerThan(string $field, int|float|string|\DateTimeInterface $value): self
67+
public function lowerThan(string $field, \DateTimeInterface|float|int|string $value): self
6868
{
6969
return $this->withFilter(new ScalarFilter($field, 'lt', $value));
7070
}
7171

72-
public function greaterThanOrEqual(string $field, int|float|string|\DateTimeInterface $value): self
72+
public function greaterThanOrEqual(string $field, \DateTimeInterface|float|int|string $value): self
7373
{
7474
return $this->withFilter(new ScalarFilter($field, 'gteq', $value));
7575
}
7676

77-
public function lowerThanOrEqual(string $field, int|float|string|\DateTimeInterface $value): self
77+
public function lowerThanOrEqual(string $field, \DateTimeInterface|float|int|string $value): self
7878
{
7979
return $this->withFilter(new ScalarFilter($field, 'lteq', $value));
8080
}

src/InvoiceExtractor.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222

2323
private function walkFilterVariants(int $currentPage = 1): \Traversable
2424
{
25-
yield from [
25+
yield from [
2626
...$this->queryParameters->walkVariants([]),
2727
...[
2828
'searchCriteria[currentPage]' => $currentPage,
@@ -52,6 +52,7 @@ public function extract(): iterable
5252
$response = $this->client->salesInvoiceRepositoryV1GetListGet(
5353
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
5454
);
55+
$pageCount = (int) ceil($response->getTotalCount() / $this->pageSize);
5556

5657
if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataInvoiceSearchResultInterface
5758
&& !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataInvoiceSearchResultInterface
@@ -62,15 +63,14 @@ public function extract(): iterable
6263
}
6364

6465
yield $this->processResponse($response);
65-
}
66-
6766

68-
while ($currentPage++ < $pageCount) {
69-
$response = $this->client->salesInvoiceRepositoryV1GetListGet(
70-
queryParameters: iterator_to_array($this->walkFilterVariants($currentPage)),
71-
);
67+
while ($currentPage++ < $pageCount) {
68+
$response = $this->client->salesInvoiceRepositoryV1GetListGet(
69+
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
70+
);
7271

73-
yield $this->processResponse($response);
72+
yield $this->processResponse($response);
73+
}
7474
}
7575
} catch (NetworkExceptionInterface $exception) {
7676
$this->logger->alert(

src/OrderExtractor.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function extract(): iterable
5252
$response = $this->client->salesOrderRepositoryV1GetListGet(
5353
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
5454
);
55+
$pageCount = (int) ceil($response->getTotalCount() / $this->pageSize);
5556

5657
if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataOrderSearchResultInterface
5758
&& !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataOrderSearchResultInterface
@@ -62,15 +63,14 @@ public function extract(): iterable
6263
}
6364

6465
yield $this->processResponse($response);
65-
}
66-
6766

68-
while ($currentPage++ < $pageCount) {
69-
$response = $this->client->salesOrderRepositoryV1GetListGet(
70-
queryParameters: iterator_to_array($this->walkFilterVariants($currentPage)),
71-
);
67+
while ($currentPage++ < $pageCount) {
68+
$response = $this->client->salesOrderRepositoryV1GetListGet(
69+
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
70+
);
7271

73-
yield $this->processResponse($response);
72+
yield $this->processResponse($response);
73+
}
7474
}
7575
} catch (NetworkExceptionInterface $exception) {
7676
$this->logger->alert(

src/ProductExtractor.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function extract(): iterable
5252
$response = $this->client->catalogProductRepositoryV1GetListGet(
5353
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
5454
);
55+
$pageCount = (int) ceil($response->getTotalCount() / $this->pageSize);
5556

5657
if (!$response instanceof \Kiboko\Magento\V2_1\Model\CatalogDataProductSearchResultsInterface
5758
&& !$response instanceof \Kiboko\Magento\V2_2\Model\CatalogDataProductSearchResultsInterface
@@ -62,15 +63,14 @@ public function extract(): iterable
6263
}
6364

6465
yield $this->processResponse($response);
65-
}
66-
6766

68-
while ($currentPage++ < $pageCount) {
69-
$response = $this->client->catalogProductRepositoryV1GetListGet(
70-
queryParameters: iterator_to_array($this->walkFilterVariants($currentPage)),
71-
);
67+
while ($currentPage++ < $pageCount) {
68+
$response = $this->client->catalogProductRepositoryV1GetListGet(
69+
queryParameters: $this->applyPagination(iterator_to_array($parameters), $currentPage, $this->pageSize),
70+
);
7271

73-
yield $this->processResponse($response);
72+
yield $this->processResponse($response);
73+
}
7474
}
7575
} catch (NetworkExceptionInterface $exception) {
7676
$this->logger->alert(

src/QueryParameters.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function withGroups(FilterGroup ...$group): self
3030
*/
3131
public function walkVariants(array $parameters): \Traversable
3232
{
33-
if (count($this->groups) < 1) {
33+
if (\count($this->groups) < 1) {
3434
return;
3535
}
3636

@@ -40,7 +40,7 @@ public function walkVariants(array $parameters): \Traversable
4040
private function buildFilters(array $parameters, int $groupIndex, FilterGroup $first, FilterGroup ...$next): \Traversable
4141
{
4242
foreach ($first->walkFilters($parameters, $groupIndex) as $current) {
43-
if (count($next) >= 1) {
43+
if (\count($next) >= 1) {
4444
yield from $this->buildFilters($current, $groupIndex + 1, ...$next);
4545
} else {
4646
yield $current;

0 commit comments

Comments
 (0)