Completed
Push — master ( 8f0aff...4537c1 )
by Midori
14:28
created

Database::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
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 midorikocak\querymaker\QueryMaker;
10
use PDO;
11
use PDOStatement;
12
13
class Database implements DatabaseInterface
14
{
15
    private PDO $db;
16
    private ?QueryInterface $queryMaker = null;
17
    private ?PDOStatement $statement = null;
18
19
    public function __construct(PDO $db)
20
    {
21
        $this->db = $db;
22
    }
23
24
    public function select($table, array $columns = ['*']): self
25
    {
26
        $this->reset();
27
        $this->queryMaker = QueryMaker::select($table, $columns);
28
        return $this;
29
    }
30
31
    public function delete($table): self
32
    {
33
        $this->reset();
34
        $this->queryMaker = QueryMaker::delete($table);
35
        return $this;
36
    }
37
38
    public function update($table, array $values): self
39
    {
40
        $this->reset();
41
        $this->queryMaker = QueryMaker::update($table, $values);
42
        return $this;
43
    }
44
45
    public function insert($table, array $values): self
46
    {
47
        $this->reset();
48
        $this->queryMaker = QueryMaker::insert($table, $values);
49
        return $this;
50
    }
51
52
    public function where($key, $value, string $operator = '='): self
53
    {
54
        $this->queryMaker->where($key, $value, $operator);
0 ignored issues
show
Bug introduced by
The method where() 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

54
        $this->queryMaker->/** @scrutinizer ignore-call */ 
55
                           where($key, $value, $operator);

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...
55
        return $this;
56
    }
57
58
    public function and($key, $value, string $operator = '='): self
59
    {
60
        $this->queryMaker->and($key, $value, $operator);
61
        return $this;
62
    }
63
64
    public function orderBy($key, $order = 'ASC'): self
65
    {
66
        if ($order !== 'DESC' && $order !== 'ASC') {
67
            throw new InvalidArgumentException('Invalid order value');
68
        }
69
70
        $this->queryMaker->orderBy($key, $order);
71
        return $this;
72
    }
73
74
    public function limit(int $limit): self
75
    {
76
        $this->queryMaker->limit($limit);
77
        return $this;
78
    }
79
80
    public function offset(int $offset): self
81
    {
82
        $this->queryMaker->offset($offset);
83
        return $this;
84
    }
85
86
    public function or($key, $value, string $operator = '='): self
87
    {
88
        $this->queryMaker->or($key, $value, $operator);
89
        return $this;
90
    }
91
92
    public function between($key, $before, $after): self
93
    {
94
        $this->queryMaker->between($key, $before, $after);
95
        return $this;
96
    }
97
98
    public function lastInsertId(): string
99
    {
100
        return $this->db->lastInsertId();
101
    }
102
103
    public function rowCount(): int
104
    {
105
        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

105
        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...
106
    }
107
108
    public function execute(): bool
109
    {
110
        $this->statement = $this->db->prepare($this->queryMaker->getStatement());
111
        return $this->statement->execute($this->queryMaker->getParams());
112
    }
113
114
    public function fetch(): array
115
    {
116
        $this->execute();
117
        $toReturn = $this->statement->fetch(PDO::FETCH_ASSOC);
118
        return $toReturn === false ? [] : $toReturn;
119
    }
120
121
    public function fetchAll(): array
122
    {
123
        $this->execute();
124
125
        $toReturn = $this->statement->fetchAll(PDO::FETCH_ASSOC);
126
        return $toReturn === false ? [] : $toReturn;
127
    }
128
129
    private function reset()
130
    {
131
        $this->statement = null;
132
        $this->queryMaker = null;
133
    }
134
}
135