Passed
Pull Request — master (#3)
by Carlos C
12:19 queued 41s
created

Repository::convertScalarNullToStringValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Database;
6
7
use PDO;
8
use PDOException;
9
use RuntimeException;
10
11
class Repository
12
{
13
    private PDO $pdo;
14
15
    public function __construct(string $dbfile)
16 13
    {
17
        if (':memory:' !== $dbfile) {
18 13
            // TODO: validate other sources ?
19
            $dbfile = '//' . $dbfile;
20
        }
21
        $this->pdo = new PDO('sqlite:' . $dbfile, '', '', [
22 13
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
23 13
        ]);
24
    }
25 13
26
    public function pdo(): PDO
27 1
    {
28
        return $this->pdo;
29 1
    }
30
31
    public function hasTable(string $table): bool
32 6
    {
33
        $sql = 'SELECT count(*) FROM sqlite_master WHERE type = :type AND name = :table;';
34 6
        return (1 === (int) $this->queryOne($sql, ['type' => 'table', 'table' => $table]));
35 6
    }
36
37
    public function getRecordCount(string $table): int
38 4
    {
39
        $sql = 'SELECT count(*) FROM ' . $this->escapeName($table) . ';';
40 4
        return (int) $this->queryOne($sql);
41 4
    }
42
43
    /**
44 13
     * @param mixed[] $values
45
     */
46 13
    public function execute(string $sql, array $values = []): void
47 13
    {
48
        $stmt = $this->pdo->prepare($sql);
49
        if (false === $stmt->execute($values)) {
50
            $errorInfo = $stmt->errorInfo();
51 13
            throw new PDOException(sprintf('[%s] %s', $errorInfo[1] ?? 'UNDEF', $errorInfo[2] ?? 'Unknown error'));
52
        }
53 1
    }
54
55 1
    /**
56 1
     * @param mixed[] $values
57
     * @return array<int, array<string, string|null>>
58
     */
59 1
    public function queryArray(string $sql, array $values = []): array
60 1
    {
61 1
        $stmt = $this->pdo->prepare($sql);
62
        if (false === $stmt->execute($values)) {
63
            throw new RuntimeException("Unable to execute $sql");
64 1
        }
65
        $table = [];
66
        while (false !== $row = $stmt->fetch(PDO::FETCH_ASSOC)) {
67 4
            /** @var array<string, scalar|null> $row */
68
            $table[] = $this->convertScalarNullToStringArray($row);
69 4
        }
70 4
71
        return $table;
72
    }
73 4
74 4
    /**
75 4
     * @param mixed[] $values
76
     * @return array<string, string|null>
77
     */
78
    public function queryRow(string $sql, array $values = []): array
79
    {
80
        $stmt = $this->pdo->prepare($sql);
81
        if (false === $stmt->execute($values)) {
82
            throw new RuntimeException("Unable to execute $sql");
83
        }
84
        /** @var false|array<string, scalar|null> $row */
85
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
86 9
        if (! is_array($row)) {
87
            return [];
88 9
        }
89 9
        return $this->convertScalarNullToStringArray($row);
90 9
    }
91
92
    /**
93 13
     * @param mixed[] $values
94
     * @return string|null
95 13
     */
96
    public function queryOne(string $sql, array $values = [])
97
    {
98
        $stmt = $this->pdo->prepare($sql);
99
        $stmt->execute($values);
100
        return $this->convertScalarNullToStringValue($stmt->fetchColumn(0));
101
    }
102
103
    public function escapeName(string $name): string
104
    {
105
        return '"' . str_replace('"', '""', $name) . '"';
106
    }
107
108
    /** @param scalar|null $value */
109
    private function convertScalarNullToStringValue($value): ?string
110
    {
111
        return (null === $value) ? null : (string) $value;
112
    }
113
114
    /**
115
     * @param array<scalar|null> $values
116
     * @return array<string|null>
117
     */
118
    private function convertScalarNullToStringArray(array $values): array
119
    {
120
        return array_map(
121
            fn ($value) => $this->convertScalarNullToStringValue($value),
122
            $values
123
        );
124
    }
125
}
126