Completed
Push — master ( 55546c...487dd2 )
by Pierre
03:07
created

Orm   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Test Coverage

Coverage 5%

Importance

Changes 0
Metric Value
eloc 104
dl 0
loc 273
ccs 5
cts 100
cp 0.05
rs 9.84
c 0
b 0
f 0
wmc 32

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getSql() 0 3 1
A getQuery() 0 3 1
A buildWhere() 0 7 2
B build() 0 47 11
A find() 0 7 1
A update() 0 7 1
A count() 0 15 2
A getQueryBuilder() 0 3 1
B getWhereOperator() 0 24 9
A insert() 0 6 1
A delete() 0 6 1
A __construct() 0 6 1
1
<?php
2
3
namespace App\Component\Model\Orm;
4
5
use App\Config;
6
use App\Container;
7
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
8
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
9
use NilPortugues\Sql\QueryBuilder\Manipulation\Update;
10
use NilPortugues\Sql\QueryBuilder\Manipulation\Insert;
11
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete;
12
13
/**
14
 * Poor man orm
15
 */
16
class Orm implements IOrm
17
{
18
19
    /**
20
     * config
21
     *
22
     * @var Config
23
     */
24
    protected $config;
25
26
    /**
27
     * what fields
28
     * @var array
29
     */
30
    protected $what;
31
32
    /**
33
     * where criterias
34
     * @var array
35
     */
36
    protected $where;
37
38
    /**
39
     * table name
40
     * @var string
41
     */
42
    protected $tablename;
43
44
    /**
45
     * table primary key
46
     * @var string
47
     */
48
    protected $primary;
49
50
    /**
51
     * table name
52
     * @var string
53
     */
54
    protected $dbname;
55
56
    /**
57
     * pool name
58
     * @var string
59
     */
60
    protected $poolname;
61
62
    /**
63
     * query builder instance
64
     * @var GenericBuilder
65
     * @see https://github.com/nilportugues/php-sql-query-builder
66
     */
67
    protected $queryBuilder;
68
69
    /**
70
     * query from builder
71
     * @var object
72
     */
73
    protected $query;
74
75
    /**
76
     * instanciate
77
     * @param Container $container
78
     */
79 1
    public function __construct(Container $container)
80
    {
81 1
        $this->config = $container->getService(Config::class);
82 1
        $this->queryBuilder = new GenericBuilder();
83 1
        $this->query = null;
84 1
        return $this;
85
    }
86
87
    /**
88
     * find a record with what field matching where criterias
89
     * @param array $what
90
     * @param array $where
91
     */
92
    public function find(array $what = [], array $where = []): Orm
93
    {
94
        $this->what = $what;
95
        $this->where = $where;
96
        $this->query = new Select();
97
        $this->build();
98
        return $this;
99
    }
100
101
    /**
102
     * count records matching where criterias.
103
     * aliases is formely [columnn => column_alias]
104
     *
105
     * @param array $where
106
     * @param array $aliases
107
     * @return Orm
108
     */
109
    public function count(array $where = [], array $aliases = []): Orm
110
    {
111
        $this->where = $where;
112
        $this->query = new Select();
113
        if (empty($aliases)) {
114
            $this->query->count();
115
        } else {
116
            $alias = $aliases[0];
117
            $this->query->count(
118
                array_keys($alias)[0],
119
                array_values($alias)[0]
120
            );
121
        }
122
        $this->buildWhere();
123
        return $this;
124
    }
125
126
    /**
127
     * update a record with what fields matching where criterias
128
     * @param array $what
129
     * @param array $where
130
     */
131
    public function update(array $what = [], array $where = []): Orm
132
    {
133
        $this->what = $what;
134
        $this->where = $where;
135
        $this->query = new Update();
136
        $this->build();
137
        return $this;
138
    }
139
140
    /**
141
     * insert record with what fields
142
     * @param array $what
143
     */
144
    public function insert(array $what = []): Orm
145
    {
146
        $this->what = $what;
147
        $this->query = new Insert();
148
        $this->build();
149
        return $this;
150
    }
151
152
    /**
153
     * delete matching where criterias
154
     * @param array $where
155
     */
156
    public function delete(array $where = []): Orm
157
    {
158
        $this->where = $where;
159
        $this->query = new Delete();
160
        $this->build();
161
        return $this;
162
    }
163
164
    /**
165
     * builder instance
166
     * @return GenericBuilder
167
     */
168
    public function getQueryBuilder(): GenericBuilder
169
    {
170
        return $this->queryBuilder;
171
    }
172
173
    /**
174
     * query instance
175
     * @return object
176
     */
177
    public function getQuery()
178
    {
179
        return $this->query;
180
    }
181
182
    /**
183
     * query sql string
184
     * @return string
185
     */
186
    public function getSql()
187
    {
188
        return $this->queryBuilder->write($this->query);
189
    }
190
191
    /**
192
     * build sql query
193
     *
194
     * @return Orm
195
     */
196
    protected function build(): Orm
197
    {
198
        if (false === is_object($this->query)) {
199
            throw new \Exception('Build : Invalid query instance');
200
        }
201
        $queryClassname = get_class($this->query);
202
        if (false === class_exists($queryClassname)) {
203
            throw new \Exception('Build : Invalid query type');
204
        }
205
        $this->query->setTable($this->tablename);
206
        switch ($queryClassname) {
207
            case Select::class:
208
                $this->query->setColumns($this->what);
209
                break;
210
            case Update::class:
211
                if (empty($this->what)) {
212
                    throw new \Exception(
213
                        'Build : Update requires not empty payload'
214
                    );
215
                }
216
                if (empty($this->where)) {
217
                    throw new \Exception(
218
                        'Build : Update requires at least one condition'
219
                    );
220
                }
221
                $this->query->setValues($this->what);
222
                break;
223
            case Insert::class:
224
                if (empty($this->what)) {
225
                    throw new \Exception(
226
                        'Build : Insert requires not empty payload'
227
                    );
228
                }
229
                $this->query->setValues($this->what);
230
                break;
231
            case Delete::class:
232
                if (empty($this->where)) {
233
                    throw new \Exception(
234
                        'Build : Delete requires at least one condition'
235
                    );
236
                }
237
                break;
238
            default:
239
                break;
240
        }
241
        $this->buildWhere();
242
        return $this;
243
    }
244
245
    /**
246
     * build where condition on query
247
     *
248
     * @return Orm
249
     */
250
    protected function buildWhere(): Orm
251
    {
252
        foreach ($this->where as $k => $v) {
253
            $whereOperator = $this->getWhereOperator($k, $v);
254
            $this->query->where()->{$whereOperator}($k, $v);
255
        }
256
        return $this;
257
    }
258
259
    /**
260
     * check where condition ket values to find operators
261
     *
262
     * @param string $whereColumn
263
     * @return string
264
     */
265
    protected function getWhereOperator(string &$whereColumn, $value): string
266
    {
267
        $hasArray = is_array($value);
268
        $operator = $whereColumn[strlen($whereColumn) - 1];
269
        $hasOperator = in_array($operator, self::OPERATORS);
270
        if (false === $hasOperator) {
271
            return ($hasArray) ? 'in' : 'equals';
272
        }
273
        foreach (self::OPERATORS as $op) {
274
            $whereColumn = str_replace($op, '', $whereColumn);
275
        }
276
        switch ($operator) {
277
            case '!':
278
                return ($hasArray) ? 'notIn' : 'notEquals';
279
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
280
            case '<':
281
                return 'lessThan';
282
                break;
283
            case '>':
284
                return 'greaterThan';
285
                break;
286
            case '#':
287
                return 'like';
288
                break;
289
        }
290
    }
291
}
292