|
| 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\Bucket\ResultBucketInterface; |
| 10 | + |
| 11 | +final class InvoiceExtractor implements \Kiboko\Contract\Pipeline\ExtractorInterface |
| 12 | +{ |
| 13 | + private array $queryParameters = [ |
| 14 | + 'searchCriteria[currentPage]' => 1, |
| 15 | + 'searchCriteria[pageSize]' => 100, |
| 16 | + ]; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + private \Psr\Log\LoggerInterface $logger, |
| 20 | + private \Kiboko\Magento\V2_1\Client|\Kiboko\Magento\V2_2\Client|\Kiboko\Magento\V2_3\Client|\Kiboko\Magento\V2_4\Client $client, |
| 21 | + private int $pageSize = 100, |
| 22 | + /** @var FilterGroup[] $filters */ |
| 23 | + private array $filters = [], |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + private function compileQueryParameters(int $currentPage = 1): array |
| 28 | + { |
| 29 | + $parameters = $this->queryParameters; |
| 30 | + $parameters['searchCriteria[currentPage]'] = $currentPage; |
| 31 | + $parameters['searchCriteria[pageSize]'] = $this->pageSize; |
| 32 | + |
| 33 | + $filters = array_map(fn (FilterGroup $item, int $key) => $item->compileFilters($key), $this->filters, array_keys($this->filters)); |
| 34 | + return array_merge($parameters, ...$filters); |
| 35 | + } |
| 36 | + |
| 37 | + public function extract(): iterable |
| 38 | + { |
| 39 | + try { |
| 40 | + $response = $this->client->salesInvoiceRepositoryV1GetListGet( |
| 41 | + queryParameters: $this->compileQueryParameters(), |
| 42 | + ); |
| 43 | + |
| 44 | + if (!$response instanceof \Kiboko\Magento\V2_1\Model\SalesDataInvoiceSearchResultInterface |
| 45 | + && !$response instanceof \Kiboko\Magento\V2_2\Model\SalesDataInvoiceSearchResultInterface |
| 46 | + && !$response instanceof \Kiboko\Magento\V2_3\Model\SalesDataInvoiceSearchResultInterface |
| 47 | + && !$response instanceof \Kiboko\Magento\V2_4\Model\SalesDataInvoiceSearchResultInterface |
| 48 | + ) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + yield $this->processResponse($response); |
| 53 | + |
| 54 | + $currentPage = 1; |
| 55 | + $pageCount = ceil($response->getTotalCount() / $this->pageSize); |
| 56 | + while ($currentPage++ < $pageCount) { |
| 57 | + $response = $this->client->salesInvoiceRepositoryV1GetListGet( |
| 58 | + queryParameters: $this->compileQueryParameters($currentPage), |
| 59 | + ); |
| 60 | + |
| 61 | + yield $this->processResponse($response); |
| 62 | + } |
| 63 | + } catch (\Exception $exception) { |
| 64 | + $this->logger->alert($exception->getMessage(), ['exception' => $exception]); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private function processResponse($response): ResultBucketInterface |
| 69 | + { |
| 70 | + if ($response instanceof \Kiboko\Magento\V2_1\Model\ErrorResponse |
| 71 | + || $response instanceof \Kiboko\Magento\V2_2\Model\ErrorResponse |
| 72 | + || $response instanceof \Kiboko\Magento\V2_3\Model\ErrorResponse |
| 73 | + || $response instanceof \Kiboko\Magento\V2_4\Model\ErrorResponse |
| 74 | + ) { |
| 75 | + return new RejectionResultBucket($response); |
| 76 | + } |
| 77 | + |
| 78 | + return new AcceptanceResultBucket(...$response->getItems()); |
| 79 | + } |
| 80 | +} |
0 commit comments