Skip to content

Commit ac41212

Browse files
committed
Improved logger management
1 parent 46a62b3 commit ac41212

File tree

8 files changed

+155
-89
lines changed

8 files changed

+155
-89
lines changed

src/CSV/FingersCrossed/Extractor.php

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Kiboko\Component\Flow\Spreadsheet\CSV\FingersCrossed;
66

77
use Box\Spout\Common\Entity\Row;
8+
use Box\Spout\Reader\Exception\ReaderNotOpenedException;
89
use Box\Spout\Reader\ReaderInterface;
910
use Kiboko\Component\Bucket\AcceptanceResultBucket;
1011
use Kiboko\Contract\Pipeline\ExtractorInterface;
@@ -22,39 +23,42 @@ public function __construct(
2223

2324
public function extract(): iterable
2425
{
25-
$sheet = $this->reader->getSheetIterator();
26-
27-
$currentLine = $this->skipLines + 1;
28-
29-
$columns = [];
30-
$columnCount = 0;
31-
$cellCount = 0;
32-
33-
/**
34-
* @var int $rowIndex
35-
* @var Row $row
36-
*/
37-
foreach ($sheet->current()->getRowIterator() as $rowIndex => $row) {
38-
if ($rowIndex === $currentLine) {
39-
$columns = $row->toArray();
40-
$columnCount = \count($columns);
41-
}
26+
try {
27+
$sheet = $this->reader->getSheetIterator();
28+
$currentLine = $this->skipLines + 1;
4229

43-
if ($rowIndex > $currentLine) {
44-
$line = $row->toArray();
45-
$cellCount = \count($line);
46-
}
30+
$columns = [];
31+
$columnCount = 0;
32+
$cellCount = 0;
4733

48-
if (empty($line)) {
49-
continue;
50-
}
51-
if ($cellCount > $columnCount) {
52-
$line = \array_slice($line, 0, $columnCount, true);
53-
} elseif ($cellCount < $columnCount) {
54-
$line = array_pad($line, $columnCount - $cellCount, null);
55-
}
34+
/**
35+
* @var int $rowIndex
36+
* @var Row $row
37+
*/
38+
foreach ($sheet->current()->getRowIterator() as $rowIndex => $row) {
39+
if ($rowIndex === $currentLine) {
40+
$columns = $row->toArray();
41+
$columnCount = \count($columns);
42+
}
5643

57-
yield new AcceptanceResultBucket(array_combine($columns, $line));
44+
if ($rowIndex > $currentLine) {
45+
$line = $row->toArray();
46+
$cellCount = \count($line);
47+
}
48+
49+
if (empty($line)) {
50+
continue;
51+
}
52+
if ($cellCount > $columnCount) {
53+
$line = \array_slice($line, 0, $columnCount, true);
54+
} elseif ($cellCount < $columnCount) {
55+
$line = array_pad($line, $columnCount - $cellCount, null);
56+
}
57+
58+
yield new AcceptanceResultBucket(array_combine($columns, $line));
59+
}
60+
} catch (ReaderNotOpenedException $exception) {
61+
$this->logger->error('Impossible to extract data from the given CSV file.', ['message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
5862
}
5963
}
6064
}

src/CSV/FingersCrossed/Loader.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Box\Spout\Common\Entity\Cell;
88
use Box\Spout\Common\Entity\Row;
9+
use Box\Spout\Common\Exception\IOException;
10+
use Box\Spout\Writer\Exception\WriterNotOpenedException;
911
use Box\Spout\Writer\WriterInterface;
1012
use Kiboko\Component\Bucket\AcceptanceResultBucket;
1113
use Kiboko\Component\Bucket\EmptyResultBucket;
@@ -26,14 +28,23 @@ public function __construct(
2628
public function load(): \Generator
2729
{
2830
$line = yield;
29-
$this->writer->addRow(
30-
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
31-
);
32-
33-
while (true) {
31+
try {
3432
$this->writer->addRow(
35-
new Row(array_map(fn ($value) => new Cell($value), $line), null)
33+
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
3634
);
35+
} catch (WriterNotOpenedException|IOException $exception) {
36+
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
37+
return;
38+
}
39+
40+
while (true) {
41+
try {
42+
$this->writer->addRow(
43+
new Row(array_map(fn ($value) => new Cell($value), $line), null)
44+
);
45+
} catch (WriterNotOpenedException|IOException $exception) {
46+
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
47+
}
3748

3849
$line = yield new AcceptanceResultBucket($line);
3950
}

src/CSV/Safe/Extractor.php

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Kiboko\Component\Flow\Spreadsheet\CSV\Safe;
66

77
use Box\Spout\Common\Entity\Row;
8+
use Box\Spout\Reader\Exception\ReaderNotOpenedException;
89
use Box\Spout\Reader\ReaderInterface;
910
use Kiboko\Component\Bucket\AcceptanceResultBucket;
1011
use Kiboko\Contract\Pipeline\ExtractorInterface;
@@ -22,40 +23,43 @@ public function __construct(
2223

2324
public function extract(): iterable
2425
{
25-
$sheet = $this->reader->getSheetIterator();
26-
27-
$currentLine = $this->skipLines + 1;
28-
29-
$columns = [];
30-
$columnCount = 0;
31-
$cellCount = 0;
32-
33-
/**
34-
* @var int $rowIndex
35-
* @var Row $row
36-
*/
37-
foreach ($sheet->current()->getRowIterator() as $rowIndex => $row) {
38-
if ($rowIndex === $currentLine) {
39-
$columns = $row->toArray();
40-
$columnCount = \count($columns);
41-
}
26+
try {
27+
$sheet = $this->reader->getSheetIterator();
28+
$currentLine = $this->skipLines + 1;
4229

43-
if ($rowIndex > $currentLine) {
44-
$line = $row->toArray();
45-
$cellCount = \count($row->getCells());
46-
}
30+
$columns = [];
31+
$columnCount = 0;
32+
$cellCount = 0;
4733

48-
if (empty($line)) {
49-
continue;
50-
}
51-
if ($cellCount > $columnCount) {
52-
throw new \RuntimeException(strtr('The line %line% contains too much values: found %actual% values, was expecting %expected% values.', ['%line%' => $currentLine, '%expected%' => $columnCount, '%actual%' => $cellCount]));
53-
}
54-
if ($cellCount < $columnCount) {
55-
throw new \RuntimeException(strtr('The line %line% does not contain the proper values count: found %actual% values, was expecting %expected% values.', ['%line%' => $currentLine, '%expected%' => $columnCount, '%actual%' => $cellCount]));
56-
}
34+
/**
35+
* @var int $rowIndex
36+
* @var Row $row
37+
*/
38+
foreach ($sheet->current()->getRowIterator() as $rowIndex => $row) {
39+
if ($rowIndex === $currentLine) {
40+
$columns = $row->toArray();
41+
$columnCount = \count($columns);
42+
}
43+
44+
if ($rowIndex > $currentLine) {
45+
$line = $row->toArray();
46+
$cellCount = \count($row->getCells());
47+
}
5748

58-
yield new AcceptanceResultBucket(array_combine($columns, $line));
49+
if (empty($line)) {
50+
continue;
51+
}
52+
if ($cellCount > $columnCount) {
53+
throw new \RuntimeException(strtr('The line %line% contains too much values: found %actual% values, was expecting %expected% values.', ['%line%' => $currentLine, '%expected%' => $columnCount, '%actual%' => $cellCount]));
54+
}
55+
if ($cellCount < $columnCount) {
56+
throw new \RuntimeException(strtr('The line %line% does not contain the proper values count: found %actual% values, was expecting %expected% values.', ['%line%' => $currentLine, '%expected%' => $columnCount, '%actual%' => $cellCount]));
57+
}
58+
59+
yield new AcceptanceResultBucket(array_combine($columns, $line));
60+
}
61+
} catch (ReaderNotOpenedException $exception) {
62+
$this->logger->error('Impossible to extract data from the given CSV file.', ['message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
5963
}
6064
}
6165
}

src/CSV/Safe/Loader.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Box\Spout\Common\Entity\Cell;
88
use Box\Spout\Common\Entity\Row;
9+
use Box\Spout\Common\Exception\IOException;
10+
use Box\Spout\Writer\Exception\WriterNotOpenedException;
911
use Box\Spout\Writer\WriterInterface;
1012
use Kiboko\Component\Bucket\AcceptanceResultBucket;
1113
use Kiboko\Component\Bucket\EmptyResultBucket;
@@ -27,12 +29,21 @@ public function load(): \Generator
2729
{
2830
$line = yield;
2931
$headers = array_keys($line);
30-
$this->writer->addRow(
31-
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
32-
);
32+
try {
33+
$this->writer->addRow(
34+
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
35+
);
36+
} catch (WriterNotOpenedException|IOException $exception) {
37+
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
38+
return;
39+
}
3340

3441
while (true) {
35-
$this->writer->addRow($this->orderColumns($headers, $line));
42+
try {
43+
$this->writer->addRow($this->orderColumns($headers, $line));
44+
} catch (WriterNotOpenedException|IOException $exception) {
45+
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
46+
}
3647

3748
$line = yield new AcceptanceResultBucket($line);
3849
}

src/Sheet/FingersCrossed/Extractor.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Kiboko\Component\Flow\Spreadsheet\Sheet\FingersCrossed;
66

7+
use Box\Spout\Reader\Exception\ReaderNotOpenedException;
78
use Box\Spout\Reader\ReaderInterface;
89
use Box\Spout\Reader\SheetInterface;
910
use Kiboko\Component\Bucket\AcceptanceResultBucket;
@@ -68,12 +69,18 @@ public function flush(): ResultBucketInterface
6869

6970
private function findSheet(string $name): SheetInterface
7071
{
71-
foreach ($this->reader->getSheetIterator() as $sheet) {
72-
if ($sheet->getName() === $name) {
73-
return $sheet;
72+
try {
73+
$iterator = $this->reader->getSheetIterator();
74+
75+
foreach ($iterator as $sheet) {
76+
if ($sheet->getName() === $name) {
77+
return $sheet;
78+
}
7479
}
80+
} catch (ReaderNotOpenedException $exception) {
81+
$this->logger->error('Impossible to extract data from the given Spreadsheet file.', ['message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
7582
}
7683

77-
throw new \OutOfBoundsException('No sheet with the name %name% can be found.', ['%name%' => $name]);
84+
throw new \OutOfBoundsException(strtr('No sheet with the name %name% can be found.', ['%name%' => $name]));
7885
}
7986
}

src/Sheet/FingersCrossed/Loader.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Box\Spout\Common\Entity\Cell;
88
use Box\Spout\Common\Entity\Row;
9+
use Box\Spout\Common\Exception\IOException;
10+
use Box\Spout\Writer\Exception\WriterNotOpenedException;
911
use Box\Spout\Writer\WriterInterface;
1012
use Kiboko\Component\Bucket\AcceptanceResultBucket;
1113
use Kiboko\Component\Bucket\EmptyResultBucket;
@@ -30,14 +32,23 @@ public function load(): \Generator
3032
{
3133
$line = yield;
3234

33-
$this->writer->addRow(
34-
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
35-
);
36-
37-
while (true) {
35+
try {
3836
$this->writer->addRow(
39-
new Row(array_map(fn ($value) => new Cell($value), $line), null)
37+
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
4038
);
39+
} catch (WriterNotOpenedException|IOException $exception) {
40+
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
41+
return;
42+
}
43+
44+
while (true) {
45+
try {
46+
$this->writer->addRow(
47+
new Row(array_map(fn ($value) => new Cell($value), $line), null)
48+
);
49+
} catch (WriterNotOpenedException|IOException $exception) {
50+
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
51+
}
4152

4253
$line = yield new AcceptanceResultBucket($line);
4354
}

src/Sheet/Safe/Extractor.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Kiboko\Component\Flow\Spreadsheet\Sheet\Safe;
66

7+
use Box\Spout\Reader\Exception\ReaderNotOpenedException;
78
use Box\Spout\Reader\ReaderInterface;
89
use Box\Spout\Reader\SheetInterface;
910
use Kiboko\Component\Bucket\AcceptanceResultBucket;
@@ -69,12 +70,18 @@ public function flush(): ResultBucketInterface
6970

7071
private function findSheet(string $name): SheetInterface
7172
{
72-
foreach ($this->reader->getSheetIterator() as $sheet) {
73-
if ($sheet->getName() === $name) {
74-
return $sheet;
73+
try {
74+
$iterator = $this->reader->getSheetIterator();
75+
76+
foreach ($iterator as $sheet) {
77+
if ($sheet->getName() === $name) {
78+
return $sheet;
79+
}
7580
}
81+
} catch (ReaderNotOpenedException $exception) {
82+
$this->logger->error('Impossible to extract data from the given Spreadsheet file.', ['message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
7683
}
7784

78-
throw new \OutOfBoundsException('No sheet with the name %name% can be found.', ['%name%' => $name]);
85+
throw new \OutOfBoundsException(strtr('No sheet with the name %name% can be found.', ['%name%' => $name]));
7986
}
8087
}

src/Sheet/Safe/Loader.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Box\Spout\Common\Entity\Cell;
88
use Box\Spout\Common\Entity\Row;
9+
use Box\Spout\Common\Exception\IOException;
10+
use Box\Spout\Writer\Exception\WriterNotOpenedException;
911
use Box\Spout\Writer\WriterInterface;
1012
use Kiboko\Component\Bucket\AcceptanceResultBucket;
1113
use Kiboko\Component\Bucket\EmptyResultBucket;
@@ -30,12 +32,21 @@ public function load(): \Generator
3032
{
3133
$line = yield;
3234
$headers = array_keys($line);
33-
$this->writer->addRow(
34-
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
35-
);
35+
try {
36+
$this->writer->addRow(
37+
new Row(array_map(fn ($value) => new Cell($value), array_keys($line)), null)
38+
);
39+
} catch (WriterNotOpenedException|IOException $exception) {
40+
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
41+
return;
42+
}
3643

3744
while (true) {
38-
$this->writer->addRow($this->orderColumns($headers, $line));
45+
try {
46+
$this->writer->addRow($this->orderColumns($headers, $line));
47+
} catch (WriterNotOpenedException|IOException $exception) {
48+
$this->logger->error('Impossible to load data to the given CSV file.', ['line' => $line, 'message' => $exception->getMessage(), 'previous' => $exception->getPrevious()]);
49+
}
3950

4051
$line = yield new AcceptanceResultBucket($line);
4152
}

0 commit comments

Comments
 (0)