Passed
Pull Request — master (#51)
by Arman
05:24 queued 02:50
created

SleekDbal::getBuilder()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 39
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 19
nc 512
nop 0
dl 0
loc 39
rs 4.1777
c 2
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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