Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/CSV/FingersCrossed/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function __construct(
private ReaderInterface $reader,
private int $skipLines = 0,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function extract(): iterable
{
Expand Down
3 changes: 2 additions & 1 deletion src/CSV/FingersCrossed/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
public function __construct(
private WriterInterface $writer,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function load(): \Generator
{
Expand Down
3 changes: 2 additions & 1 deletion src/CSV/Safe/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function __construct(
private ReaderInterface $reader,
private int $skipLines = 0,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function extract(): iterable
{
Expand Down
3 changes: 2 additions & 1 deletion src/CSV/Safe/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
public function __construct(
private WriterInterface $writer,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function load(): \Generator
{
Expand Down
3 changes: 2 additions & 1 deletion src/Sheet/FingersCrossed/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function __construct(
private string $sheetName,
private int $skipLines = 0,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function extract(): iterable
{
Expand Down
14 changes: 1 addition & 13 deletions src/Sheet/FingersCrossed/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@
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,
private string $sheetName,
private LoggerInterface $logger = new NullLogger()
) {
/* @phpstan-ignore-next-line */
$this->writer->getCurrentSheet()->setName($this->sheetName);
}

public function load(): \Generator
Expand Down Expand Up @@ -65,11 +60,4 @@ public function load(): \Generator
$line = yield new AcceptanceResultBucket($line);
}
}

public function flush(): ResultBucketInterface
{
$this->writer->close();

return new EmptyResultBucket();
}
}
3 changes: 2 additions & 1 deletion src/Sheet/Safe/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function __construct(
private string $sheetName,
private int $skipLines = 0,
private LoggerInterface $logger = new NullLogger()
) {}
) {
}

public function extract(): iterable
{
Expand Down
32 changes: 5 additions & 27 deletions src/Sheet/Safe/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@
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,
private string $sheetName,
private LoggerInterface $logger = new NullLogger()
) {
/* @phpstan-ignore-next-line */
$this->writer->getCurrentSheet()->setName($this->sheetName);
}

public function load(): \Generator
Expand All @@ -37,27 +31,18 @@ public function load(): \Generator
$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);
Expand All @@ -73,11 +58,4 @@ private function orderColumns(array $headers, array $line): Row

return new Row($result, null);
}

public function flush(): ResultBucketInterface
{
$this->writer->close();

return new EmptyResultBucket();
}
}
4 changes: 3 additions & 1 deletion tests/functional/Sheet/FingersCrossed/ExcelLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/Sheet/Safe/ExcelLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/Sheet/Safe/OpenDocumentLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down