Dbal   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCask() 0 24 5
A getEstimatedSize() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow\Provider;
5
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DBALException;
8
use SlayerBirden\DataFlow\Data\SimpleBag;
9
use SlayerBirden\DataFlow\DataBagInterface;
10
use SlayerBirden\DataFlow\IdentificationTrait;
11
use SlayerBirden\DataFlow\Provider\Exception\ProviderException;
12
use SlayerBirden\DataFlow\ProviderInterface;
13
14
class Dbal implements ProviderInterface
15
{
16
    use IdentificationTrait;
17
18
    const LIMIT = 100;
19
    /**
20
     * @var string
21
     */
22
    private $id;
23
    /**
24
     * @var Connection
25
     */
26
    private $connection;
27
    /**
28
     * @var string
29
     */
30
    private $table;
31
32 2
    public function __construct(string $id, Connection $connection, string $table)
33
    {
34 2
        $this->id = $id;
35 2
        $this->connection = $connection;
36 2
        $this->table = $table;
37 2
    }
38
39
    /**
40
     * @return \Generator|DataBagInterface[]
41
     */
42 1
    public function getCask(): \Generator
43
    {
44 1
        $offset = 0;
45 1
        $qb = $this->connection->createQueryBuilder();
46
        do {
47 1
            $qb->select('*')
48 1
                ->from($this->table)
49 1
                ->setFirstResult($offset)
50 1
                ->setMaxResults(self::LIMIT);
51
            try {
52 1
                $stmt = $this->connection->query($qb->getSQL());
53
            } catch (DBALException $e) {
54
                yield new ProviderException($e->getMessage(), $e->getCode(), $e);
55
            }
56 1
            $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
57 1
            $count = count($result);
58 1
            if ($count) {
59 1
                foreach ($result as $row) {
60 1
                    yield new SimpleBag($row);
61
                }
62 1
                $offset += self::LIMIT;
63
            }
64 1
        } while ($count > 0);
65 1
    }
66
67
    /**
68
     * @inheritdoc
69
     * @throws \Doctrine\DBAL\DBALException
70
     */
71 1
    public function getEstimatedSize(): int
72
    {
73 1
        $qb = $this->connection->createQueryBuilder();
74 1
        $qb->select('count(*)')->from($this->table);
75
76 1
        $stmt = $this->connection->query($qb->getSQL());
77 1
        return (int)$stmt->fetchColumn();
78
    }
79
}
80