Skip to content

Commit

Permalink
Fixed design problem : add rejection buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Nov 22, 2023
1 parent 55be771 commit 192f6b6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
17 changes: 13 additions & 4 deletions src/CSV/FingersCrossed/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand All @@ -26,24 +27,32 @@ public function __construct(

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

Check failure on line 30 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

Generator expects value type Kiboko\Contract\Bucket\ResultBucketInterface<mixed>, null given.

Check failure on line 30 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

Generator expects value type Kiboko\Contract\Bucket\ResultBucketInterface<mixed>, null given.

Check failure on line 30 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

Generator expects value type Kiboko\Contract\Bucket\ResultBucketInterface<mixed>, null given.

Check failure on line 30 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

Generator expects value type Kiboko\Contract\Bucket\ResultBucketInterface<mixed>, null given.

Check failure on line 30 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

Generator expects value type Kiboko\Contract\Bucket\ResultBucketInterface<mixed>, null given.
try {
$this->writer->addRow(
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
);
} 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
);
}

while ($line) {
while (true) {

Check failure on line 44 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

While loop condition is always true.

Check failure on line 44 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

While loop condition is always true.

Check failure on line 44 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

While loop condition is always true.

Check failure on line 44 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

While loop condition is always true.

Check failure on line 44 in src/CSV/FingersCrossed/Loader.php

View workflow job for this annotation

GitHub Actions / phpstan

While loop condition is always true.
try {
$this->writer->addRow(
new Row(array_map(fn ($value) => new Cell($value), $line), null)
);
} 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
);
}

$line = yield new AcceptanceResultBucket($line);
Expand Down
13 changes: 11 additions & 2 deletions src/CSV/Safe/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand All @@ -34,15 +35,23 @@ public function load(): \Generator
);
} 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
);
}

while ($line) {
try {
$this->writer->addRow($this->orderColumns($headers, $line));
} 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
);
}

$line = yield new AcceptanceResultBucket($line);
Expand Down
13 changes: 11 additions & 2 deletions src/Sheet/FingersCrossed/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -38,8 +39,11 @@ public function load(): \Generator
);
} 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
);
}

while ($line) {
Expand All @@ -49,6 +53,11 @@ public function load(): \Generator
);
} 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
);
}

$line = yield new AcceptanceResultBucket($line);
Expand Down
13 changes: 11 additions & 2 deletions src/Sheet/Safe/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -38,15 +39,23 @@ public function load(): \Generator
);
} 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
);
}

while ($line) {
try {
$this->writer->addRow($this->orderColumns($headers, $line));
} 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
);
}

$line = yield new AcceptanceResultBucket($line);
Expand Down

0 comments on commit 192f6b6

Please sign in to comment.