Completed
Push — master ( 082214...beebf5 )
by Midori
02:28
created

Database::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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 0
cts 3
cp 0
crap 2
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
    public function __construct(PDO $db, QueryInterface $query)
19
    {
20
        $this->db = $db;
21
        $this->query = $query;
22
    }
23
24
    public function query(QueryInterface $query): self
25
    {
26
        $this->reset();
27
        $this->query = $query;
28
        return $this;
29
    }
30
31
    public function select($table, array $columns = ['*']): self
32
    {
33
        $this->reset();
34
        $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
        return $this;
36
    }
37
38
    public function delete($table): self
39
    {
40
        $this->reset();
41
        $this->query->delete($table);
42
        return $this;
43
    }
44
45
    public function update($table, array $values): self
46
    {
47
        $this->reset();
48
        $this->query->update($table, $values);
49
        return $this;
50
    }
51
52
    public function insert($table, array $values): self
53
    {
54
        $this->reset();
55
        $this->query->insert($table, $values);
56
        return $this;
57
    }
58
59
    public function where($key, $value, string $operator = '='): self
60
    {
61
        $this->query->where($key, $value, $operator);
62
        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
    public function orderBy($key, $order = 'ASC'): self
72
    {
73
        if ($order !== 'DESC' && $order !== 'ASC') {
74
            throw new InvalidArgumentException('Invalid order value');
75
        }
76
77
        $this->query->orderBy($key, $order);
78
        return $this;
79
    }
80
81
    public function limit(int $limit): self
82
    {
83
        $this->query->limit($limit);
84
        return $this;
85
    }
86
87
    public function offset(int $offset): self
88
    {
89
        $this->query->offset($offset);
90
        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
    public function lastInsertId(): string
106
    {
107
        return $this->db->lastInsertId();
108
    }
109
110
    public function rowCount(): int
111
    {
112
        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
    public function execute(): bool
116
    {
117
        $this->statement = $this->db->prepare($this->query->getStatement());
118
        return $this->statement->execute($this->query->getParams());
119
    }
120
121
    public function fetch(): array
122
    {
123
        $this->execute();
124
        $toReturn = $this->statement->fetch(PDO::FETCH_ASSOC);
125
        return $toReturn === false ? [] : $toReturn;
126
    }
127
128
    public function fetchAll(): array
129
    {
130
        $this->execute();
131
132
        $toReturn = $this->statement->fetchAll(PDO::FETCH_ASSOC);
133
        return $toReturn === false ? [] : $toReturn;
134
    }
135
136
    private function reset()
137
    {
138
        $this->statement = null;
139
    }
140
}
141