Skip to content

Commit 6297da7

Browse files
committed
Created a nex lookup for categories
1 parent 16dbdd9 commit 6297da7

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/CategoryLookup.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kiboko\Component\Flow\Magento2;
6+
7+
use Kiboko\Component\Bucket\AcceptanceResultBucket;
8+
use Kiboko\Component\Bucket\RejectionResultBucket;
9+
use Kiboko\Contract\Mapping\CompiledMapperInterface;
10+
use Kiboko\Contract\Pipeline\TransformerInterface;
11+
use Psr\SimpleCache\CacheInterface;
12+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
13+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
14+
use Symfony\Component\Serializer\Serializer;
15+
use Symfony\Component\Serializer\SerializerInterface;
16+
17+
final class CategoryLookup implements TransformerInterface
18+
{
19+
private SerializerInterface $serializer;
20+
21+
public function __construct(
22+
private \Psr\Log\LoggerInterface $logger,
23+
private \Kiboko\Magento\V2_1\Client|\Kiboko\Magento\V2_2\Client|\Kiboko\Magento\V2_3\Client|\Kiboko\Magento\V2_4\Client $client,
24+
private CacheInterface $cache,
25+
private string $cacheKey,
26+
private CompiledMapperInterface $mapper,
27+
private string $mappingField,
28+
) {
29+
$this->serializer = new Serializer(
30+
normalizers: [
31+
new ObjectNormalizer()
32+
],
33+
encoders: [
34+
new JsonEncoder()
35+
]
36+
);
37+
}
38+
39+
public function transform(): \Generator
40+
{
41+
$line = yield;
42+
while (true) {
43+
try {
44+
$lookup = $this->cache->get(sprintf($this->cacheKey, $line[$this->mappingField]));
45+
46+
if ($lookup === null) {
47+
$lookup = $this->client->catalogCategoryRepositoryV1GetGet(
48+
categoryId: $line[$this->mappingField],
49+
);
50+
51+
if (!$lookup instanceof \Kiboko\Magento\V2_1\Model\CatalogDataCategoryInterface
52+
&& !$lookup instanceof \Kiboko\Magento\V2_2\Model\CatalogDataCategoryInterface
53+
&& !$lookup instanceof \Kiboko\Magento\V2_3\Model\CatalogDataCategoryInterface
54+
&& !$lookup instanceof \Kiboko\Magento\V2_4\Model\CatalogDataCategoryInterface
55+
) {
56+
return;
57+
}
58+
59+
$this->cache->set(
60+
sprintf($this->cacheKey, $line[$this->mappingField]),
61+
$this->serializer->serialize($lookup, null),
62+
);
63+
}
64+
} catch (\RuntimeException $exception) {
65+
$this->logger->warning($exception->getMessage(), ['exception' => $exception, 'item' => $line]);
66+
$line = yield new RejectionResultBucket($line);
67+
continue;
68+
}
69+
70+
$output = ($this->mapper)($lookup, $line);
71+
72+
$line = yield new AcceptanceResultBucket($output);
73+
}
74+
}
75+
}

src/Lookup.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,31 @@
99
use Kiboko\Contract\Mapping\CompiledMapperInterface;
1010
use Kiboko\Contract\Pipeline\TransformerInterface;
1111
use Psr\SimpleCache\CacheInterface;
12+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
13+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
14+
use Symfony\Component\Serializer\Serializer;
1215
use Symfony\Component\Serializer\SerializerInterface;
1316

1417
final class Lookup implements TransformerInterface
1518
{
19+
private SerializerInterface $serializer;
20+
1621
public function __construct(
1722
private \Psr\Log\LoggerInterface $logger,
1823
private \Kiboko\Magento\V2_1\Client|\Kiboko\Magento\V2_2\Client|\Kiboko\Magento\V2_3\Client|\Kiboko\Magento\V2_4\Client $client,
1924
private CacheInterface $cache,
2025
private string $cacheKey,
2126
private CompiledMapperInterface $mapper,
22-
private SerializerInterface $serializer,
2327
private string $mappingField,
2428
) {
29+
$this->serializer = new Serializer(
30+
normalizers: [
31+
new ObjectNormalizer()
32+
],
33+
encoders: [
34+
new JsonEncoder()
35+
]
36+
);
2537
}
2638

2739
public function transform(): \Generator
@@ -46,7 +58,7 @@ public function transform(): \Generator
4658

4759
$this->cache->set(
4860
sprintf($this->cacheKey, $line[$this->mappingField]),
49-
$lookup = $this->serializer->serialize($lookup, 'json'),
61+
$this->serializer->serialize($lookup),
5062
);
5163
}
5264
} catch (\RuntimeException $exception) {

0 commit comments

Comments
 (0)