Passed
Push — main ( 02c469...a6e9f3 )
by Alex
11:40
created

Database::columns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Erykai\Database;
4
5
use PDO;
6
use stdClass;
7
8
/**
9
 * CLASS CRUD
10
 */
11
class Database extends Resource
0 ignored issues
show
Bug introduced by
The type Erykai\Database\Resource 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...
12
{
13
    /**
14
     * @var string|null
15
     */
16
    private null|string $returnColumns = null;
17
    /**
18
     * @param $name
19
     * @param $value
20
     */
21
    public function __set($name, $value)
22
    {
23
        if (empty($this->data)) {
24
            $this->data = new stdClass();
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
        }
26
        $this->data->$name = $value;
27
    }
28
29
    /**
30
     * @param $name
31
     * @return mixed
32
     */
33
    public function __get($name)
34
    {
35
        return $this->data->$name;
36
    }
37
38
    /**
39
     * @param $name
40
     * @return bool
41
     */
42
    public function __isset($name)
43
    {
44
        return isset($this->data->$name);
45
    }
46
47
    /**
48
     * @param string|null $columns
49
     */
50
    public function columns(?string $columns = null): void
51
    {
52
        if($columns){
53
            $this->returnColumns = $columns;
54
        }
55
    }
56
57
    /**
58
     * @param string $columns
59
     * @param string|null $condition
60
     * @param array $params
61
     * @return $this
62
     */
63
    public function find(string $columns = '*', string $condition = null, array $params = null): static
64
    {
65
        if($this->returnColumns){
66
            $columns = $this->returnColumns . $columns;
67
        }
68
69
        $this->query = "SELECT $columns FROM $this->table";
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug Best Practice introduced by
The property table does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
70
        $this->params = $params;
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
71
        if ($condition) {
72
            $this->query .= " WHERE $condition ";
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param string $inner
80
     * @return $this
81
     */
82
    public function inner(string $inner): static
83
    {
84
        if (str_contains($this->query, 'WHERE')) {
85
            $query  = explode("WHERE", $this->query);
86
            $this->query = $query[0];
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
        }
88
        $this->query .= " $inner ";
89
        if (isset($query[1])){
90
            $this->query .= "WHERE $query[1] ";
91
        }
92
        return $this;
93
    }
94
95
    /**
96
     * @param string $order
97
     * @return $this
98
     */
99
    public function order(string $order = null): static
100
    {
101
        if($order){
102
            $this->queryEnd .= " ORDER BY $order ";
0 ignored issues
show
Bug Best Practice introduced by
The property queryEnd does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
103
        }
104
        return $this;
105
    }
106
107
    /**
108
     * @param string $column
109
     * @return $this
110
     */
111
    public function group(string $column): static
112
    {
113
        $this->queryEnd .= " GROUP BY $column ";
0 ignored issues
show
Bug Best Practice introduced by
The property queryEnd does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
114
        return $this;
115
    }
116
117
    /**
118
     * @param int $limit
119
     * @return $this
120
     */
121
    public function limit(int $limit): static
122
    {
123
        $this->queryEnd .= " LIMIT $limit ";
0 ignored issues
show
Bug Best Practice introduced by
The property queryEnd does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
124
        return $this;
125
    }
126
127
    /**
128
     * @param int $offset
129
     * @return $this
130
     */
131
    public function offset(int $offset): static
132
    {
133
        $this->queryEnd .= " OFFSET $offset ";
0 ignored issues
show
Bug Best Practice introduced by
The property queryEnd does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
134
        return $this;
135
    }
136
137
    /**
138
     * @param bool $all
139
     * @return object|null
140
     */
141
    public function fetch(bool $all = false): ?object
142
    {
143
        if($this->queryEnd){
0 ignored issues
show
Bug Best Practice introduced by
The property queryEnd does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
144
            $this->query = $this->query . $this->queryEnd;
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
145
        }
146
        $this->data = null;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
147
        $this->stmt = $this->conn->prepare($this->query);
0 ignored issues
show
Bug Best Practice introduced by
The property conn does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property stmt does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
148
        if(!$this->params){
149
            $this->params = [];
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
150
        }
151
        $this->bind($this->params);
152
        $this->stmt->execute();
153
154
        if ($all) {
155
            if ($this->stmt->rowCount()) {
156
                $this->data = (object)$this->stmt->fetchAll();
157
            } else {
158
                $this->setResponse(404, "error", "no results found");
159
            }
160
        } else if ($this->stmt->rowCount()) {
161
            $this->data = (object)$this->stmt->fetch();
162
        } else {
163
            $this->setResponse(404, "error", "no results found");
164
        }
165
166
        return $this->data;
167
    }
168
169
    /**
170
     * @param int $id
171
     * @return bool
172
     */
173
    public function delete(int $id): bool
174
    {
175
        $dynamic = $this->nameId . " " . $id;
0 ignored issues
show
Bug Best Practice introduced by
The property nameId does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
176
177
        $stmt = $this->conn->prepare("DELETE FROM $this->table WHERE $this->nameId = :$this->nameId");
0 ignored issues
show
Bug Best Practice introduced by
The property conn does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property table does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
178
        $stmt->bindParam(":$this->nameId", $id, PDO::PARAM_INT);
179
180
        if ($stmt->execute()) {
181
            $this->setResponse(200, "success", "$dynamic was successfully removed", dynamic: $dynamic);
182
            return true;
183
        }
184
        $this->setResponse(400, "error", "failed to delete $dynamic", dynamic: $dynamic);
185
        return false;
186
    }
187
188
    /**
189
     * @param string $columns
190
     * @return object|null
191
     */
192
    public function data(string $columns = "*"): ?object
193
    {
194
        if (!empty($this->data->{0})) {
195
            return $this->data;
196
        }
197
198
        $id = $this->nameId;
0 ignored issues
show
Bug Best Practice introduced by
The property nameId does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
199
        if (!isset($this->data->$id)) {
200
            $this->find($columns, "$id=:$id", [$id => $this->conn->lastInsertId()])->fetch();
0 ignored issues
show
Bug Best Practice introduced by
The property conn does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
201
            return $this->data;
202
        }
203
        $this->find($columns, "$id=:$id", [$id => $this->data->$id])->fetch();
204
        return $this->data;
205
    }
206
207
    public function count()
208
    {
209
        $this->data = null;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
210
        $this->stmt = $this->conn->prepare($this->query);
0 ignored issues
show
Bug Best Practice introduced by
The property stmt does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug Best Practice introduced by
The property conn does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
211
        if(!$this->params){
212
            $this->params = [];
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
213
        }
214
        $this->bind($this->params);
215
        $this->stmt->execute();
216
        return $this->stmt->rowCount();
217
    }
218
219
    /**
220
     * @return bool
221
     */
222
    public function save(): bool
223
    {
224
        $id = $this->nameId;
0 ignored issues
show
Bug Best Practice introduced by
The property nameId does not exist on Erykai\Database\Database. Since you implemented __get, consider adding a @property annotation.
Loading history...
225
        if (!isset($this->data->$id)) {
226
            if ($this->create()) {
227
                $this->stmt->execute();
228
                $this->setResponse(200, "success", "registration successful", $this->data);
229
                return true;
230
            }
231
            return false;
232
        }
233
234
        if ($this->update()) {
235
            $this->stmt->execute();
236
            $this->setResponse(200, "success", "updated successful", $this->data);
237
            return true;
238
        }
239
        return false;
240
    }
241
    /**
242
     * @return object
243
     */
244
    public function response(): object
245
    {
246
        return $this->getResponse();
247
    }
248
249
}