Total Complexity | 99 |
Total Lines | 601 |
Duplicated Lines | 21.13 % |
Coverage | 0.72% |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QueryBuilderFactory 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 QueryBuilderFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class QueryBuilderFactory extends AbstractQuery |
||
9 | { |
||
10 | const DIRECTION_AZ = 'asc'; |
||
11 | |||
12 | const DIRECTION_ZA = 'desc'; |
||
13 | |||
14 | const DEFAULT_OPERATOR = 'eq'; |
||
15 | |||
16 | /** |
||
17 | * @var QueryBuilder |
||
18 | */ |
||
19 | protected $qBuilder; |
||
20 | |||
21 | protected $fields; |
||
22 | |||
23 | protected $filtering; |
||
24 | |||
25 | protected $orFiltering; |
||
26 | |||
27 | protected $relationEntityAlias; |
||
28 | |||
29 | protected $sorting; |
||
30 | |||
31 | protected $joins; |
||
32 | |||
33 | protected $rel; |
||
34 | |||
35 | protected $printing; |
||
36 | |||
37 | protected $page; |
||
38 | |||
39 | protected $pageLength; |
||
40 | |||
41 | protected $select; |
||
42 | |||
43 | protected $configProvider; |
||
44 | |||
45 | public function getAvailableFilters() |
||
46 | { |
||
47 | return array_keys($this->getValueAvailableFilters()); |
||
48 | } |
||
49 | |||
50 | public function getValueAvailableFilters() |
||
51 | { |
||
52 | return Operators::getOperators(); |
||
53 | } |
||
54 | |||
55 | public function setFields(array $fields = []) |
||
56 | { |
||
57 | $this->fields = $fields; |
||
58 | |||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | public function getFields() |
||
63 | { |
||
64 | if (null === $this->fields) { |
||
65 | throw new \RuntimeException( |
||
66 | 'Oops! Fields are not defined' |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | return $this->fields; |
||
71 | } |
||
72 | |||
73 | public function setFilters(array $filtering = []) |
||
78 | 1 | } |
|
79 | |||
80 | public function setOrFilters(array $orFiltering = []) |
||
81 | { |
||
82 | $this->orFiltering = $orFiltering; |
||
83 | |||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | public function setSorting(array $sorting = []) |
||
88 | { |
||
89 | $this->sorting = $sorting; |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | public function getFilters() |
||
95 | { |
||
96 | return $this->filtering; |
||
97 | } |
||
98 | |||
99 | public function getOrFilters() |
||
100 | { |
||
101 | return $this->orFiltering; |
||
102 | } |
||
103 | |||
104 | private function noExistsJoin($prevEntityAlias, $currentEntityAlias) |
||
105 | { |
||
106 | if (null === $this->joins) { |
||
107 | $this->joins = []; |
||
108 | } |
||
109 | |||
110 | $needle = $prevEntityAlias . "_" . $currentEntityAlias; |
||
111 | |||
112 | return ! in_array($needle, $this->joins); |
||
113 | } |
||
114 | |||
115 | private function storeJoin($prevEntityAlias, $currentEntityAlias) |
||
116 | { |
||
117 | $needle = $prevEntityAlias . "_" . $currentEntityAlias; |
||
118 | $this->joins[$needle] = $needle; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param String $relation Nome della relazione semplice (groups.name) o con embedded (_embedded.groups.name) |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function join(String $relation) |
||
126 | { |
||
127 | $relation = explode('|', $relation)[0]; |
||
128 | $relations = [$relation]; |
||
129 | |||
130 | if (strstr($relation, '_embedded.')) { |
||
131 | $embeddedFields = explode('.', $relation); |
||
132 | $relation = $this->parser->camelize($embeddedFields[1]); |
||
|
|||
133 | |||
134 | // elimino l'ultimo elemento che dovrebbe essere il nome del campo |
||
135 | unset($embeddedFields[count($embeddedFields) - 1]); |
||
136 | |||
137 | // elimino il primo elemento _embedded |
||
138 | unset($embeddedFields[0]); |
||
139 | |||
140 | $relations = $embeddedFields; |
||
141 | } |
||
142 | |||
143 | $entityName = $this->getEntityName(); |
||
144 | $entityAlias = $this->entityAlias; |
||
145 | |||
146 | foreach ($relations as $relation) { |
||
147 | |||
148 | $relation = $this->parser->camelize($relation); |
||
149 | $relationEntityAlias = 'table_' . $relation; |
||
150 | |||
151 | $metadata = $this->manager->getClassMetadata($entityName); |
||
152 | |||
153 | if ($metadata->hasAssociation($relation)) { |
||
154 | |||
155 | $association = $metadata->getAssociationMapping($relation); |
||
156 | |||
157 | $fieldName = $this->parser->camelize($association['fieldName']); |
||
158 | |||
159 | if ($this->noExistsJoin($relationEntityAlias, $relation)) { |
||
160 | |||
161 | $this->qBuilder |
||
162 | ->join($entityAlias . "." . $fieldName, $relationEntityAlias); |
||
163 | |||
164 | if ($this->configProvider) { |
||
165 | // add where conditions |
||
166 | // with user's additional filters |
||
167 | } |
||
168 | |||
169 | $this->storeJoin($relationEntityAlias, $relation); |
||
170 | } |
||
171 | $entityName = $association['targetEntity']; |
||
172 | $entityAlias = $relationEntityAlias; |
||
173 | } |
||
174 | |||
175 | $this->setRelationEntityAlias($relationEntityAlias); |
||
176 | } |
||
177 | |||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | public function filter() |
||
182 | { |
||
183 | if (null === $this->filtering) { |
||
184 | throw new \RuntimeException( |
||
185 | 'Oops! Filtering is not defined' |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | if (!$this->fields) { |
||
190 | throw new \RuntimeException( |
||
191 | 'Oops! Fields are not defined' |
||
192 | ); |
||
193 | } |
||
194 | |||
195 | foreach ($this->filtering as $filter => $value) { |
||
196 | $this->applyFilterAnd($filter, $value); |
||
197 | } |
||
198 | |||
199 | if (null !== $this->orFiltering) { |
||
200 | $orFilter = []; |
||
201 | $orFilter['orCondition'] = null; |
||
202 | $orFilter['parameters'] = []; |
||
203 | |||
204 | foreach ($this->orFiltering as $filter => $value) { |
||
205 | $orFilter = $this->applyFilterOr($filter, $value, $orFilter); |
||
206 | } |
||
207 | |||
208 | if ((count($orFilter) > 0) && ($orFilter['orCondition'] != null)) { |
||
209 | $this->qBuilder->andWhere($orFilter['orCondition']); |
||
210 | |||
211 | foreach ($orFilter['parameters'] as $parameter) { |
||
212 | $this->qBuilder->setParameter($parameter['field'], $parameter['value']); |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | |||
217 | return $this; |
||
218 | } |
||
219 | |||
220 | private function applyFilterAnd($filter, $value) |
||
221 | { |
||
222 | $whereCondition = null; |
||
223 | $filterAndOperator = explode('|',$filter); |
||
224 | |||
225 | $fieldName = $filterAndOperator[0]; |
||
226 | $fieldName = $this->parser->camelize($fieldName); |
||
227 | |||
228 | $operator = $this->getValueAvailableFilters()[self::DEFAULT_OPERATOR]; |
||
229 | if(isset($filterAndOperator[1])){ |
||
230 | $operator = $this->getValueAvailableFilters()[$filterAndOperator[1]]; |
||
231 | } |
||
232 | |||
233 | // controllo se il filtro che mi arriva dalla richiesta è una proprietà di questa entità |
||
234 | // esempio per users: filtering[username|contains]=mado |
||
235 | if (in_array($fieldName, $this->fields)) { |
||
236 | |||
237 | $salt = ''; |
||
238 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
239 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
240 | $salt = '_' . rand(111, 999); |
||
241 | } |
||
242 | } |
||
243 | |||
244 | // filtering[foo|bar] |
||
245 | // $filterAndOperator[0] = 'foo' |
||
246 | // $filterAndOperator[1] = 'bar' |
||
247 | View Code Duplication | if (isset($filterAndOperator[1])) { |
|
248 | if ('list' == $filterAndOperator[1]) { |
||
249 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
250 | } else if ('field_eq' == $filterAndOperator[1]) { |
||
251 | $whereCondition = |
||
252 | $this->entityAlias . '.' . $fieldName . ' '. |
||
253 | $operator['meta'] . '' . |
||
254 | $this->entityAlias . '.' . $value |
||
255 | ; |
||
256 | //} else { |
||
257 | //throw new \RuntimeException( |
||
258 | //'Oops! Eccezzione' |
||
259 | //); |
||
260 | } else { |
||
261 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
262 | } |
||
263 | } else { |
||
264 | $whereCondition = $this->entityAlias.'.'.$fieldName.' = :field_'.$fieldName . $salt; |
||
265 | } |
||
266 | |||
267 | $this->qBuilder->andWhere($whereCondition); |
||
268 | |||
269 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
270 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
271 | $value = explode(',', $value); |
||
272 | } else { |
||
273 | $value = str_replace( |
||
274 | '{string}', |
||
275 | $value, |
||
276 | $operator['substitution_pattern'] |
||
277 | ); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | $this->qBuilder->setParameter('field_' . $fieldName . $salt, $value); |
||
282 | } else { |
||
283 | $isNotARelation = 0 !== strpos($fieldName, 'Embedded.'); |
||
284 | if ($isNotARelation) { |
||
285 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' ' . $this->entityAlias . '.' . $value; |
||
286 | $this->qBuilder->andWhere($whereCondition); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | // controllo se il filtro si riferisce ad una relazione dell'entità quindi devo fare dei join |
||
291 | // esempio per users: filtering[_embedded.groups.name|eq]=admin |
||
292 | if (strstr($filter, '_embedded.')) { |
||
293 | |||
294 | $this->join($filter); |
||
295 | $relationEntityAlias = $this->getRelationEntityAlias(); |
||
296 | |||
297 | $embeddedFields = explode('.', $fieldName); |
||
298 | $fieldName = $this->parser->camelize($embeddedFields[count($embeddedFields)-1]); |
||
299 | |||
300 | $salt = ''; |
||
301 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
302 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
303 | $salt = '_' . rand(111, 999); |
||
304 | } |
||
305 | } |
||
306 | |||
307 | View Code Duplication | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
|
308 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
309 | } else { |
||
310 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
311 | } |
||
312 | |||
313 | $this->qBuilder->andWhere($whereCondition); |
||
314 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
315 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
316 | $value = explode(',', $value); |
||
317 | } else { |
||
318 | $value = str_replace( |
||
319 | '{string}', |
||
320 | $value, |
||
321 | $operator['substitution_pattern'] |
||
322 | ); |
||
323 | } |
||
324 | } |
||
325 | |||
326 | $this->qBuilder->setParameter('field_' . $fieldName . $salt, $value); |
||
327 | } |
||
328 | } |
||
329 | |||
330 | private function applyFilterOr($filter, $value, $orCondition) |
||
331 | { |
||
332 | $whereCondition = null; |
||
333 | $filterAndOperator = explode('|',$filter); |
||
334 | |||
335 | $fieldName = $filterAndOperator[0]; |
||
336 | $fieldName = $this->parser->camelize($fieldName); |
||
337 | |||
338 | $operator = $this->getValueAvailableFilters()[self::DEFAULT_OPERATOR]; |
||
339 | if(isset($filterAndOperator[1])){ |
||
340 | $operator = $this->getValueAvailableFilters()[$filterAndOperator[1]]; |
||
341 | } |
||
342 | |||
343 | // controllo se il filtro che mi arriva dalla richiesta è una proprietà di questa entità |
||
344 | // esempio per users: filtering[username|contains]=mado |
||
345 | if (in_array($fieldName, $this->fields)) { |
||
346 | |||
347 | $salt = ''; |
||
348 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
349 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
350 | $salt = '_' . rand(111, 999); |
||
351 | } |
||
352 | } |
||
353 | |||
354 | if ($salt == '') { |
||
355 | $salt = '_' . rand(111, 999); |
||
356 | } |
||
357 | |||
358 | // filtering[foo|bar] |
||
359 | // $filterAndOperator[0] = 'foo' |
||
360 | // $filterAndOperator[1] = 'bar' |
||
361 | View Code Duplication | if (isset($filterAndOperator[1])) { |
|
362 | if ('list' == $filterAndOperator[1]) { |
||
363 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
364 | } else if ('field_eq' == $filterAndOperator[1]) { |
||
365 | $whereCondition = |
||
366 | $this->entityAlias . '.' . $fieldName . ' '. |
||
367 | $operator['meta'] . '' . |
||
368 | $this->entityAlias . '.' . $value |
||
369 | ; |
||
370 | //} else { |
||
371 | //throw new \RuntimeException( |
||
372 | //'Oops! Eccezzione' |
||
373 | //); |
||
374 | } else { |
||
375 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
376 | } |
||
377 | } else { |
||
378 | $whereCondition = $this->entityAlias.'.'.$fieldName.' = :field_'.$fieldName . $salt; |
||
379 | } |
||
380 | |||
381 | View Code Duplication | if ($orCondition['orCondition'] != null) { |
|
382 | $orCondition['orCondition'] .= ' OR ' . $whereCondition; |
||
383 | } else { |
||
384 | $orCondition['orCondition'] = $whereCondition; |
||
385 | } |
||
386 | |||
387 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
388 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
389 | $value = explode(',', $value); |
||
390 | } else { |
||
391 | $value = str_replace( |
||
392 | '{string}', |
||
393 | $value, |
||
394 | $operator['substitution_pattern'] |
||
395 | ); |
||
396 | } |
||
397 | } |
||
398 | |||
399 | $orCondition['parameters'][] = [ |
||
400 | 'field' => 'field_' . $fieldName . $salt, |
||
401 | 'value' => $value |
||
402 | ]; |
||
403 | } else { |
||
404 | $isNotARelation = 0 !== strpos($fieldName, 'Embedded.'); |
||
405 | if ($isNotARelation) { |
||
406 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' ' . $this->entityAlias . '.' . $value; |
||
407 | View Code Duplication | if ($orCondition['orCondition'] != null) { |
|
408 | $orCondition['orCondition'] .= ' OR ' . $whereCondition; |
||
409 | } else { |
||
410 | $orCondition['orCondition'] = $whereCondition; |
||
411 | } |
||
412 | } |
||
413 | } |
||
414 | |||
415 | // controllo se il filtro si riferisce ad una relazione dell'entità quindi devo fare dei join |
||
416 | // esempio per users: filtering[_embedded.groups.name|eq]=admin |
||
417 | if (strstr($filter, '_embedded.')) { |
||
418 | |||
419 | $this->join($filter); |
||
420 | $relationEntityAlias = $this->getRelationEntityAlias(); |
||
421 | |||
422 | $embeddedFields = explode('.', $fieldName); |
||
423 | $fieldName = $this->parser->camelize($embeddedFields[count($embeddedFields)-1]); |
||
424 | |||
425 | $salt = ''; |
||
426 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
427 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
428 | $salt = '_' . rand(111, 999); |
||
429 | } |
||
430 | } |
||
431 | |||
432 | if ($salt == '') { |
||
433 | $salt = '_' . rand(111, 999); |
||
434 | } |
||
435 | |||
436 | View Code Duplication | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
|
437 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
438 | } else { |
||
439 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
440 | } |
||
441 | |||
442 | View Code Duplication | if ($orCondition['orCondition'] != null) { |
|
443 | $orCondition['orCondition'] .= ' OR ' . $whereCondition; |
||
444 | } else { |
||
445 | $orCondition['orCondition'] = $whereCondition; |
||
446 | } |
||
447 | |||
448 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
449 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
450 | $value = explode(',', $value); |
||
451 | } else { |
||
452 | $value = str_replace( |
||
453 | '{string}', |
||
454 | $value, |
||
455 | $operator['substitution_pattern'] |
||
456 | ); |
||
457 | } |
||
458 | } |
||
459 | |||
460 | $orCondition['parameters'][] = [ |
||
461 | 'field' => 'field_' . $fieldName . $salt, |
||
462 | 'value' => $value |
||
463 | ]; |
||
464 | } |
||
465 | |||
466 | return $orCondition; |
||
467 | } |
||
468 | |||
469 | public function sort() |
||
470 | { |
||
471 | if (!$this->fields) { |
||
472 | throw new \RuntimeException( |
||
473 | 'Oops! Fields are not defined' |
||
474 | ); |
||
475 | } |
||
476 | |||
477 | if (null === $this->sorting) { |
||
478 | throw new \RuntimeException( |
||
479 | 'Oops! Sorting is not defined' |
||
480 | ); |
||
481 | } |
||
482 | |||
483 | foreach ($this->sorting as $sort => $val) { |
||
484 | $val = strtolower($val); |
||
485 | |||
486 | $fieldName = $this->parser->camelize($sort); |
||
487 | |||
488 | if (in_array($fieldName, $this->fields)) { |
||
489 | $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
||
490 | $this->qBuilder->addOrderBy($this->entityAlias .'.'. $fieldName, $direction); |
||
491 | } |
||
492 | |||
493 | if (strstr($sort, '_embedded.')) { |
||
494 | $this->join($sort); |
||
495 | $relationEntityAlias = $this->getRelationEntityAlias(); |
||
496 | |||
497 | $embeddedFields = explode('.', $sort); |
||
498 | $fieldName = $this->parser->camelize($embeddedFields[2]); |
||
499 | $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
||
500 | |||
501 | $this->qBuilder->addOrderBy($relationEntityAlias.'.'.$fieldName, $direction); |
||
502 | } |
||
503 | |||
504 | } |
||
505 | |||
506 | return $this; |
||
507 | } |
||
508 | |||
509 | public function getQueryBuilder() |
||
510 | { |
||
511 | if (!$this->qBuilder) { |
||
512 | throw new \RuntimeException( |
||
513 | "Oops! Query builder was never initialized! call ::createQueryBuilder('entityName', 'alias') to start." |
||
514 | ); |
||
515 | } |
||
516 | |||
517 | return $this->qBuilder; |
||
518 | } |
||
519 | |||
520 | public function buildSelectValue() : string |
||
521 | { |
||
522 | if ("" == $this->getSelect()) { |
||
523 | return $this->getEntityAlias( |
||
524 | $this->getEntityName() |
||
525 | ); |
||
526 | } |
||
527 | |||
528 | return $this->getSelect(); |
||
529 | } |
||
530 | |||
531 | private function setRelationEntityAlias(string $relationEntityAlias) |
||
532 | { |
||
533 | $this->relationEntityAlias = $relationEntityAlias; |
||
534 | } |
||
535 | |||
536 | private function getRelationEntityAlias() |
||
537 | { |
||
538 | return $this->relationEntityAlias; |
||
539 | } |
||
540 | |||
541 | public function setRel($rel) |
||
542 | { |
||
543 | $this->rel = $rel; |
||
544 | |||
545 | return $this; |
||
546 | } |
||
547 | |||
548 | public function getRel() |
||
549 | { |
||
550 | return $this->rel; |
||
551 | } |
||
552 | |||
553 | public function setPrinting($printing) |
||
554 | { |
||
555 | $this->printing = $printing; |
||
556 | |||
557 | return $this; |
||
558 | } |
||
559 | |||
560 | public function getPrinting() |
||
563 | } |
||
564 | |||
565 | public function setPage($page) |
||
566 | { |
||
567 | $this->page = $page; |
||
568 | |||
569 | return $this; |
||
570 | } |
||
571 | |||
572 | public function getPage() |
||
573 | { |
||
574 | return $this->page; |
||
575 | } |
||
576 | |||
577 | public function setPageLength($pageLength) |
||
578 | { |
||
579 | $this->pageLength = $pageLength; |
||
580 | |||
581 | return $this; |
||
582 | } |
||
583 | |||
584 | public function getPageLength() |
||
585 | { |
||
586 | return $this->pageLength; |
||
587 | } |
||
588 | |||
589 | public function setSelect( $select) : QueryBuilderFactory |
||
590 | { |
||
591 | $this->select = $select; |
||
592 | |||
593 | return $this; |
||
594 | } |
||
595 | |||
596 | public function getSelect() |
||
599 | } |
||
600 | |||
601 | public function getEntityManager() |
||
602 | { |
||
603 | return $this->manager; |
||
604 | } |
||
605 | |||
606 | public function setConfigProvider(ConfigProvider $configProvider) |
||
609 | } |
||
610 | } |
||
611 |