RfcLogGateway::eachByRfc()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\RfcLinc\DataGateway\Pdo;
6
7
use PDO;
8
use PDOStatement;
9
use PhpCfdi\RfcLinc\DataGateway\RfcLogGatewayInterface;
10
use PhpCfdi\RfcLinc\Domain\RfcLog;
11
use PhpCfdi\RfcLinc\Domain\VersionDate;
12
13
class RfcLogGateway extends AbstractPdoGateway implements RfcLogGatewayInterface
14
{
15 3
    public function eachByRfc(string $rfc)
16
    {
17 3
        $stmt = $this->openStatementByRfc($rfc);
18 3
        while (false !== $values = $stmt->fetch(PDO::FETCH_ASSOC)) {
19 3
            yield $this->createRfcLogFromArray($values);
20
        }
21 3
    }
22
23 1
    public function byRfc(string $rfc): array
24
    {
25 1
        $list = [];
26 1
        $stmt = $this->openStatementByRfc($rfc);
27 1
        while (false !== $values = $stmt->fetch(PDO::FETCH_ASSOC)) {
28 1
            $list[] = $this->createRfcLogFromArray($values);
29
        }
30 1
        return $list;
31
    }
32
33 5
    public function insert(RfcLog $rfcLog)
34
    {
35
        $query = 'insert into rfclogs (version, rfc, action)'
36 5
            . ' values (:version, :rfc, :action);';
37 5
        $this->executePrepared($query, [
38 5
            'version' => $rfcLog->date()->timestamp(),
39 5
            'rfc' => $rfcLog->rfc(),
40 5
            'action' => $rfcLog->action(),
41 5
        ], 'Cannot insert into rfc logs list');
42 5
    }
43
44 4
    private function openStatementByRfc(string $rfc): PDOStatement
45
    {
46
        $query = 'select version, rfc, action from rfclogs'
47 4
            . ' where (rfc = :rfc) order by version';
48 4
        return $this->executePrepared($query, ['rfc' => $rfc], 'Cannot get logs from rfc list');
49
    }
50
51 4
    private function createRfcLogFromArray(array $values): RfcLog
52
    {
53 4
        return new RfcLog(
54 4
            VersionDate::createFromTimestamp((int) $values['version']),
55 4
            (string) $values['rfc'],
56 4
            (int) $values['action']
57
        );
58
    }
59
}
60