Passed
Push — master ( 0e87cd...c4ac3e )
by Carlos C
12:45 queued 01:03
created

DataTableGateway::sqlInsert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 2
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogosPopulate\Database;
6
7
use LogicException;
8
use PDOException;
9
10
class DataTableGateway
11
{
12 14
    public function __construct(private readonly DataTable $dataTable, private readonly Repository $repository)
13
    {
14
    }
15
16 10
    public function dataTable(): DataTable
17
    {
18 10
        return $this->dataTable;
19
    }
20
21 11
    public function recreate(): void
22
    {
23 11
        $this->drop();
24 11
        $this->create();
25
    }
26
27 14
    private function sqlDiscoverField(DataFieldInterface $field): string
28
    {
29 14
        $sqlName = $this->repository->escapeName($field->name());
30 14
        if ($field instanceof TextDataField || $field instanceof DateDataField) {
31 14
            return $sqlName . ' text not null';
32
        }
33 5
        if ($field instanceof IntegerDataField || $field instanceof BoolDataField) {
0 ignored issues
show
Bug introduced by
The type PhpCfdi\SatCatalogosPopu...\Database\BoolDataField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34 5
            return $sqlName . ' int not null';
35
        }
36
        if ($field instanceof FloatDataField) {
37
            return $sqlName . ' real not null';
38
        }
39
40
        throw new LogicException("Don't know what to do with " . $field::class);
41
    }
42
43 11
    public function drop(): void
44
    {
45 11
        $sql = 'DROP TABLE IF EXISTS ' . $this->repository->escapeName($this->dataTable->name()) . ';';
46 11
        $this->repository->execute($sql);
47
    }
48
49 14
    public function create(): void
50
    {
51 14
        $fields = [];
52 14
        foreach ($this->dataTable->fields() as $field) {
53 14
            $fields[] = $this->sqlDiscoverField($field);
54
        }
55
56 14
        $pkDefinition = '';
57 14
        if (count($this->dataTable->primaryKey()) > 0) {
58 13
            $pkDefinition = 'PRIMARY KEY ('
59 13
                . implode(', ', array_map(
60 13
                    fn (string $input): string => $this->repository->escapeName($input),
61 13
                    $this->dataTable->primaryKey()
62
                ))
63
                . ')';
64
        }
65
66 14
        $sql = 'CREATE TABLE ' . $this->repository->escapeName($this->dataTable->name())
67 14
            . ' ( ' . implode(', ', array_filter([...$fields, ...[$pkDefinition]])) . ' )'
68
            . ';';
69 14
        $this->repository->execute($sql);
70
    }
71
72
    /** @param mixed[] $input */
73 13
    public function insert(array $input): void
74
    {
75 13
        $sql = $this->sqlInsert('INSERT INTO');
76
77
        try {
78 13
            $this->repository->execute($sql, $input);
79 1
        } catch (PDOException $exception) {
80 1
            $message = sprintf('Unable to run %s using %s', $sql, json_encode($input, JSON_THROW_ON_ERROR));
81 1
            throw new PDOException($message, 0, $exception);
82
        }
83
    }
84
85
    /** @param mixed[] $input */
86 1
    public function replace(array $input): void
87
    {
88 1
        $sql = $this->sqlInsert('REPLACE INTO');
89
90
        try {
91 1
            $this->repository->execute($sql, $input);
92
        } catch (PDOException $exception) {
93
            $message = sprintf('Unable to run %s using %s', $sql, json_encode($input, JSON_THROW_ON_ERROR));
94
            throw new PDOException($message, 0, $exception);
95
        }
96
    }
97
98 13
    private function sqlInsert(string $sqlCommand): string
99
    {
100 13
        $fieldNames = [];
101 13
        $preparedNames = [];
102 13
        foreach ($this->dataTable->fields() as $dataField) {
103 13
            $fieldNames[] = $this->repository->escapeName($dataField->name());
104 13
            $preparedNames[] = ':' . $dataField->name();
105
        }
106
107 13
        return $sqlCommand . ' ' . $this->repository->escapeName($this->dataTable->name())
108 13
            . ' (' . implode(', ', $fieldNames) . ')'
109 13
            . ' VALUES (' . implode(', ', $preparedNames) . ')'
110
            . ';';
111
    }
112
}
113