Total Complexity | 57 |
Total Lines | 471 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like Dolphin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Dolphin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class Dolphin |
||
56 | { |
||
57 | protected $fields = array(); |
||
58 | public $table; |
||
59 | public $className; |
||
60 | protected $groupBy; |
||
61 | protected $orderBy; |
||
62 | protected $having; |
||
63 | protected $join = array(); |
||
64 | protected $leftJoin = array(); |
||
65 | protected $rightJoin = array(); |
||
66 | protected $crossJoin = array(); |
||
67 | protected $where = array(); |
||
68 | protected $whereIn = array(); |
||
69 | protected $whereNotIn = array(); |
||
70 | protected $whereNull = array(); |
||
71 | protected $whereNotNull = array(); |
||
72 | protected $limit; |
||
73 | protected $offset; |
||
74 | protected $results; |
||
75 | |||
76 | public function select() |
||
77 | { |
||
78 | $args = func_get_args(); |
||
79 | $fldAr = array(); |
||
80 | $qb = new QueryBuilder(); |
||
81 | |||
82 | foreach ($args as $arg) { |
||
83 | $argsAr = explode(',', $arg); |
||
84 | foreach ($argsAr as $ar) { |
||
85 | $fldAr[] = $qb->quote(trim($ar)); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | $this->fields = array_merge($this->fields, $fldAr); |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | public function selectRaw() |
||
95 | { |
||
96 | $args = func_get_args(); |
||
97 | $fldAr = array(); |
||
98 | |||
99 | foreach ($args as $arg) { |
||
100 | $argsAr = explode(',', $arg); |
||
101 | foreach ($argsAr as $ar) { |
||
102 | $fldAr[] = trim($ar); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | $this->fields = array_merge($this->fields, $fldAr); |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | public function join($join, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
||
112 | { |
||
113 | $this->join = array_merge($this->join, [[$join, $mixedParam, $param3, $param4, $mixedParam2]]); |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | public function leftJoin($leftJoin, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
||
119 | { |
||
120 | $this->leftJoin = array_merge($this->leftJoin, [[$leftJoin, $mixedParam, $param3, $param4, $mixedParam2]]); |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | public function rightJoin($rightJoin, $mixedParam, $param3 = null, $param4 = null, $mixedParam2 = null) |
||
126 | { |
||
127 | $this->rightJoin = array_merge($this->rightJoin, [[$rightJoin, $mixedParam, $param3, $param4, $mixedParam2]]); |
||
128 | |||
129 | return $this; |
||
130 | } |
||
131 | |||
132 | public function crossJoin($crossJoin, $params = null) |
||
133 | { |
||
134 | $this->crossJoin = array_merge($this->crossJoin, [[$crossJoin, $params]]); |
||
135 | |||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | public function where() |
||
140 | { |
||
141 | $args = func_get_args(); |
||
142 | if(func_num_args()===2){ |
||
143 | $this->where = array_merge($this->where, [[$args[0], '=', $args[1]]]); |
||
144 | } elseif(func_num_args()===3){ |
||
145 | $this->where = array_merge($this->where, [[$args[0], $args[1], $args[2]]]); |
||
146 | } else{ |
||
147 | throw new Exception('Where parameter contains invalid number of parameters', 1); |
||
148 | } |
||
149 | |||
150 | return $this; |
||
151 | } |
||
152 | |||
153 | public function whereIn($whereIn, $params = array()) |
||
154 | { |
||
155 | $this->whereIn = array_merge($this->whereIn, [[$whereIn, $params]]); |
||
156 | |||
157 | return $this; |
||
158 | } |
||
159 | |||
160 | public function whereNotIn($whereNotIn, $params = array()) |
||
161 | { |
||
162 | $this->whereNotIn = array_merge($this->whereNotIn, [[$whereNotIn, $params]]); |
||
163 | |||
164 | return $this; |
||
165 | } |
||
166 | |||
167 | public function whereNull($whereNull) |
||
168 | { |
||
169 | $this->whereNull = array_merge($this->whereNull, [$whereNull]); |
||
170 | |||
171 | return $this; |
||
172 | } |
||
173 | |||
174 | public function whereNotNull($whereNotNull) |
||
179 | } |
||
180 | |||
181 | public function offset($offset) |
||
182 | { |
||
183 | $this->offset = $offset; |
||
184 | |||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | public function limit($limit) |
||
189 | { |
||
190 | $this->limit = $limit; |
||
191 | |||
192 | return $this; |
||
193 | } |
||
194 | |||
195 | public function orderBy($orderBy) |
||
196 | { |
||
197 | $this->orderBy = $orderBy; |
||
198 | |||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | public function groupBy($groupBy) |
||
203 | { |
||
204 | $this->groupBy = $groupBy; |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | public function having($having) |
||
210 | { |
||
211 | $this->having = $having; |
||
212 | |||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | protected function buildAllWhereQuery() |
||
224 | ); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Builds Query added by method chaining. |
||
229 | * It has the main logic of ORM |
||
230 | */ |
||
231 | protected function buildQuery() |
||
232 | { |
||
233 | $query = array(); |
||
234 | $tblWithPrefix = $this->table; |
||
235 | $qb = new QueryBuilder(); |
||
236 | $jqb = new JoinQueryBuilder(); |
||
237 | $prefix = $qb->getPrefix(); |
||
238 | $tbl = str_replace($prefix, '', $tblWithPrefix); |
||
239 | |||
291 | } |
||
292 | |||
293 | protected function reset() |
||
294 | { |
||
295 | $this->fields = array(); |
||
296 | $this->table = null; |
||
297 | $this->className = null; |
||
298 | $this->groupBy = null; |
||
299 | $this->orderBy = null; |
||
300 | $this->having = null; |
||
301 | $this->join = array(); |
||
302 | $this->leftJoin = array(); |
||
303 | $this->rightJoin = array(); |
||
304 | $this->crossJoin = array(); |
||
305 | $this->where = array(); |
||
306 | $this->whereIn = array(); |
||
307 | $this->whereNotIn = array(); |
||
308 | $this->whereNull = array(); |
||
309 | $this->whereNotNull = array(); |
||
310 | $this->limit = null; |
||
311 | $this->offset = null; |
||
312 | } |
||
313 | |||
314 | public function prepare($query, $fetchRows = 'all') |
||
315 | { |
||
316 | $qb = new QueryBuilder(); |
||
317 | $wqp = new WhereQueryParser(); |
||
318 | $util = new Utils(); |
||
319 | |||
320 | try { |
||
321 | $ar = $wqp->parseWhereQuery($this->where); |
||
322 | $stmt = Connection::get()->prepare($qb->queryPrefix($query)); |
||
323 | $stmt->execute($ar); |
||
324 | |||
325 | if ($fetchRows == 'first') { |
||
326 | $rows = $stmt->fetch(\PDO::FETCH_OBJ); |
||
327 | $this->results = $rows; |
||
328 | // now turn this stdClass object to the object type of calling model |
||
329 | $rows = $util->turnObject($this->className, $rows); |
||
330 | } else { |
||
331 | $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
||
332 | $this->results = $rows; |
||
333 | $rows = $util->turnObjects($this->className, $rows); |
||
334 | } |
||
335 | |||
336 | // Reset class variables |
||
337 | $this->reset(); |
||
338 | |||
339 | return $rows; |
||
340 | } catch (\PDOException $ex) { |
||
341 | throw new \PDOException($ex->getMessage(), 1); |
||
342 | } catch (Exception $e) { |
||
343 | throw new Exception($e->getMessage(), 1); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | public function query($query, $fetchRows = 'all') |
||
366 | } |
||
367 | } |
||
368 | |||
369 | public function get() |
||
370 | { |
||
371 | return $this->prepare($this->buildQuery()); |
||
372 | } |
||
373 | |||
374 | public function first() |
||
375 | { |
||
376 | $query = $this->buildQuery(); |
||
377 | |||
378 | if (strripos($query, 'LIMIT 1')) { |
||
379 | // [TODO] |
||
380 | } else { |
||
381 | $query .= ' LIMIT 1'; |
||
382 | } |
||
383 | |||
384 | return $this->prepare($query, 'first'); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * It fetches the row by primary key |
||
389 | * |
||
390 | * @author RN Kushwaha <[email protected]> |
||
391 | * @since v0.0.5 |
||
392 | */ |
||
393 | public function find($id) |
||
394 | { |
||
395 | $this->where('id = :id', $id); |
||
396 | |||
397 | return $this->first(); |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * It fetches the row by primary key |
||
402 | * |
||
403 | * @param int $id |
||
404 | * @return object $row |
||
405 | * @throws Exception |
||
406 | * @author RN Kushwaha <[email protected]> |
||
407 | * @since v0.0.5 |
||
408 | */ |
||
409 | public function findOrFail($id) |
||
410 | { |
||
411 | $this->where('id = :id', $id); |
||
412 | |||
413 | $row = $this->first(); |
||
414 | |||
415 | if($row == null ){ |
||
416 | throw new Exception("The record does not exists!"); |
||
417 | } |
||
418 | |||
419 | return $row; |
||
420 | } |
||
421 | |||
422 | public function count() |
||
423 | { |
||
424 | $this->fields = null; |
||
425 | $query = $this->buildQuery(); |
||
426 | $query = str_replace('SELECT * ', 'SELECT COUNT(*) as count ', $query); |
||
427 | |||
428 | return $this->query($query, 'count'); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * It truncates the table |
||
433 | * |
||
434 | * @return boolean |
||
435 | * @throws Exception |
||
436 | * @author RN Kushwaha <[email protected]> |
||
437 | * @since v0.0.5 |
||
438 | */ |
||
439 | public function truncate() |
||
440 | { |
||
441 | $qb = new QueryBuilder(); |
||
442 | $query = "TRUNCATE ".$this->table; |
||
443 | |||
444 | try{ |
||
445 | Connection::get()->query($qb->queryPrefix($query)); |
||
446 | } catch(Exception $e){ |
||
447 | throw new Exception($e->getMessage()); |
||
448 | } |
||
449 | |||
450 | return true; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * It inserts the new rows |
||
455 | * |
||
456 | * @param array $rows |
||
457 | * @return integer $lastInsertedId |
||
458 | * @throws Exception |
||
459 | * @author RN Kushwaha <[email protected]> |
||
460 | * @since v0.0.5 |
||
461 | */ |
||
462 | public function insert($rows) |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * It updates the rows |
||
470 | * |
||
471 | * @param array $row |
||
472 | * @return boolean |
||
473 | * @throws Exception |
||
474 | * @author RN Kushwaha <[email protected]> |
||
475 | * @since v0.0.5 |
||
476 | */ |
||
477 | public function update($row) |
||
478 | { |
||
479 | $qb = new QueryBuilder(); |
||
480 | $query = "UPDATE ".$this->table." SET "; |
||
481 | $ar = array(); |
||
482 | |||
483 | foreach($row as $key => $val){ |
||
484 | $ar[':'.$key] = $val; |
||
485 | $query.= $qb->quote($key)." =:".$key.","; |
||
486 | } |
||
487 | |||
488 | $query = rtrim($query, ","); |
||
489 | |||
490 | try{ |
||
491 | $whereQuery = $this->buildAllWhereQuery(); |
||
492 | $query.= " ".join(" ", $whereQuery); |
||
493 | $stmt = Connection::get()->prepare($qb->queryPrefix($query)); |
||
494 | $stmt->execute($ar); |
||
495 | $this->reset(); |
||
496 | } catch(Exception $e){ |
||
497 | throw new Exception($e->getMessage()); |
||
498 | } |
||
499 | |||
500 | return true; |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * It deleted the rows matched by where clause |
||
505 | * |
||
506 | * @return boolean |
||
507 | * @throws Exception |
||
508 | * @author RN Kushwaha <[email protected]> |
||
509 | * @since v0.0.5 |
||
510 | */ |
||
511 | public function delete() |
||
526 | } |
||
527 | |||
528 | } |
||
529 |