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 19, 2024
1 parent 9a70f6d commit b2af9c7
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 43 deletions.
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
11 changes: 1 addition & 10 deletions src/Sheet/FingersCrossed/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -65,11 +63,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
33 changes: 6 additions & 27 deletions src/Sheet/Safe/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,33 +27,23 @@ public function __construct(

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

Check failure on line 30 in src/Sheet/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

Generator expects value type Kiboko\Contract\Bucket\ResultBucketInterface<mixed>, null given.
$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) {

Check failure on line 42 in src/Sheet/Safe/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

While loop condition is always 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 +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 b2af9c7

Please sign in to comment.