Completed
Push — master ( 05e43f...4b7a63 )
by Midori
01:37
created

Database::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace midorikocak\nanodb;
6
7
use InvalidArgumentException;
8
use midorikocak\querymaker\QueryInterface;
9
use PDO;
10
use PDOStatement;
11
12
class Database implements DatabaseInterface
13
{
14
    private PDO $db;
15
    private ?QueryInterface $query = null;
16
    private ?PDOStatement $statement = null;
17
18 13
    public function __construct(PDO $db, QueryInterface $query)
19
    {
20 13
        $this->db = $db;
21 13
        $this->query = $query;
22 13
    }
23
24
    public function query(QueryInterface $query): self
25
    {
26
        $this->reset();
27
        $this->query = $query;
28
        return $this;
29
    }
30
31 11
    public function select($table, array $columns = ['*']): self
32
    {
33 11
        $this->reset();
34 11
        $this->query->select($table, $columns);
0 ignored issues
show
Bug introduced by
The method select() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $this->query->/** @scrutinizer ignore-call */ 
35
                      select($table, $columns);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35 11
        return $this;
36
    }
37
38 2
    public function delete($table): self
39
    {
40 2
        $this->reset();
41 2
        $this->query->delete($table);
42 2
        return $this;
43
    }
44
45 2
    public function update($table, array $values): self
46
    {
47 2
        $this->reset();
48 2
        $this->query->update($table, $values);
49 2
        return $this;
50
    }
51
52 2
    public function insert($table, array $values): self
53
    {
54 2
        $this->reset();
55 2
        $this->query->insert($table, $values);
56 2
        return $this;
57
    }
58
59 7
    public function where($key, $value, string $operator = '='): self
60
    {
61 7
        $this->query->where($key, $value, $operator);
62 7
        return $this;
63
    }
64
65
    public function and($key, $value, string $operator = '='): self
66
    {
67
        $this->query->and($key, $value, $operator);
68
        return $this;
69
    }
70
71 1
    public function orderBy($key, $order = 'ASC'): self
72
    {
73 1
        if ($order !== 'DESC' && $order !== 'ASC') {
74
            throw new InvalidArgumentException('Invalid order value');
75
        }
76
77 1
        $this->query->orderBy($key, $order);
78 1
        return $this;
79
    }
80
81 1
    public function limit(int $limit): self
82
    {
83 1
        $this->query->limit($limit);
84 1
        return $this;
85
    }
86
87 1
    public function offset(int $offset): self
88
    {
89 1
        $this->query->offset($offset);
90 1
        return $this;
91
    }
92
93
    public function or($key, $value, string $operator = '='): self
94
    {
95
        $this->query->or($key, $value, $operator);
96
        return $this;
97
    }
98
99
    public function between($key, $before, $after): self
100
    {
101
        $this->query->between($key, $before, $after);
102
        return $this;
103
    }
104
105 4
    public function lastInsertId(): string
106
    {
107 4
        return $this->db->lastInsertId();
108
    }
109
110 3
    public function rowCount(): int
111
    {
112 3
        return $this->statement->rowCount();
0 ignored issues
show
Bug introduced by
The method rowCount() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        return $this->statement->/** @scrutinizer ignore-call */ rowCount();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
    }
114
115 13
    public function execute(): bool
116
    {
117 13
        $this->statement = $this->db->prepare($this->query->getStatement());
118 13
        return $this->statement->execute($this->query->getParams());
119
    }
120
121 9
    public function fetch(): array
122
    {
123 9
        $this->execute();
124 9
        $toReturn = $this->statement->fetch(PDO::FETCH_ASSOC);
125 9
        return $toReturn === false ? [] : $toReturn;
126
    }
127
128 2
    public function fetchAll(): array
129
    {
130 2
        $this->execute();
131
132 2
        $toReturn = $this->statement->fetchAll(PDO::FETCH_ASSOC);
133 2
        return $toReturn === false ? [] : $toReturn;
134
    }
135
136 13
    private function reset()
137
    {
138 13
        $this->statement = null;
139 13
    }
140
}
141