|
7 | 7 | use Kiboko\Component\Bucket\AcceptanceResultBucket;
|
8 | 8 | use Kiboko\Component\Bucket\EmptyResultBucket;
|
9 | 9 | use Kiboko\Component\Bucket\RejectionResultBucket;
|
| 10 | +use Kiboko\Contract\Bucket\RejectionResultBucketInterface; |
10 | 11 | use Kiboko\Contract\Mapping\CompiledMapperInterface;
|
11 | 12 | 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; |
13 | 20 |
|
| 21 | +/** |
| 22 | + * @template InputType of array |
| 23 | + * @template OutputType of InputType|array |
| 24 | + * @implements TransformerInterface<InputType, OutputType> |
| 25 | + */ |
14 | 26 | final readonly class CategoryLookup implements TransformerInterface
|
15 | 27 | {
|
| 28 | + /** |
| 29 | + * @param CompiledMapperInterface<CatalogDataCategoryInterface, InputType, OutputType> $mapper |
| 30 | + */ |
16 | 31 | 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, |
21 | 34 | private CompiledMapperInterface $mapper,
|
22 | 35 | private string $mappingField,
|
23 | 36 | ) {
|
24 | 37 | }
|
25 | 38 |
|
| 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 | + |
26 | 80 | public function transform(): \Generator
|
27 | 81 | {
|
28 | 82 | $line = yield new EmptyResultBucket();
|
29 | 83 | while (true) {
|
| 84 | + if ($line === null) { |
| 85 | + $line = yield new EmptyResultBucket(); |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
30 | 89 | if (null === $line[$this->mappingField]) {
|
31 |
| - $line = yield new AcceptanceResultBucket($line); |
| 90 | + $line = yield new AcceptanceResultBucket($this->passThrough($line)); |
| 91 | + continue; |
32 | 92 | }
|
33 | 93 |
|
34 | 94 | 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 | + ); |
41 | 98 |
|
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 | + } |
49 | 103 |
|
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; |
54 | 107 | }
|
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 | + ); |
58 | 141 | 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; |
59 | 159 | }
|
60 | 160 |
|
61 | 161 | $output = ($this->mapper)($lookup, $line);
|
|
0 commit comments