Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5adecfa
Add logical cursor for long query parameters
JoMessina Dec 7, 2023
d32cc33
fix type phpstan, manage api return's cursor with the array of parame…
JoMessina Dec 8, 2023
1cdb190
remove IGNORE_ENV_TRUE parameter for cs-fixer
JoMessina Dec 8, 2023
c03dc1d
Ignore a rule from phpstan
JoMessina Jan 9, 2024
3c8721e
fix from cs fixer
JoMessina Jan 9, 2024
9748ff3
Merge branch 'main' into feature/add-cursor-for-long-filter
JoMessina Jan 9, 2024
b0ca2a1
fix from cs fixer
JoMessina Jan 9, 2024
3a5066c
change phpstan config for level 5
JoMessina Jan 9, 2024
1442b14
Change a method name
JoMessina Jan 9, 2024
e973d99
Refactored the way filters are handled
gplanchat Jan 18, 2024
730e90b
[rector] Rector fixes
actions-user Jan 18, 2024
c0a80ee
refacto extractors, add withGroups and withFilters methods
JoMessina Jan 19, 2024
afc78d5
manage page count, fix extractor to use api pagination
JoMessina Jan 23, 2024
047ae6e
fix and add tests for extractors
JoMessina Jan 24, 2024
8854a9c
Merge branch 'main' into feature/add-cursor-for-long-filter
JoMessina Jan 24, 2024
7930389
[rector] Rector fixes
actions-user Jan 24, 2024
d9edd24
Refactored the way filters are handled
gplanchat Jan 30, 2024
784d8a4
[rector] Rector fixes
actions-user Jan 30, 2024
51dbb92
Fixed errors in the unit tests
gplanchat Jan 30, 2024
0ce5f65
Changed the variadic methods to use array_push instead of foreach
gplanchat Jan 30, 2024
912c39c
remove a cast on string, add a catch for UndefinedOptionsException
JoMessina Feb 2, 2024
8f501aa
[rector] Rector fixes
actions-user Feb 2, 2024
dca47a4
Fix client api version to 2.4
JoMessina Jan 31, 2024
9083255
update api-client-magento to lock symfony/serializer
JoMessina Feb 9, 2024
edfe529
valid phpstan 7, use rector and cs-fixer
JoMessina Feb 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/FilterGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
class FilterGroup
{
private array $filters = [];
private array $longFilters = [];
private int $offset = 0;
private int $lenght = 200;

public function withFilter(Filter $filter): self
{
Expand All @@ -19,6 +22,20 @@ public function withFilter(Filter $filter): self
return $this;
}

public function withLongFilter(Filter $filter, int $offset = 0, int $lenght = 200): self
{
$this->longFilters[] = [
'field' => $filter->field,
'value' => $filter->value,
'condition_type' => $filter->conditionType,
];

$this->offset = $offset;
$this->lenght = $lenght;

return $this;
}

public function withFilters(Filter ...$filters): self
{
array_walk($filters, fn (Filter $filter) => $this->filters[] = [
Expand All @@ -30,6 +47,25 @@ public function withFilters(Filter ...$filters): self
return $this;
}

private function sliceLongFilter(string $value): iterable
{
$iterator = new \ArrayIterator(explode(',', $value));
while($this->offset < iterator_count($iterator)) {
$filteredValue = array_slice(iterator_to_array($iterator), $this->offset, $this->lenght);
$this->offset += $this->lenght;
yield $filteredValue;
}
}

public function compileLongFilters(int $groupIndex = 0)
{
return array_merge(...array_map(fn (array $item, int $key) => [
sprintf('searchCriteria[filterGroups][%s][filters][%s][field]', $groupIndex, $key) => $item['field'],
sprintf('searchCriteria[filterGroups][%s][filters][%s][value]', $groupIndex, $key) => iterator_to_array($this->sliceLongFilter($item['value'])),
sprintf('searchCriteria[filterGroups][%s][filters][%s][conditionType]', $groupIndex, $key) => $item['condition_type'],
], $this->longFilters, array_keys($this->longFilters)));
}

public function compileFilters(int $groupIndex = 0): array
{
return array_merge(...array_map(fn (array $item, int $key) => [
Expand Down
76 changes: 58 additions & 18 deletions src/OrderExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
) {
}

private function compileQueryParameters(int $currentPage = 1): array
private function compileQueryParameters(int $currentPage = 1)
{
$parameters = $this->queryParameters;
$parameters['searchCriteria[currentPage]'] = $currentPage;
Expand All @@ -37,31 +37,71 @@ private function compileQueryParameters(int $currentPage = 1): array
return array_merge($parameters, ...$filters);
}

public function extract(): iterable
private function compileQueryLongParameters()
{
try {
$response = $this->client->salesOrderRepositoryV1GetListGet(
queryParameters: $this->compileQueryParameters(),
);

if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataOrderSearchResultInterface
&& !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataOrderSearchResultInterface
&& !$response instanceof \Kiboko\Magento\V2_3\Model\SalesDataOrderSearchResultInterface
&& !$response instanceof \Kiboko\Magento\V2_4\Model\SalesDataOrderSearchResultInterface
) {
return;
$filters = array_map(fn (FilterGroup $item, int $key) => $item->compileLongFilters($key), $this->filters, array_keys($this->filters));

return array_merge(...$filters);
}

private function generateFinalQueryParameters(array $queryParameters, array $queryLongParameters): array
{
$finalQueryParameters = [];
if (!empty($queryLongParameters)) {
foreach ($queryLongParameters as $key => $longParameter) {
if (str_contains($key, '[value]')) {
$queryParameterWithLongFilters = $queryParameters;
$searchString = str_replace('[value]', '', $key);
$queryParameterWithLongFilters = array_merge(
$queryParameterWithLongFilters,
[$searchString.'[field]' => $queryLongParameters[$searchString.'[field]']],
[$searchString.'[conditionType]' => $queryLongParameters[$searchString.'[conditionType]']]
);
foreach ($longParameter as $parameterSlicedValue) {
$queryParameterWithLongFilters = array_merge(
$queryParameterWithLongFilters,
[$searchString.'[value]' => implode(',', $parameterSlicedValue)]
);
$finalQueryParameters[] = $queryParameterWithLongFilters;
}
}
}
} else {
$finalQueryParameters[] = $queryParameters;
}
return $finalQueryParameters;
}

yield $this->processResponse($response);
public function extract(): iterable
{
try {
$queryParameters = $this->compileQueryParameters();
$queryLongParameters = $this->compileQueryLongParameters();
$finalQueryParameters = $this->generateFinalQueryParameters($queryParameters, $queryLongParameters);

$currentPage = 1;
$pageCount = ceil($response->getTotalCount() / $this->pageSize);
while ($currentPage++ < $pageCount) {
foreach($finalQueryParameters as $finalQueryParameter) {
$response = $this->client->salesOrderRepositoryV1GetListGet(
queryParameters: $this->compileQueryParameters($currentPage),
queryParameters: $finalQueryParameter,
);
if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataOrderSearchResultInterface
&& !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataOrderSearchResultInterface
&& !$response instanceof \Kiboko\Magento\V2_3\Model\SalesDataOrderSearchResultInterface
&& !$response instanceof \Kiboko\Magento\V2_4\Model\SalesDataOrderSearchResultInterface
) {
return;
}

yield $this->processResponse($response);

$currentPage = 1;
$pageCount = ceil($response->getTotalCount() / $this->pageSize);
while ($currentPage++ < $pageCount) {
$response = $this->client->salesOrderRepositoryV1GetListGet(
queryParameters: $this->compileQueryParameters($currentPage),
);

yield $this->processResponse($response);
}
}
} catch (NetworkExceptionInterface $exception) {
$this->logger->alert($exception->getMessage(), ['exception' => $exception]);
Expand Down