SleekDbal::setData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 3.0.0
13
 */
14
15
namespace Quantum\Libraries\Database\Adapters\Sleekdb;
16
17
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Criteria;
18
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Reducer;
19
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Result;
20
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Model;
21
use Quantum\Libraries\Database\Adapters\Sleekdb\Statements\Join;
22
use Quantum\Libraries\Database\Exceptions\DatabaseException;
23
use Quantum\Libraries\Database\Contracts\DbalInterface;
24
use SleekDB\Exceptions\InvalidConfigurationException;
25
use SleekDB\Exceptions\InvalidArgumentException;
26
use Quantum\Model\Exceptions\ModelException;
27
use Quantum\App\Exceptions\BaseException;
28
use SleekDB\Exceptions\IOException;
29
use SleekDB\QueryBuilder;
30
use SleekDB\Store;
31
32
/**
33
 * Class SleekDbal
34
 * @package Quantum\Libraries\Database
35
 */
36
class SleekDbal implements DbalInterface
37
{
38
    use Model;
39
    use Result;
40
    use Criteria;
41
    use Reducer;
42
    use Join;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $isNew = false;
48
49
    /**
50
     * @var array
51
     */
52
    protected $data = [];
53
54
    /**
55
     * @var array
56
     */
57
    protected $modifiedFields = [];
58
59
    /**
60
     * @var array
61
     */
62
    protected $criterias = [];
63
64
    /**
65
     * @var array
66
     */
67
    protected $havings = [];
68
69
    /**
70
     * @var array
71
     */
72
    protected $selected = [];
73
74
    /**
75
     * @var array
76
     */
77
    protected $grouped = [];
78
79
    /**
80
     * @var array
81
     */
82
    protected $ordered = [];
83
84
    /**
85
     * @var int|null
86
     */
87
    protected $offset;
88
89
    /**
90
     * @var int|null
91
     */
92
    protected $limit;
93
94
    /**
95
     * @var array
96
     */
97
    protected $joins = [];
98
99
    /**
100
     * Associated model name
101
     * @var string
102
     */
103
    private $modelName;
104
105
    /**
106
     * The database table associated with model
107
     * @var string
108
     */
109
    private $table;
110
111
    /**
112
     * ID column of table
113
     * @var string
114
     */
115
    private $idColumn;
116
117
    /**
118
     * Foreign keys
119
     * @var array
120
     */
121
    private $foreignKeys;
122
123
    /**
124
     * Hidden fields
125
     * @var array
126
     */
127
    public $hidden = [];
128
129
    /**
130
     * ORM Model
131
     * @var object|null
132
     */
133
    private $ormModel = null;
134
135
    /**
136
     * @var QueryBuilder|null
137
     */
138
    private $queryBuilder;
139
140
    /**
141
     * Active connection
142
     * @var array|null
143
     */
144
    private static $connection = null;
145
146
    /**
147
     * Operators map
148
     * @var string[]
149
     */
150
    private $operators = [
0 ignored issues
show
introduced by
The private property $operators is not used, and could be removed.
Loading history...
151
        '=', '!=',
152
        '>', '>=',
153
        '<', '<=',
154
        'IN', 'NOT IN',
155
        'LIKE', 'NOT LIKE',
156
        'BETWEEN', 'NOT BETWEEN',
157
    ];
158
159
    /**
160
     * @param string $table
161
     * @param string|null $modelName
162
     * @param string $idColumn
163
     * @param array $foreignKeys
164
     * @param array $hidden
165
     */
166
    public function __construct(
167
        string $table,
168
        ?string $modelName = null,
169
        string $idColumn = 'id',
170
        array  $foreignKeys = [],
171
        array  $hidden = []
172
    ) {
173
        $this->modelName = $modelName;
174
        $this->table = $table;
175
        $this->idColumn = $idColumn;
176
        $this->foreignKeys = $foreignKeys;
177
        $this->hidden = $hidden;
178
    }
179
180
    public function __get(string $key)
181
    {
182
        return $this->data[$key] ?? null;
183
    }
184
185
    /**
186
     * @inheritDoc
187
     */
188
    public static function connect(array $config)
189
    {
190
        self::$connection = $config;
191
    }
192
193
    public function setData(array $data)
194
    {
195
        $this->data = $data;
196
    }
197
198
    /**
199
     * @param $modifiedFields
200
     * @return void
201
     */
202
    public function setModifiedFields($modifiedFields)
203
    {
204
        $this->modifiedFields = $modifiedFields;
205
    }
206
207
    /**
208
     * @param bool $isNew
209
     * @return void
210
     */
211
    public function setIsNew(bool $isNew)
212
    {
213
        $this->isNew = $isNew;
214
    }
215
216
    /**
217
     * @inheritDoc
218
     */
219
    public static function getConnection(): ?array
220
    {
221
        return self::$connection;
222
    }
223
224
    /**
225
     * @inheritDoc
226
     */
227
    public static function disconnect()
228
    {
229
        self::$connection = null;
230
    }
231
232
    /**
233
     * @inheritDoc
234
     */
235
    public function getTable(): string
236
    {
237
        return $this->table;
238
    }
239
240
    /**
241
     * Gets the ORM model
242
     * @return Store
243
     * @throws DatabaseException
244
     * @throws IOException
245
     * @throws InvalidArgumentException
246
     * @throws InvalidConfigurationException
247
     * @throws BaseException
248
     */
249
    public function getOrmModel(): Store
250
    {
251
        if (!$this->ormModel) {
252
            if (!self::getConnection()) {
253
                throw DatabaseException::missingConfig('database');
254
            }
255
256
            $connection = self::getConnection();
257
258
            if (empty($connection['database_dir'])) {
259
                throw DatabaseException::incorrectConfig();
260
            }
261
262
            $connection['config']['primary_key'] = $this->idColumn;
263
264
            $this->ormModel = new Store($this->table, $connection['database_dir'], $connection['config']);
265
        }
266
267
        return $this->ormModel;
268
    }
269
270
    /**
271
     * @param array|null $modelData
272
     */
273
    public function updateOrmModel(?array $modelData)
274
    {
275
        $this->data = $modelData;
276
        $this->modifiedFields = $modelData;
277
        $this->isNew = false;
278
    }
279
280
    /**
281
     * @inheritdoc
282
     */
283
    public function truncate(): bool
284
    {
285
        try {
286
            $this->getOrmModel()->deleteStore();
287
            return true;
288
        } catch (\Exception $e) {
289
            return false;
290
        }
291
    }
292
293
    /**
294
     * Gets the query builder object
295
     * @return QueryBuilder
296
     * @throws BaseException
297
     * @throws DatabaseException
298
     * @throws IOException
299
     * @throws InvalidArgumentException
300
     * @throws InvalidConfigurationException
301
     * @throws ModelException
302
     */
303
    public function getBuilder(): QueryBuilder
304
    {
305
        if (!$this->queryBuilder) {
306
            $this->queryBuilder = $this->getOrmModel()->createQueryBuilder();
307
        }
308
309
        if ($this->selected !== []) {
310
            $this->queryBuilder->select($this->selected);
311
        }
312
313
        if ($this->joins !== []) {
314
            $this->applyJoins();
315
        }
316
317
        if ($this->criterias !== []) {
318
            $this->queryBuilder->where($this->criterias);
319
        }
320
321
        if ($this->havings !== []) {
322
            $this->queryBuilder->having($this->havings);
323
        }
324
325
        if ($this->grouped !== []) {
326
            $this->queryBuilder->groupBy($this->grouped);
327
        }
328
329
        if ($this->ordered !== []) {
330
            $this->queryBuilder->orderBy($this->ordered);
331
        }
332
333
        if ($this->offset) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->offset of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
334
            $this->queryBuilder->skip($this->offset);
335
        }
336
337
        if ($this->limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
338
            $this->queryBuilder->limit($this->limit);
339
        }
340
341
        return $this->queryBuilder;
342
    }
343
344
    /**
345
     * Gets foreign keys
346
     * @return array
347
     */
348
    public function getForeignKeys(): array
349
    {
350
        return $this->foreignKeys;
351
    }
352
353
    /**
354
     * Gets the associated model name
355
     * @return string
356
     */
357
    public function getModelName(): string
358
    {
359
        return $this->modelName;
360
    }
361
362
    /**
363
     * Resets the builder state
364
     */
365
    protected function resetBuilderState(): void
366
    {
367
        $this->criterias = [];
368
        $this->havings = [];
369
        $this->selected = [];
370
        $this->grouped = [];
371
        $this->ordered = [];
372
        $this->offset = null;
373
        $this->limit = null;
374
        $this->joins = [];
375
        $this->queryBuilder = null;
376
    }
377
}
378