Skip to content

Commit d09d044

Browse files
author
Oleg
committed
introduce notion of provider error
1 parent a9cb826 commit d09d044

11 files changed

+56
-29
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"require": {
1212
"php": "^7.1",
1313
"doctrine/dbal": "^2.5",
14-
"ext-json": "*"
14+
"ext-json": "*",
15+
"ext-pdo": "*"
1516
},
1617
"require-dev": {
1718
"phpunit/phpunit": "^7.0",

src/Plumber.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace SlayerBirden\DataFlow;
55

66
use SlayerBirden\DataFlow\Exception\FlowTerminationException;
7+
use SlayerBirden\DataFlow\Provider\Exception\ProviderExceptionInterface;
78

89
class Plumber
910
{
@@ -33,17 +34,21 @@ public function __construct(ProviderInterface $source, PipeLineInterface $pipeLi
3334
public function pour(): void
3435
{
3536
$provider = $this->source->getCask();
36-
foreach ($provider as $dataBag) {
37-
try {
38-
$this->pipeLine->rewind();
39-
while ($this->pipeLine->valid()) {
40-
$handler = $this->pipeLine->current();
41-
$dataBag = $handler->pass($dataBag);
42-
$this->pipeLine->next();
37+
try {
38+
foreach ($provider as $dataBag) {
39+
try {
40+
$this->pipeLine->rewind();
41+
while ($this->pipeLine->valid()) {
42+
$handler = $this->pipeLine->current();
43+
$dataBag = $handler->pass($dataBag);
44+
$this->pipeLine->next();
45+
}
46+
} catch (FlowTerminationException $exception) {
47+
$this->emitter->emit('valve_closed', $exception->getIdentifier(), $dataBag);
4348
}
44-
} catch (FlowTerminationException $exception) {
45-
$this->emitter->emit('valve_closed', $exception->getIdentifier(), $dataBag);
4649
}
50+
} catch (ProviderExceptionInterface $exception) {
51+
$this->emitter->emit('provider_error', $exception->getMessage());
4752
}
4853
$this->emitter->emit('empty_cask');
4954
}

src/Provider/ArrayProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private function validate(array $data)
3838
$localKeys = array_keys($row);
3939
sort($localKeys);
4040
if (isset($keys)) {
41-
if ($keys != $localKeys) {
41+
if ($keys !== $localKeys) {
4242
throw new InvalidDataException(
4343
sprintf('Row #(%s) has different structure than the 1st element.', $key)
4444
);

src/Provider/Csv.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use SlayerBirden\DataFlow\Data\SimpleBag;
77
use SlayerBirden\DataFlow\IdentificationTrait;
88
use SlayerBirden\DataFlow\Provider\Exception\FileDoesNotExist;
9-
use SlayerBirden\DataFlow\Provider\Exception\HeaderInvalid;
9+
use SlayerBirden\DataFlow\Provider\Exception\RowInvalid;
1010
use SlayerBirden\DataFlow\Provider\Exception\HeaderMissing;
1111
use SlayerBirden\DataFlow\ProviderInterface;
1212

@@ -59,7 +59,7 @@ public function __construct(string $id, string $fileName, bool $headerRow = true
5959

6060
/**
6161
* @inheritdoc
62-
* @throws HeaderInvalid
62+
* @throws RowInvalid
6363
*/
6464
public function getCask(): \Generator
6565
{
@@ -75,11 +75,11 @@ public function getCask(): \Generator
7575
while ($this->file->valid()) {
7676
$row = $this->file->current();
7777
if (count($row) !== count($this->header)) {
78-
throw new HeaderInvalid(
78+
throw new RowInvalid(
7979
sprintf(
80-
'Invalid header %s for row %s. Column count mismatch.',
81-
json_encode($this->header),
82-
json_encode($row)
80+
'Invalid row %s for header %s. Column count mismatch.',
81+
json_encode($row),
82+
json_encode($this->header)
8383
)
8484
);
8585
}
@@ -99,7 +99,11 @@ public function getEstimatedSize(): int
9999
// attempt to reach max (will reach last line);
100100
$prevPos = $this->file->key();
101101
$this->file->seek(PHP_INT_MAX);
102-
$numberOfLines = $this->file->key() + 1;
102+
$numberOfLines = $this->file->key();
103+
if ($this->file->valid()) {
104+
// this line is not eof
105+
$numberOfLines += 1;
106+
}
103107
if ($this->headerRow) {
104108
// subtract header row if it's present in the file
105109
--$numberOfLines;

src/Provider/Dbal.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
namespace SlayerBirden\DataFlow\Provider;
55

66
use Doctrine\DBAL\Connection;
7+
use Doctrine\DBAL\DBALException;
78
use SlayerBirden\DataFlow\Data\SimpleBag;
89
use SlayerBirden\DataFlow\DataBagInterface;
910
use SlayerBirden\DataFlow\IdentificationTrait;
11+
use SlayerBirden\DataFlow\Provider\Exception\ProviderException;
1012
use SlayerBirden\DataFlow\ProviderInterface;
1113

1214
class Dbal implements ProviderInterface
@@ -36,7 +38,6 @@ public function __construct(string $id, Connection $connection, string $table)
3638

3739
/**
3840
* @return \Generator|DataBagInterface[]
39-
* @throws \Doctrine\DBAL\DBALException
4041
*/
4142
public function getCask(): \Generator
4243
{
@@ -47,7 +48,11 @@ public function getCask(): \Generator
4748
->from($this->table)
4849
->setFirstResult($offset)
4950
->setMaxResults(self::LIMIT);
50-
$stmt = $this->connection->query($qb->getSQL());
51+
try {
52+
$stmt = $this->connection->query($qb->getSQL());
53+
} catch (DBALException $e) {
54+
throw new ProviderException($e->getMessage(), $e->getCode(), $e);
55+
}
5156
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
5257
$count = count($result);
5358
if ($count) {
@@ -61,6 +66,7 @@ public function getCask(): \Generator
6166

6267
/**
6368
* @inheritdoc
69+
* @throws \Doctrine\DBAL\DBALException
6470
*/
6571
public function getEstimatedSize(): int
6672
{

src/Provider/Exception/FileDoesNotExist.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace SlayerBirden\DataFlow\Provider\Exception;
55

6-
use SlayerBirden\DataFlow\Exception\DomainExceptionInterface;
7-
8-
class FileDoesNotExist extends \InvalidArgumentException implements DomainExceptionInterface
6+
class FileDoesNotExist extends \InvalidArgumentException implements ProviderExceptionInterface
97
{
108
}

src/Provider/Exception/HeaderMissing.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace SlayerBirden\DataFlow\Provider\Exception;
55

6-
use SlayerBirden\DataFlow\Exception\DomainExceptionInterface;
7-
8-
class HeaderMissing extends \LogicException implements DomainExceptionInterface
6+
class HeaderMissing extends \RuntimeException implements ProviderExceptionInterface
97
{
108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SlayerBirden\DataFlow\Provider\Exception;
5+
6+
class ProviderException extends \LogicException implements ProviderExceptionInterface
7+
{
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
2-
declare(strict_types=1);
32

43
namespace SlayerBirden\DataFlow\Provider\Exception;
54

65
use SlayerBirden\DataFlow\Exception\DomainExceptionInterface;
76

8-
class HeaderInvalid extends \LogicException implements DomainExceptionInterface
7+
interface ProviderExceptionInterface extends DomainExceptionInterface
98
{
109
}

src/Provider/Exception/RowInvalid.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SlayerBirden\DataFlow\Provider\Exception;
5+
6+
class RowInvalid extends \RuntimeException implements ProviderExceptionInterface
7+
{
8+
}

tests/unit/Provider/CsvTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testGetCask()
8282
}
8383

8484
/**
85-
* @expectedException \SlayerBirden\DataFlow\Provider\Exception\HeaderInvalid
85+
* @expectedException \SlayerBirden\DataFlow\Provider\Exception\RowInvalid
8686
*/
8787
public function testInvalidHeader()
8888
{

0 commit comments

Comments
 (0)