|
11 | 11 | use Box\Spout\Writer\WriterInterface; |
12 | 12 | use Kiboko\Component\Bucket\AcceptanceResultBucket; |
13 | 13 | use Kiboko\Component\Bucket\EmptyResultBucket; |
| 14 | +use Kiboko\Component\Bucket\RejectionResultBucket; |
14 | 15 | use Kiboko\Contract\Bucket\ResultBucketInterface; |
15 | 16 | use Kiboko\Contract\Pipeline\FlushableInterface; |
16 | 17 | use Kiboko\Contract\Pipeline\LoaderInterface; |
|
22 | 23 | public function __construct( |
23 | 24 | private WriterInterface $writer, |
24 | 25 | private LoggerInterface $logger = new NullLogger() |
25 | | - ) { |
26 | | - } |
| 26 | + ) {} |
27 | 27 |
|
28 | 28 | public function load(): \Generator |
29 | 29 | { |
30 | | - $line = yield; |
| 30 | + $line = yield new EmptyResultBucket(); |
31 | 31 | try { |
32 | 32 | $this->writer->addRow( |
33 | 33 | new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null) |
34 | 34 | ); |
35 | | - } catch (WriterNotOpenedException|IOException $exception) { |
| 35 | + } catch (IOException|WriterNotOpenedException $exception) { |
36 | 36 | $this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]); |
37 | | - |
38 | | - return; |
| 37 | + $line = yield new RejectionResultBucket( |
| 38 | + 'Impossible to load data to the given CSV file.', |
| 39 | + $exception, |
| 40 | + $line |
| 41 | + ); |
39 | 42 | } |
40 | 43 |
|
| 44 | + /* @phpstan-ignore-next-line */ |
41 | 45 | while (true) { |
42 | 46 | try { |
43 | 47 | $this->writer->addRow( |
44 | 48 | new Row(array_map(fn ($value) => new Cell($value), $line), null) |
45 | 49 | ); |
46 | | - } catch (WriterNotOpenedException|IOException $exception) { |
| 50 | + } catch (IOException|WriterNotOpenedException $exception) { |
47 | 51 | $this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]); |
| 52 | + $line = yield new RejectionResultBucket( |
| 53 | + 'Impossible to load data to the given CSV file.', |
| 54 | + $exception, |
| 55 | + $line |
| 56 | + ); |
| 57 | + continue; |
48 | 58 | } |
49 | 59 |
|
50 | 60 | $line = yield new AcceptanceResultBucket($line); |
|
0 commit comments