Skip to content

Commit

Permalink
Improved the writer Management + ran php-cs-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Jun 24, 2024
1 parent a48608c commit ee91c53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
32 changes: 17 additions & 15 deletions src/Sheet/FingersCrossed/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
use Box\Spout\Writer\WriterInterface;
use Kiboko\Component\Bucket\AcceptanceResultBucket;
use Kiboko\Component\Bucket\EmptyResultBucket;
use Kiboko\Contract\Bucket\ResultBucketInterface;
use Kiboko\Contract\Pipeline\FlushableInterface;
use Kiboko\Component\Bucket\RejectionResultBucket;
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,
Expand All @@ -30,35 +29,38 @@ public function __construct(

public function load(): \Generator
{
$line = yield;
$line = yield new EmptyResultBucket();

try {
$this->writer->addRow(
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
);
} catch (WriterNotOpenedException|IOException $exception) {
} catch (IOException|WriterNotOpenedException $exception) {
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);

return;
$line = yield new RejectionResultBucket(
'Impossible to load data to the given CSV file.',
$exception,
$line
);
}

/* @phpstan-ignore-next-line */
while (true) {
try {
$this->writer->addRow(
new Row(array_map(fn ($value) => new Cell($value), $line), null)
);
} catch (WriterNotOpenedException|IOException $exception) {
} catch (IOException|WriterNotOpenedException $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);
}
}

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

return new EmptyResultBucket();
}
}
12 changes: 1 addition & 11 deletions src/Sheet/Safe/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +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\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,
Expand Down Expand Up @@ -62,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();
}
}

0 comments on commit ee91c53

Please sign in to comment.