Skip to content

Commit 68b0dae

Browse files
gplanchatJoMessina
authored andcommitted
Refactored the way filters are handled
1 parent 6d22048 commit 68b0dae

17 files changed

+862
-425
lines changed

.github/workflows/phpstan-7.yaml

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/workflows/phpstan-6.yaml renamed to .github/workflows/phpstan-9.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
name: PHPStan level 6
1+
name: PHPStan level 8
22
on: push
33
jobs:
4-
phpstan:
4+
phpstan-9:
55
runs-on: ubuntu-latest
66
steps:
77
- uses: actions/checkout@v3
@@ -20,4 +20,4 @@ jobs:
2020
uses: php-actions/phpstan@v3
2121
with:
2222
path: src/
23-
level: 6
23+
level: 9

.github/workflows/quality.yaml

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Quality (PHPStan lvl 4)
1+
name: Quality (PHPStan lvl 7)
22
on: push
33
jobs:
44
cs-fixer:
@@ -17,20 +17,19 @@ jobs:
1717
phpstan:
1818
runs-on: ubuntu-latest
1919
steps:
20-
- uses: actions/checkout@v3
21-
- uses: actions/cache@v3
22-
with:
23-
path: '**/vendor'
24-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
25-
restore-keys: |
26-
${{ runner.os }}-composer-
27-
- uses: php-actions/composer@v6
28-
with:
29-
args: --prefer-dist
30-
php_version: '8.2'
31-
32-
- name: PHPStan
33-
uses: php-actions/phpstan@v3
34-
with:
35-
path: src/
36-
level: 4
20+
- uses: actions/checkout@v3
21+
- uses: actions/cache@v3
22+
with:
23+
path: '**/vendor'
24+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
25+
restore-keys: |
26+
${{ runner.os }}-composer-
27+
- uses: php-actions/composer@v6
28+
with:
29+
args: --prefer-dist
30+
php_version: '8.2'
31+
- name: PHPStan
32+
uses: php-actions/phpstan@v3
33+
with:
34+
path: src/
35+
level: 7

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
parameters:
2-
level: 5
2+
level: 7
33
treatPhpDocTypesAsCertain: false

src/CategoryLookup.php

Lines changed: 126 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,155 @@
77
use Kiboko\Component\Bucket\AcceptanceResultBucket;
88
use Kiboko\Component\Bucket\EmptyResultBucket;
99
use Kiboko\Component\Bucket\RejectionResultBucket;
10+
use Kiboko\Contract\Bucket\RejectionResultBucketInterface;
1011
use Kiboko\Contract\Mapping\CompiledMapperInterface;
1112
use Kiboko\Contract\Pipeline\TransformerInterface;
12-
use Psr\SimpleCache\CacheInterface;
13+
use Kiboko\Magento\Client;
14+
use Kiboko\Magento\Exception\GetV1CategoriesCategoryIdBadRequestException;
15+
use Kiboko\Magento\Exception\UnexpectedStatusCodeException;
16+
use Kiboko\Magento\Model\CatalogDataCategoryInterface;
17+
use Kiboko\Magento\Model\ErrorResponse;
18+
use Psr\Http\Client\NetworkExceptionInterface;
19+
use Psr\Log\LoggerInterface;
1320

21+
/**
22+
* @template InputType of array
23+
* @template OutputType of InputType|array
24+
* @implements TransformerInterface<InputType, OutputType>
25+
*/
1426
final readonly class CategoryLookup implements TransformerInterface
1527
{
28+
/**
29+
* @param CompiledMapperInterface<CatalogDataCategoryInterface, InputType, OutputType> $mapper
30+
*/
1631
public function __construct(
17-
private \Psr\Log\LoggerInterface $logger,
18-
private \Kiboko\Magento\V2_1\Client|\Kiboko\Magento\V2_2\Client|\Kiboko\Magento\V2_3\Client|\Kiboko\Magento\V2_4\Client $client,
19-
private CacheInterface $cache,
20-
private string $cacheKey,
32+
private LoggerInterface $logger,
33+
private Client $client,
2134
private CompiledMapperInterface $mapper,
2235
private string $mappingField,
2336
) {
2437
}
2538

39+
/**
40+
* @param ErrorResponse $response
41+
* @return RejectionResultBucketInterface<OutputType>
42+
*/
43+
private function rejectErrorResponse(ErrorResponse $response): RejectionResultBucketInterface
44+
{
45+
$this->logger->error(
46+
$response->getMessage(),
47+
[
48+
'resource' => 'getV1CategoriesCategoryId',
49+
'method' => 'get',
50+
],
51+
);
52+
return new RejectionResultBucket($response->getMessage(), null);
53+
}
54+
55+
/**
56+
* @return RejectionResultBucketInterface<OutputType>
57+
*/
58+
private function rejectInvalidResponse(): RejectionResultBucketInterface
59+
{
60+
$this->logger->error(
61+
$message = 'The result provided by the API client does not match the expected type. The connector compilation may have fetched incompatible versions.',
62+
[
63+
'resource' => 'getV1CategoriesCategoryId',
64+
'method' => 'get',
65+
],
66+
);
67+
return new RejectionResultBucket($message, null);
68+
}
69+
70+
/**
71+
* @param InputType $line
72+
* @return OutputType
73+
*/
74+
public function passThrough(array $line): array
75+
{
76+
/** @var OutputType $line */
77+
return $line;
78+
}
79+
2680
public function transform(): \Generator
2781
{
2882
$line = yield new EmptyResultBucket();
2983
while (true) {
84+
if ($line === null) {
85+
$line = yield new EmptyResultBucket();
86+
continue;
87+
}
88+
3089
if (null === $line[$this->mappingField]) {
31-
$line = yield new AcceptanceResultBucket($line);
90+
$line = yield new AcceptanceResultBucket($this->passThrough($line));
91+
continue;
3292
}
3393

3494
try {
35-
$lookup = $this->cache->get(sprintf($this->cacheKey, $line[$this->mappingField]));
36-
37-
if (null === $lookup) {
38-
$lookup = $this->client->catalogCategoryRepositoryV1GetGet(
39-
categoryId: (int) $line[$this->mappingField],
40-
);
95+
$lookup = $this->client->getV1CategoriesCategoryId(
96+
categoryId: (int) $line[$this->mappingField],
97+
);
4198

42-
if (!$lookup instanceof \Kiboko\Magento\V2_1\Model\CatalogDataCategoryInterface
43-
&& !$lookup instanceof \Kiboko\Magento\V2_2\Model\CatalogDataCategoryInterface
44-
&& !$lookup instanceof \Kiboko\Magento\V2_3\Model\CatalogDataCategoryInterface
45-
&& !$lookup instanceof \Kiboko\Magento\V2_4\Model\CatalogDataCategoryInterface
46-
) {
47-
return;
48-
}
99+
if ($lookup instanceof ErrorResponse) {
100+
$line = yield $this->rejectErrorResponse($lookup);
101+
continue;
102+
}
49103

50-
$this->cache->set(
51-
sprintf($this->cacheKey, $line[$this->mappingField]),
52-
$lookup,
53-
);
104+
if (!$lookup instanceof CatalogDataCategoryInterface) {
105+
$line = yield $this->rejectInvalidResponse();
106+
continue;
54107
}
55-
} catch (\RuntimeException $exception) {
56-
$this->logger->warning($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
57-
$line = yield new RejectionResultBucket($line);
108+
} catch (NetworkExceptionInterface $exception) {
109+
$this->logger->critical(
110+
$exception->getMessage(),
111+
[
112+
'exception' => $exception,
113+
'resource' => 'getV1CategoriesCategoryId',
114+
'method' => 'get',
115+
'categoryId' => (int) $line[$this->mappingField],
116+
'mappingField' => $this->mappingField,
117+
],
118+
);
119+
$line = yield new RejectionResultBucket(
120+
'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.',
121+
$exception,
122+
$this->passThrough($line),
123+
);
124+
continue;
125+
} catch (GetV1CategoriesCategoryIdBadRequestException $exception) {
126+
$this->logger->error(
127+
$exception->getMessage(),
128+
[
129+
'exception' => $exception,
130+
'resource' => 'getV1CategoriesCategoryId',
131+
'method' => 'get',
132+
'categoryId' => (int) $line[$this->mappingField],
133+
'mappingField' => $this->mappingField,
134+
],
135+
);
136+
$line = yield new RejectionResultBucket(
137+
'The source API rejected our request. Ignoring line. Maybe you are requesting on incompatible versions.',
138+
$exception,
139+
$this->passThrough($line),
140+
);
58141
continue;
142+
} catch (UnexpectedStatusCodeException $exception) {
143+
$this->logger->critical(
144+
$exception->getMessage(),
145+
[
146+
'exception' => $exception,
147+
'resource' => 'getV1CategoriesCategoryId',
148+
'method' => 'get',
149+
'categoryId' => (int) $line[$this->mappingField],
150+
'mappingField' => $this->mappingField,
151+
],
152+
);
153+
$line = yield new RejectionResultBucket(
154+
'The source API responded with a status we did not expect. Aborting. Please check the availability of the source API and if there are no rate limiting or redirections active.',
155+
$exception,
156+
$this->passThrough($line),
157+
);
158+
return;
59159
}
60160

61161
$output = ($this->mapper)($lookup, $line);

0 commit comments

Comments
 (0)