Total Complexity | 99 |
Total Lines | 602 |
Duplicated Lines | 21.1 % |
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) |
||
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 | // il config provider deve fornire anche le aclp |
||
168 | } |
||
169 | |||
170 | $this->storeJoin($relationEntityAlias, $relation); |
||
171 | } |
||
172 | $entityName = $association['targetEntity']; |
||
173 | $entityAlias = $relationEntityAlias; |
||
174 | } |
||
175 | |||
176 | $this->setRelationEntityAlias($relationEntityAlias); |
||
177 | } |
||
178 | |||
179 | return $this; |
||
180 | } |
||
181 | |||
182 | public function filter() |
||
183 | { |
||
184 | if (null === $this->filtering) { |
||
185 | throw new \RuntimeException( |
||
186 | 'Oops! Filtering is not defined' |
||
187 | ); |
||
188 | } |
||
189 | |||
190 | if (!$this->fields) { |
||
191 | throw new \RuntimeException( |
||
192 | 'Oops! Fields are not defined' |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | foreach ($this->filtering as $filter => $value) { |
||
197 | $this->applyFilterAnd($filter, $value); |
||
198 | } |
||
199 | |||
200 | if (null !== $this->orFiltering) { |
||
201 | $orFilter = []; |
||
202 | $orFilter['orCondition'] = null; |
||
203 | $orFilter['parameters'] = []; |
||
204 | |||
205 | foreach ($this->orFiltering as $filter => $value) { |
||
206 | $orFilter = $this->applyFilterOr($filter, $value, $orFilter); |
||
207 | } |
||
208 | |||
209 | if ((count($orFilter) > 0) && ($orFilter['orCondition'] != null)) { |
||
210 | $this->qBuilder->andWhere($orFilter['orCondition']); |
||
211 | |||
212 | foreach ($orFilter['parameters'] as $parameter) { |
||
213 | $this->qBuilder->setParameter($parameter['field'], $parameter['value']); |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | return $this; |
||
219 | } |
||
220 | |||
221 | private function applyFilterAnd($filter, $value) |
||
222 | { |
||
223 | $whereCondition = null; |
||
224 | $filterAndOperator = explode('|',$filter); |
||
225 | |||
226 | $fieldName = $filterAndOperator[0]; |
||
227 | $fieldName = $this->parser->camelize($fieldName); |
||
228 | |||
229 | $operator = $this->getValueAvailableFilters()[self::DEFAULT_OPERATOR]; |
||
230 | if(isset($filterAndOperator[1])){ |
||
231 | $operator = $this->getValueAvailableFilters()[$filterAndOperator[1]]; |
||
232 | } |
||
233 | |||
234 | // controllo se il filtro che mi arriva dalla richiesta è una proprietà di questa entità |
||
235 | // esempio per users: filtering[username|contains]=mado |
||
236 | if (in_array($fieldName, $this->fields)) { |
||
237 | |||
238 | $salt = ''; |
||
239 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
240 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
241 | $salt = '_' . rand(111, 999); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | // filtering[foo|bar] |
||
246 | // $filterAndOperator[0] = 'foo' |
||
247 | // $filterAndOperator[1] = 'bar' |
||
248 | View Code Duplication | if (isset($filterAndOperator[1])) { |
|
249 | if ('list' == $filterAndOperator[1]) { |
||
250 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
251 | } else if ('field_eq' == $filterAndOperator[1]) { |
||
252 | $whereCondition = |
||
253 | $this->entityAlias . '.' . $fieldName . ' '. |
||
254 | $operator['meta'] . '' . |
||
255 | $this->entityAlias . '.' . $value |
||
256 | ; |
||
257 | //} else { |
||
258 | //throw new \RuntimeException( |
||
259 | //'Oops! Eccezzione' |
||
260 | //); |
||
261 | } else { |
||
262 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
263 | } |
||
264 | } else { |
||
265 | $whereCondition = $this->entityAlias.'.'.$fieldName.' = :field_'.$fieldName . $salt; |
||
266 | } |
||
267 | |||
268 | $this->qBuilder->andWhere($whereCondition); |
||
269 | |||
270 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
271 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
272 | $value = explode(',', $value); |
||
273 | } else { |
||
274 | $value = str_replace( |
||
275 | '{string}', |
||
276 | $value, |
||
277 | $operator['substitution_pattern'] |
||
278 | ); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | $this->qBuilder->setParameter('field_' . $fieldName . $salt, $value); |
||
283 | } else { |
||
284 | $isNotARelation = 0 !== strpos($fieldName, 'Embedded.'); |
||
285 | if ($isNotARelation) { |
||
286 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' ' . $this->entityAlias . '.' . $value; |
||
287 | $this->qBuilder->andWhere($whereCondition); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | // controllo se il filtro si riferisce ad una relazione dell'entità quindi devo fare dei join |
||
292 | // esempio per users: filtering[_embedded.groups.name|eq]=admin |
||
293 | if (strstr($filter, '_embedded.')) { |
||
294 | |||
295 | $this->join($filter); |
||
296 | $relationEntityAlias = $this->getRelationEntityAlias(); |
||
297 | |||
298 | $embeddedFields = explode('.', $fieldName); |
||
299 | $fieldName = $this->parser->camelize($embeddedFields[count($embeddedFields)-1]); |
||
300 | |||
301 | $salt = ''; |
||
302 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
303 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
304 | $salt = '_' . rand(111, 999); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | View Code Duplication | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
|
309 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
310 | } else { |
||
311 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
312 | } |
||
313 | |||
314 | $this->qBuilder->andWhere($whereCondition); |
||
315 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
316 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
317 | $value = explode(',', $value); |
||
318 | } else { |
||
319 | $value = str_replace( |
||
320 | '{string}', |
||
321 | $value, |
||
322 | $operator['substitution_pattern'] |
||
323 | ); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | $this->qBuilder->setParameter('field_' . $fieldName . $salt, $value); |
||
328 | } |
||
329 | } |
||
330 | |||
331 | private function applyFilterOr($filter, $value, $orCondition) |
||
332 | { |
||
333 | $whereCondition = null; |
||
334 | $filterAndOperator = explode('|',$filter); |
||
335 | |||
336 | $fieldName = $filterAndOperator[0]; |
||
337 | $fieldName = $this->parser->camelize($fieldName); |
||
338 | |||
339 | $operator = $this->getValueAvailableFilters()[self::DEFAULT_OPERATOR]; |
||
340 | if(isset($filterAndOperator[1])){ |
||
341 | $operator = $this->getValueAvailableFilters()[$filterAndOperator[1]]; |
||
342 | } |
||
343 | |||
344 | // controllo se il filtro che mi arriva dalla richiesta è una proprietà di questa entità |
||
345 | // esempio per users: filtering[username|contains]=mado |
||
346 | if (in_array($fieldName, $this->fields)) { |
||
347 | |||
348 | $salt = ''; |
||
349 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
350 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
351 | $salt = '_' . rand(111, 999); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | if ($salt == '') { |
||
356 | $salt = '_' . rand(111, 999); |
||
357 | } |
||
358 | |||
359 | // filtering[foo|bar] |
||
360 | // $filterAndOperator[0] = 'foo' |
||
361 | // $filterAndOperator[1] = 'bar' |
||
362 | View Code Duplication | if (isset($filterAndOperator[1])) { |
|
363 | if ('list' == $filterAndOperator[1]) { |
||
364 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
365 | } else if ('field_eq' == $filterAndOperator[1]) { |
||
366 | $whereCondition = |
||
367 | $this->entityAlias . '.' . $fieldName . ' '. |
||
368 | $operator['meta'] . '' . |
||
369 | $this->entityAlias . '.' . $value |
||
370 | ; |
||
371 | //} else { |
||
372 | //throw new \RuntimeException( |
||
373 | //'Oops! Eccezzione' |
||
374 | //); |
||
375 | } else { |
||
376 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
377 | } |
||
378 | } else { |
||
379 | $whereCondition = $this->entityAlias.'.'.$fieldName.' = :field_'.$fieldName . $salt; |
||
380 | } |
||
381 | |||
382 | View Code Duplication | if ($orCondition['orCondition'] != null) { |
|
383 | $orCondition['orCondition'] .= ' OR ' . $whereCondition; |
||
384 | } else { |
||
385 | $orCondition['orCondition'] = $whereCondition; |
||
386 | } |
||
387 | |||
388 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
389 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
390 | $value = explode(',', $value); |
||
391 | } else { |
||
392 | $value = str_replace( |
||
393 | '{string}', |
||
394 | $value, |
||
395 | $operator['substitution_pattern'] |
||
396 | ); |
||
397 | } |
||
398 | } |
||
399 | |||
400 | $orCondition['parameters'][] = [ |
||
401 | 'field' => 'field_' . $fieldName . $salt, |
||
402 | 'value' => $value |
||
403 | ]; |
||
404 | } else { |
||
405 | $isNotARelation = 0 !== strpos($fieldName, 'Embedded.'); |
||
406 | if ($isNotARelation) { |
||
407 | $whereCondition = $this->entityAlias.'.'.$fieldName.' '.$operator['meta'].' ' . $this->entityAlias . '.' . $value; |
||
408 | View Code Duplication | if ($orCondition['orCondition'] != null) { |
|
409 | $orCondition['orCondition'] .= ' OR ' . $whereCondition; |
||
410 | } else { |
||
411 | $orCondition['orCondition'] = $whereCondition; |
||
412 | } |
||
413 | } |
||
414 | } |
||
415 | |||
416 | // controllo se il filtro si riferisce ad una relazione dell'entità quindi devo fare dei join |
||
417 | // esempio per users: filtering[_embedded.groups.name|eq]=admin |
||
418 | if (strstr($filter, '_embedded.')) { |
||
419 | |||
420 | $this->join($filter); |
||
421 | $relationEntityAlias = $this->getRelationEntityAlias(); |
||
422 | |||
423 | $embeddedFields = explode('.', $fieldName); |
||
424 | $fieldName = $this->parser->camelize($embeddedFields[count($embeddedFields)-1]); |
||
425 | |||
426 | $salt = ''; |
||
427 | View Code Duplication | foreach ($this->qBuilder->getParameters() as $parameter) { |
|
428 | if ($parameter->getName() == 'field_' . $fieldName) { |
||
429 | $salt = '_' . rand(111, 999); |
||
430 | } |
||
431 | } |
||
432 | |||
433 | if ($salt == '') { |
||
434 | $salt = '_' . rand(111, 999); |
||
435 | } |
||
436 | |||
437 | View Code Duplication | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
|
438 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' (:field_'.$fieldName . $salt . ')'; |
||
439 | } else { |
||
440 | $whereCondition = $relationEntityAlias.'.'.$fieldName.' '.$operator['meta'].' :field_'.$fieldName . $salt; |
||
441 | } |
||
442 | |||
443 | View Code Duplication | if ($orCondition['orCondition'] != null) { |
|
444 | $orCondition['orCondition'] .= ' OR ' . $whereCondition; |
||
445 | } else { |
||
446 | $orCondition['orCondition'] = $whereCondition; |
||
447 | } |
||
448 | |||
449 | View Code Duplication | if (isset($operator['substitution_pattern'])) { |
|
450 | if (isset($filterAndOperator[1]) && 'list' == $filterAndOperator[1]) { |
||
451 | $value = explode(',', $value); |
||
452 | } else { |
||
453 | $value = str_replace( |
||
454 | '{string}', |
||
455 | $value, |
||
456 | $operator['substitution_pattern'] |
||
457 | ); |
||
458 | } |
||
459 | } |
||
460 | |||
461 | $orCondition['parameters'][] = [ |
||
462 | 'field' => 'field_' . $fieldName . $salt, |
||
463 | 'value' => $value |
||
464 | ]; |
||
465 | } |
||
466 | |||
467 | return $orCondition; |
||
468 | } |
||
469 | |||
470 | public function sort() |
||
471 | { |
||
472 | if (!$this->fields) { |
||
473 | throw new \RuntimeException( |
||
474 | 'Oops! Fields are not defined' |
||
475 | ); |
||
476 | } |
||
477 | |||
478 | if (null === $this->sorting) { |
||
479 | throw new \RuntimeException( |
||
480 | 'Oops! Sorting is not defined' |
||
481 | ); |
||
482 | } |
||
483 | |||
484 | foreach ($this->sorting as $sort => $val) { |
||
485 | $val = strtolower($val); |
||
486 | |||
487 | $fieldName = $this->parser->camelize($sort); |
||
488 | |||
489 | if (in_array($fieldName, $this->fields)) { |
||
490 | $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
||
491 | $this->qBuilder->addOrderBy($this->entityAlias .'.'. $fieldName, $direction); |
||
492 | } |
||
493 | |||
494 | if (strstr($sort, '_embedded.')) { |
||
495 | $this->join($sort); |
||
496 | $relationEntityAlias = $this->getRelationEntityAlias(); |
||
497 | |||
498 | $embeddedFields = explode('.', $sort); |
||
499 | $fieldName = $this->parser->camelize($embeddedFields[2]); |
||
500 | $direction = ($val === self::DIRECTION_AZ) ? self::DIRECTION_AZ : self::DIRECTION_ZA; |
||
501 | |||
502 | $this->qBuilder->addOrderBy($relationEntityAlias.'.'.$fieldName, $direction); |
||
503 | } |
||
504 | |||
505 | } |
||
506 | |||
507 | return $this; |
||
508 | } |
||
509 | |||
510 | public function getQueryBuilder() |
||
511 | { |
||
512 | if (!$this->qBuilder) { |
||
513 | throw new \RuntimeException( |
||
514 | "Oops! Query builder was never initialized! call ::createQueryBuilder('entityName', 'alias') to start." |
||
515 | ); |
||
516 | } |
||
517 | |||
518 | return $this->qBuilder; |
||
519 | } |
||
520 | |||
521 | public function buildSelectValue() : string |
||
522 | { |
||
523 | if ("" == $this->getSelect()) { |
||
524 | return $this->getEntityAlias( |
||
525 | $this->getEntityName() |
||
526 | ); |
||
527 | } |
||
528 | |||
529 | return $this->getSelect(); |
||
530 | } |
||
531 | |||
532 | private function setRelationEntityAlias(string $relationEntityAlias) |
||
533 | { |
||
534 | $this->relationEntityAlias = $relationEntityAlias; |
||
535 | } |
||
536 | |||
537 | private function getRelationEntityAlias() |
||
538 | { |
||
539 | return $this->relationEntityAlias; |
||
540 | } |
||
541 | |||
542 | public function setRel($rel) |
||
543 | { |
||
544 | $this->rel = $rel; |
||
545 | |||
546 | return $this; |
||
547 | } |
||
548 | |||
549 | public function getRel() |
||
550 | { |
||
551 | return $this->rel; |
||
552 | } |
||
553 | |||
554 | public function setPrinting($printing) |
||
555 | { |
||
556 | $this->printing = $printing; |
||
557 | |||
558 | return $this; |
||
559 | } |
||
560 | |||
561 | public function getPrinting() |
||
564 | } |
||
565 | |||
566 | public function setPage($page) |
||
567 | { |
||
568 | $this->page = $page; |
||
569 | |||
570 | return $this; |
||
571 | } |
||
572 | |||
573 | public function getPage() |
||
574 | { |
||
575 | return $this->page; |
||
576 | } |
||
577 | |||
578 | public function setPageLength($pageLength) |
||
579 | { |
||
580 | $this->pageLength = $pageLength; |
||
581 | |||
582 | return $this; |
||
583 | } |
||
584 | |||
585 | public function getPageLength() |
||
586 | { |
||
587 | return $this->pageLength; |
||
588 | } |
||
589 | |||
590 | public function setSelect( $select) : QueryBuilderFactory |
||
591 | { |
||
592 | $this->select = $select; |
||
593 | |||
594 | return $this; |
||
595 | } |
||
596 | |||
597 | public function getSelect() |
||
600 | } |
||
601 | |||
602 | public function getEntityManager() |
||
603 | { |
||
604 | return $this->manager; |
||
605 | } |
||
606 | |||
607 | public function setConfigProvider(ConfigProvider $configProvider) |
||
610 | } |
||
611 | } |
||
612 |