From b2af9c71c7cbe3e970c227a8e21e2dc25fd72812 Mon Sep 17 00:00:00 2001 From: sebprt Date: Wed, 19 Jun 2024 16:55:56 +0200 Subject: [PATCH 1/3] Improved the writer Management + ran php-cs-fixer --- src/CSV/FingersCrossed/Extractor.php | 3 ++- src/CSV/FingersCrossed/Loader.php | 3 ++- src/CSV/Safe/Extractor.php | 3 ++- src/CSV/Safe/Loader.php | 3 ++- src/Sheet/FingersCrossed/Extractor.php | 3 ++- src/Sheet/FingersCrossed/Loader.php | 11 +-------- src/Sheet/Safe/Extractor.php | 3 ++- src/Sheet/Safe/Loader.php | 33 +++++--------------------- 8 files changed, 19 insertions(+), 43 deletions(-) diff --git a/src/CSV/FingersCrossed/Extractor.php b/src/CSV/FingersCrossed/Extractor.php index ecc1927..5a7a1dd 100644 --- a/src/CSV/FingersCrossed/Extractor.php +++ b/src/CSV/FingersCrossed/Extractor.php @@ -18,7 +18,8 @@ public function __construct( private ReaderInterface $reader, private int $skipLines = 0, private LoggerInterface $logger = new NullLogger() - ) {} + ) { + } public function extract(): iterable { diff --git a/src/CSV/FingersCrossed/Loader.php b/src/CSV/FingersCrossed/Loader.php index 7614357..26a1701 100644 --- a/src/CSV/FingersCrossed/Loader.php +++ b/src/CSV/FingersCrossed/Loader.php @@ -23,7 +23,8 @@ public function __construct( private WriterInterface $writer, private LoggerInterface $logger = new NullLogger() - ) {} + ) { + } public function load(): \Generator { diff --git a/src/CSV/Safe/Extractor.php b/src/CSV/Safe/Extractor.php index 0b0021b..5aca5c0 100644 --- a/src/CSV/Safe/Extractor.php +++ b/src/CSV/Safe/Extractor.php @@ -18,7 +18,8 @@ public function __construct( private ReaderInterface $reader, private int $skipLines = 0, private LoggerInterface $logger = new NullLogger() - ) {} + ) { + } public function extract(): iterable { diff --git a/src/CSV/Safe/Loader.php b/src/CSV/Safe/Loader.php index a27ed69..7263437 100644 --- a/src/CSV/Safe/Loader.php +++ b/src/CSV/Safe/Loader.php @@ -23,7 +23,8 @@ public function __construct( private WriterInterface $writer, private LoggerInterface $logger = new NullLogger() - ) {} + ) { + } public function load(): \Generator { diff --git a/src/Sheet/FingersCrossed/Extractor.php b/src/Sheet/FingersCrossed/Extractor.php index 37d643d..6ba9d0a 100644 --- a/src/Sheet/FingersCrossed/Extractor.php +++ b/src/Sheet/FingersCrossed/Extractor.php @@ -21,7 +21,8 @@ public function __construct( private string $sheetName, private int $skipLines = 0, private LoggerInterface $logger = new NullLogger() - ) {} + ) { + } public function extract(): iterable { diff --git a/src/Sheet/FingersCrossed/Loader.php b/src/Sheet/FingersCrossed/Loader.php index 2321faa..7c3c15e 100644 --- a/src/Sheet/FingersCrossed/Loader.php +++ b/src/Sheet/FingersCrossed/Loader.php @@ -12,13 +12,11 @@ use Kiboko\Component\Bucket\AcceptanceResultBucket; use Kiboko\Component\Bucket\EmptyResultBucket; use Kiboko\Component\Bucket\RejectionResultBucket; -use Kiboko\Contract\Bucket\ResultBucketInterface; -use Kiboko\Contract\Pipeline\FlushableInterface; use Kiboko\Contract\Pipeline\LoaderInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; -final readonly class Loader implements LoaderInterface, FlushableInterface +final readonly class Loader implements LoaderInterface { public function __construct( private WriterInterface $writer, @@ -65,11 +63,4 @@ public function load(): \Generator $line = yield new AcceptanceResultBucket($line); } } - - public function flush(): ResultBucketInterface - { - $this->writer->close(); - - return new EmptyResultBucket(); - } } diff --git a/src/Sheet/Safe/Extractor.php b/src/Sheet/Safe/Extractor.php index 949312c..62a9c1e 100644 --- a/src/Sheet/Safe/Extractor.php +++ b/src/Sheet/Safe/Extractor.php @@ -21,7 +21,8 @@ public function __construct( private string $sheetName, private int $skipLines = 0, private LoggerInterface $logger = new NullLogger() - ) {} + ) { + } public function extract(): iterable { diff --git a/src/Sheet/Safe/Loader.php b/src/Sheet/Safe/Loader.php index bcea776..eccf5fe 100644 --- a/src/Sheet/Safe/Loader.php +++ b/src/Sheet/Safe/Loader.php @@ -10,15 +10,11 @@ use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\WriterInterface; use Kiboko\Component\Bucket\AcceptanceResultBucket; -use Kiboko\Component\Bucket\EmptyResultBucket; -use Kiboko\Component\Bucket\RejectionResultBucket; -use Kiboko\Contract\Bucket\ResultBucketInterface; -use Kiboko\Contract\Pipeline\FlushableInterface; use Kiboko\Contract\Pipeline\LoaderInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; -final readonly class Loader implements LoaderInterface, FlushableInterface +final readonly class Loader implements LoaderInterface { public function __construct( private WriterInterface $writer, @@ -31,33 +27,23 @@ public function __construct( public function load(): \Generator { - $line = yield new EmptyResultBucket(); + $line = yield; $headers = array_keys($line); try { $this->writer->addRow( new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null) ); - } catch (IOException|WriterNotOpenedException $exception) { + } catch (WriterNotOpenedException|IOException $exception) { $this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]); - $line = yield new RejectionResultBucket( - 'Impossible to load data to the given CSV file.', - $exception, - $line - ); + + return; } - /* @phpstan-ignore-next-line */ while (true) { try { $this->writer->addRow($this->orderColumns($headers, $line)); - } catch (IOException|WriterNotOpenedException $exception) { + } catch (WriterNotOpenedException|IOException $exception) { $this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]); - $line = yield new RejectionResultBucket( - 'Impossible to load data to the given CSV file.', - $exception, - $line - ); - continue; } $line = yield new AcceptanceResultBucket($line); @@ -73,11 +59,4 @@ private function orderColumns(array $headers, array $line): Row return new Row($result, null); } - - public function flush(): ResultBucketInterface - { - $this->writer->close(); - - return new EmptyResultBucket(); - } } From 5ac15485cfe617a85c1ac1fc22ae9a559589cd08 Mon Sep 17 00:00:00 2001 From: sebprt Date: Mon, 24 Jun 2024 11:38:22 +0200 Subject: [PATCH 2/3] Removed sheet management from Loader --- src/Sheet/FingersCrossed/Loader.php | 3 --- src/Sheet/Safe/Loader.php | 3 --- 2 files changed, 6 deletions(-) diff --git a/src/Sheet/FingersCrossed/Loader.php b/src/Sheet/FingersCrossed/Loader.php index 7c3c15e..de452ff 100644 --- a/src/Sheet/FingersCrossed/Loader.php +++ b/src/Sheet/FingersCrossed/Loader.php @@ -20,11 +20,8 @@ { public function __construct( private WriterInterface $writer, - private string $sheetName, private LoggerInterface $logger = new NullLogger() ) { - /* @phpstan-ignore-next-line */ - $this->writer->getCurrentSheet()->setName($this->sheetName); } public function load(): \Generator diff --git a/src/Sheet/Safe/Loader.php b/src/Sheet/Safe/Loader.php index eccf5fe..5822fed 100644 --- a/src/Sheet/Safe/Loader.php +++ b/src/Sheet/Safe/Loader.php @@ -18,11 +18,8 @@ { public function __construct( private WriterInterface $writer, - private string $sheetName, private LoggerInterface $logger = new NullLogger() ) { - /* @phpstan-ignore-next-line */ - $this->writer->getCurrentSheet()->setName($this->sheetName); } public function load(): \Generator From fc5cb8cf444ba10ab0d5bd73d53e671187b5bb4b Mon Sep 17 00:00:00 2001 From: sebprt Date: Tue, 2 Jul 2024 12:03:14 +0200 Subject: [PATCH 3/3] Updated tests + fixed PhpStan errors --- src/Sheet/Safe/Loader.php | 4 +++- tests/functional/Sheet/FingersCrossed/ExcelLoaderTest.php | 4 +++- .../Sheet/FingersCrossed/OpenDocumentLoaderTest.php | 4 +++- tests/functional/Sheet/Safe/ExcelLoaderTest.php | 4 +++- tests/functional/Sheet/Safe/OpenDocumentLoaderTest.php | 4 +++- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Sheet/Safe/Loader.php b/src/Sheet/Safe/Loader.php index 5822fed..c863ad6 100644 --- a/src/Sheet/Safe/Loader.php +++ b/src/Sheet/Safe/Loader.php @@ -10,6 +10,7 @@ use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\WriterInterface; use Kiboko\Component\Bucket\AcceptanceResultBucket; +use Kiboko\Component\Bucket\EmptyResultBucket; use Kiboko\Contract\Pipeline\LoaderInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -24,7 +25,7 @@ public function __construct( public function load(): \Generator { - $line = yield; + $line = yield new EmptyResultBucket(); $headers = array_keys($line); try { $this->writer->addRow( @@ -36,6 +37,7 @@ public function load(): \Generator return; } + /* @phpstan-ignore-next-line */ while (true) { try { $this->writer->addRow($this->orderColumns($headers, $line)); diff --git a/tests/functional/Sheet/FingersCrossed/ExcelLoaderTest.php b/tests/functional/Sheet/FingersCrossed/ExcelLoaderTest.php index 49911a9..6369a08 100644 --- a/tests/functional/Sheet/FingersCrossed/ExcelLoaderTest.php +++ b/tests/functional/Sheet/FingersCrossed/ExcelLoaderTest.php @@ -74,9 +74,11 @@ public function load(): void 'last name' => 'dupont', ], ], - new Loader($this->writer, 'Sheet1') + new Loader($this->writer) ); + $this->writer->close(); + $this->assertRowWasWrittenToExcel( /* 'vfs://test.xlsx' */ $path, 'Sheet1', diff --git a/tests/functional/Sheet/FingersCrossed/OpenDocumentLoaderTest.php b/tests/functional/Sheet/FingersCrossed/OpenDocumentLoaderTest.php index 6794e82..16a7db9 100644 --- a/tests/functional/Sheet/FingersCrossed/OpenDocumentLoaderTest.php +++ b/tests/functional/Sheet/FingersCrossed/OpenDocumentLoaderTest.php @@ -74,9 +74,11 @@ public function load(): void 'last name' => 'dupont', ], ], - new Loader($this->writer, 'Sheet1') + new Loader($this->writer) ); + $this->writer->close(); + $this->assertRowWasWrittenToOpenDocument( /* 'vfs://test.ods' */ $path, 'Sheet1', diff --git a/tests/functional/Sheet/Safe/ExcelLoaderTest.php b/tests/functional/Sheet/Safe/ExcelLoaderTest.php index d794c4a..01255fc 100644 --- a/tests/functional/Sheet/Safe/ExcelLoaderTest.php +++ b/tests/functional/Sheet/Safe/ExcelLoaderTest.php @@ -74,9 +74,11 @@ public function load(): void 'last name' => 'dupont', ], ], - new Loader($this->writer, 'Sheet1') + new Loader($this->writer) ); + $this->writer->close(); + $this->assertRowWasWrittenToExcel( /* 'vfs://test.xlsx' */ $path, 'Sheet1', diff --git a/tests/functional/Sheet/Safe/OpenDocumentLoaderTest.php b/tests/functional/Sheet/Safe/OpenDocumentLoaderTest.php index 4b85834..ddb14d0 100644 --- a/tests/functional/Sheet/Safe/OpenDocumentLoaderTest.php +++ b/tests/functional/Sheet/Safe/OpenDocumentLoaderTest.php @@ -74,9 +74,11 @@ public function load(): void 'last name' => 'dupont', ], ], - new Loader($this->writer, 'Sheet1') + new Loader($this->writer) ); + $this->writer->close(); + $this->assertRowWasWrittenToOpenDocument( /* 'vfs://test.ods' */ $path, 'Sheet1',