Database   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 87
c 5
b 1
f 0
dl 0
loc 240
rs 9.6
wmc 35

16 Methods

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