Skip to content

Commit 37379dc

Browse files
committed
Improved the management of filters
1 parent 9a2fa0e commit 37379dc

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

src/CustomerExtractor.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,20 @@ public function __construct(
1818
private \Psr\Log\LoggerInterface $logger,
1919
private \Kiboko\Magento\V2_1\Client|\Kiboko\Magento\V2_2\Client|\Kiboko\Magento\V2_3\Client|\Kiboko\Magento\V2_4\Client $client,
2020
private int $pageSize = 100,
21+
/** @var FilterGroup[] $filters */
22+
private array $filters = [],
2123
) {
2224
}
2325

24-
public function withFilter(string $filter, mixed $value): self
25-
{
26-
$this->queryParameters[$filter] = $value;
27-
28-
return $this;
29-
}
30-
3126
private function compileQueryParameters(int $currentPage = 1): array
3227
{
3328
$parameters = $this->queryParameters;
3429
$parameters['searchCriteria[currentPage]'] = $currentPage;
3530
$parameters['searchCriteria[pageSize]'] = $this->pageSize;
3631

37-
return $parameters;
32+
$filters = array_map(fn (FilterGroup $item, int $key) => $item->compileFilters($key), $this->filters, array_keys($this->filters));
33+
34+
return array_merge($parameters, ...$filters);
3835
}
3936

4037
public function extract(): iterable

src/FilterGroup.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Kiboko\Component\Flow\Magento2;
44

5+
use phpDocumentor\Reflection\Types\This;
6+
57
class FilterGroup
68
{
79
private array $filters = [];
@@ -13,15 +15,24 @@ public function asArray(): array
1315

1416
public function withFilter(string $field, string $operator, mixed $value): self
1517
{
16-
$this->filters = [
17-
'[filters][0][field]' => $field,
18-
'[filters][0][value]' => $value,
19-
'[filters][0][condition_type]' => $operator,
18+
$this->filters[] = [
19+
'field' => $field,
20+
'value' => $value,
21+
'condition_type' => $operator,
2022
];
2123

2224
return $this;
2325
}
2426

27+
public function compileFilters(int $groupIndex = 0): array
28+
{
29+
return array_merge(...array_map(fn (array $item, int $key) => [
30+
sprintf('searchCriteria[filter_groups][%s][filters][%s][field]', $groupIndex, $key) => $item['field'],
31+
sprintf('searchCriteria[filter_groups][%s][filters][%s][value]', $groupIndex, $key) => $item['value'],
32+
sprintf('searchCriteria[filter_groups][%s][filters][%s][condition_type]', $groupIndex, $key) => $item['condition_type'],
33+
], $this->filters, array_keys($this->filters)));
34+
}
35+
2536
public function greaterThan(string $field, mixed $value): self
2637
{
2738
return $this->withFilter($field, 'gt', $value);

src/OrderExtractor.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,17 @@ public function __construct(
1818
private \Psr\Log\LoggerInterface $logger,
1919
private \Kiboko\Magento\V2_1\Client|\Kiboko\Magento\V2_2\Client|\Kiboko\Magento\V2_3\Client|\Kiboko\Magento\V2_4\Client $client,
2020
private int $pageSize = 100,
21+
private array $filters = [],
2122
) {
2223
}
2324

24-
public function withFilterGroup(FilterGroup $group): self
25-
{
26-
$this->queryParameters['searchCriteria[filter_groups]'][] = $group->asArray();
27-
28-
return $this;
29-
}
30-
3125
private function compileQueryParameters(int $currentPage = 1): array
3226
{
3327
$parameters = $this->queryParameters;
3428
$parameters['searchCriteria[currentPage]'] = $currentPage;
3529
$parameters['searchCriteria[pageSize]'] = $this->pageSize;
3630

37-
return $parameters;
31+
return array_merge($parameters, $this->filters);
3832
}
3933

4034
public function extract(): iterable

src/ProductExtractor.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,17 @@ public function __construct(
1818
private \Psr\Log\LoggerInterface $logger,
1919
private \Kiboko\Magento\V2_1\Client|\Kiboko\Magento\V2_2\Client|\Kiboko\Magento\V2_3\Client|\Kiboko\Magento\V2_4\Client $client,
2020
private int $pageSize = 100,
21+
private array $filters = [],
2122
) {
2223
}
2324

24-
public function withFilterGroup(FilterGroup $group): self
25-
{
26-
$this->queryParameters['searchCriteria[filter_groups]'][] = $group->asArray();
27-
28-
return $this;
29-
}
30-
3125
private function compileQueryParameters(int $currentPage = 1): array
3226
{
3327
$parameters = $this->queryParameters;
3428
$parameters['searchCriteria[currentPage]'] = $currentPage;
3529
$parameters['searchCriteria[pageSize]'] = $this->pageSize;
3630

37-
return $parameters;
31+
return array_merge($parameters, $this->filters);
3832
}
3933

4034
public function extract(): iterable

tests/CustomerExtractorTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tests\Kiboko\Magento\V2\Extractor;
66

77
use Kiboko\Component\Flow\Magento2\CustomerExtractor;
8+
use Kiboko\Component\Flow\Magento2\FilterGroup;
89
use Kiboko\Component\PHPUnitExtension\Assert\ExtractorAssertTrait;
910
use Kiboko\Component\PHPUnitExtension\PipelineRunner;
1011
use Kiboko\Contract\Pipeline\PipelineRunnerInterface;
@@ -25,26 +26,38 @@ public function testIsSuccessful(): void
2526
->setLastname('Doe')
2627
->setEmail('johndoe@example.com');
2728

29+
$customer2 = (new CustomerDataCustomerInterface())
30+
->setFirstname('Seb')
31+
->setLastname('Parrat')
32+
->setEmail('seb@com');
33+
2834
$client = $this->createMock(Client::class);
2935
$client
3036
->expects($this->once())
3137
->method('customerCustomerRepositoryV1GetListGet')
3238
->willReturn(
3339
(new CustomerDataCustomerSearchResultsInterface())
3440
->setItems([
35-
$customer
41+
$customer,
42+
$customer2,
3643
])
3744
->setTotalCount(1)
3845
);
3946

4047
$extractor = new CustomerExtractor(
4148
new NullLogger(),
4249
$client,
50+
1,
51+
[
52+
(new FilterGroup())->withFilter('updated_at', 'eq', '2022-09-05'),
53+
(new FilterGroup())->withFilter('active', 'eq', true),
54+
]
4355
);
4456

4557
$this->assertExtractorExtractsExactly(
4658
[
47-
$customer
59+
$customer,
60+
$customer2
4861
],
4962
$extractor
5063
);

0 commit comments

Comments
 (0)