Total Complexity | 52 |
Total Lines | 508 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractQueryBuilder 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 AbstractQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | abstract class AbstractQueryBuilder { |
||
50 | |||
51 | /** |
||
52 | * @var Query |
||
53 | */ |
||
54 | protected $queryToBuild = null; |
||
55 | |||
56 | /** |
||
57 | * @param Query $query |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function startFrom(Query $query) |
||
61 | { |
||
62 | $this->queryToBuild = $query; |
||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return Query |
||
68 | */ |
||
69 | public function getQuery(): Query |
||
70 | { |
||
71 | return $this->queryToBuild; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param bool $omitHeader |
||
76 | * @return $this |
||
77 | */ |
||
78 | public function useOmitHeader($omitHeader = true) |
||
79 | { |
||
80 | $this->queryToBuild->setOmitHeader($omitHeader); |
||
81 | |||
82 | return $this; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Uses an array of filters and applies them to the query. |
||
87 | * |
||
88 | * @param array $filterArray |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function useFilterArray(array $filterArray) |
||
92 | { |
||
93 | foreach ($filterArray as $key => $additionalFilter) { |
||
94 | $this->useFilter($additionalFilter, $key); |
||
95 | } |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Applies the queryString that is used to search |
||
102 | * |
||
103 | * @param string $queryString |
||
104 | * @return $this |
||
105 | */ |
||
106 | public function useQueryString($queryString) |
||
107 | { |
||
108 | $this->queryToBuild->setQuery($queryString); |
||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Applies the passed queryType to the query. |
||
114 | * |
||
115 | * @param string $queryType |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function useQueryType(string $queryType) |
||
119 | { |
||
120 | $this->queryToBuild->addParam('qt', $queryType); |
||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Remove the queryType (qt) from the query. |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function removeQueryType() |
||
130 | { |
||
131 | $this->queryToBuild->addParam('qt', null); |
||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Can be used to remove all sortings from the query. |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function removeAllSortings() |
||
141 | { |
||
142 | $this->queryToBuild->clearSorts(); |
||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Applies the passed sorting to the query. |
||
148 | * |
||
149 | * @param Sorting $sorting |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function useSorting(Sorting $sorting) |
||
153 | { |
||
154 | if (strpos($sorting->getFieldName(), 'relevance') !== false) { |
||
155 | $this->removeAllSortings(); |
||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | $this->queryToBuild->addSort($sorting->getFieldName(), $sorting->getDirection()); |
||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Applies the passed sorting to the query. |
||
165 | * |
||
166 | * @param Sortings $sortings |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function useSortings(Sortings $sortings) |
||
170 | { |
||
171 | foreach($sortings->getSortings() as $sorting) { |
||
172 | $this->useSorting($sorting); |
||
173 | } |
||
174 | |||
175 | return $this; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param int $resultsPerPage |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function useResultsPerPage($resultsPerPage) |
||
183 | { |
||
184 | $this->queryToBuild->setRows($resultsPerPage); |
||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param int $page |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function usePage($page) |
||
193 | { |
||
194 | $this->queryToBuild->setStart($page); |
||
195 | return $this; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param Operator $operator |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function useOperator(Operator $operator) |
||
203 | { |
||
204 | $this->queryToBuild->setQueryDefaultOperator( $operator->getOperator()); |
||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Remove the default query operator. |
||
210 | * |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function removeOperator() |
||
214 | { |
||
215 | $this->queryToBuild->setQueryDefaultOperator(null); |
||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param Slops $slops |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function useSlops(Slops $slops) |
||
224 | { |
||
225 | return $slops->build($this); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Uses the passed boostQuer(y|ies) for the query. |
||
230 | * |
||
231 | * @param string|array $boostQueries |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function useBoostQueries($boostQueries) |
||
235 | { |
||
236 | $boostQueryArray = []; |
||
237 | if(is_array($boostQueries)) { |
||
238 | foreach($boostQueries as $boostQuery) { |
||
239 | $boostQueryArray[] = ['key' => md5($boostQuery), 'query' => $boostQuery]; |
||
240 | } |
||
241 | } else { |
||
242 | $boostQueryArray[] = ['key' => md5($boostQueries), 'query' => $boostQueries]; |
||
243 | } |
||
244 | |||
245 | $this->queryToBuild->getEDisMax()->setBoostQueries($boostQueryArray); |
||
246 | return $this; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Removes all boost queries from the query. |
||
251 | * |
||
252 | * @return $this |
||
253 | */ |
||
254 | public function removeAllBoostQueries() |
||
255 | { |
||
256 | $this->queryToBuild->getEDisMax()->clearBoostQueries(); |
||
257 | return $this; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Uses the passed boostFunction for the query. |
||
262 | * |
||
263 | * @param string $boostFunction |
||
264 | * @return $this |
||
265 | */ |
||
266 | public function useBoostFunction(string $boostFunction) |
||
267 | { |
||
268 | $this->queryToBuild->getEDisMax()->setBoostFunctions($boostFunction); |
||
269 | return $this; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Removes all previously configured boost functions. |
||
274 | * |
||
275 | * @return $this |
||
276 | */ |
||
277 | public function removeAllBoostFunctions() |
||
278 | { |
||
279 | $this->queryToBuild->getEDisMax()->setBoostFunctions(null); |
||
280 | return $this; |
||
281 | } |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Uses the passed minimumMatch(mm) for the query. |
||
286 | * |
||
287 | * @param string $minimumMatch |
||
288 | * @return $this |
||
289 | */ |
||
290 | public function useMinimumMatch(string $minimumMatch) |
||
291 | { |
||
292 | $this->queryToBuild->getEDisMax()->setMinimumMatch($minimumMatch); |
||
293 | return $this; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Remove any previous passed minimumMatch parameter. |
||
298 | * |
||
299 | * @return $this |
||
300 | */ |
||
301 | public function removeMinimumMatch() |
||
302 | { |
||
303 | $this->queryToBuild->getEDisMax()->setMinimumMatch(null); |
||
304 | return $this; |
||
305 | } |
||
306 | |||
307 | |||
308 | /** |
||
309 | * Applies the tie parameter to the query. |
||
310 | * |
||
311 | * @param mixed $tie |
||
312 | * @return $this |
||
313 | */ |
||
314 | public function useTieParameter($tie) |
||
315 | { |
||
316 | $this->queryToBuild->getEDisMax()->setTie($tie); |
||
317 | return $this; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Applies custom QueryFields to the query. |
||
322 | * |
||
323 | * @param QueryFields $queryFields |
||
324 | * @return $this |
||
325 | */ |
||
326 | public function useQueryFields(QueryFields $queryFields) |
||
327 | { |
||
328 | return $queryFields->build($this); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Applies custom ReturnFields to the query. |
||
333 | * |
||
334 | * @param ReturnFields $returnFields |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function useReturnFields(ReturnFields $returnFields) |
||
338 | { |
||
339 | return $returnFields->build($this); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Can be used to use a specific filter string in the solr query. |
||
344 | * |
||
345 | * @param string $filterString |
||
346 | * @param string $filterName |
||
347 | * @return $this |
||
348 | */ |
||
349 | public function useFilter($filterString, $filterName = '') |
||
350 | { |
||
351 | $filterName = $filterName === '' ? $filterString : $filterName; |
||
352 | |||
353 | $nameWasPassedAndFilterIsAllreadySet = $filterName !== '' && $this->queryToBuild->getFilterQuery($filterName) !== null; |
||
354 | if($nameWasPassedAndFilterIsAllreadySet) { |
||
355 | return $this; |
||
356 | } |
||
357 | $this->queryToBuild->addFilterQuery(['key' => $filterName, 'query' => $filterString]); |
||
358 | return $this; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Removes a filter by the fieldName. |
||
363 | * |
||
364 | * @param string $fieldName |
||
365 | * @return $this |
||
366 | */ |
||
367 | public function removeFilterByFieldName($fieldName) |
||
368 | { |
||
369 | return $this->removeFilterByFunction( |
||
370 | function($key, $query) use ($fieldName) { |
||
371 | $queryString = $query->getQuery(); |
||
372 | $storedFieldName = substr($queryString,0, strpos($queryString, ":")); |
||
373 | return $storedFieldName == $fieldName; |
||
374 | } |
||
375 | ); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Removes a filter by the name of the filter (also known as key). |
||
380 | * |
||
381 | * @param string $name |
||
382 | * @return $this |
||
383 | */ |
||
384 | public function removeFilterByName($name) |
||
385 | { |
||
386 | return $this->removeFilterByFunction( |
||
387 | function($key, $query) use ($name) { |
||
388 | $key = $query->getKey(); |
||
389 | return $key == $name; |
||
390 | } |
||
391 | ); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Removes a filter by the filter value. |
||
396 | * |
||
397 | * @param string $value |
||
398 | * @return $this |
||
399 | */ |
||
400 | public function removeFilterByValue($value) |
||
401 | { |
||
402 | return $this->removeFilterByFunction( |
||
403 | function($key, $query) use ($value) { |
||
404 | $query = $query->getQuery(); |
||
405 | return $query == $value; |
||
406 | } |
||
407 | ); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @param \Closure $filterFunction |
||
412 | * @return $this |
||
413 | */ |
||
414 | public function removeFilterByFunction($filterFunction) |
||
415 | { |
||
416 | $queries = $this->queryToBuild->getFilterQueries(); |
||
417 | foreach($queries as $key => $query) { |
||
418 | $canBeRemoved = $filterFunction($key, $query); |
||
419 | if($canBeRemoved) { |
||
420 | unset($queries[$key]); |
||
421 | } |
||
422 | } |
||
423 | |||
424 | $this->queryToBuild->setFilterQueries($queries); |
||
425 | return $this; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Passes the alternative query to the Query |
||
430 | * @param string $query |
||
431 | * @return $this |
||
432 | */ |
||
433 | public function useAlternativeQuery(string $query) |
||
434 | { |
||
435 | $this->queryToBuild->getEDisMax()->setQueryAlternative($query); |
||
436 | return $this; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Remove the alternative query from the Query. |
||
441 | * |
||
442 | * @return $this |
||
443 | */ |
||
444 | public function removeAlternativeQuery() |
||
445 | { |
||
446 | $this->queryToBuild->getEDisMax()->setQueryAlternative(null); |
||
447 | return $this; |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * Applies a custom Faceting configuration to the query. |
||
452 | * |
||
453 | * @param Faceting $faceting |
||
454 | * @return $this |
||
455 | */ |
||
456 | public function useFaceting(Faceting $faceting) |
||
457 | { |
||
458 | return $faceting->build($this); |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * @param FieldCollapsing $fieldCollapsing |
||
463 | * @return $this |
||
464 | */ |
||
465 | public function useFieldCollapsing(FieldCollapsing $fieldCollapsing) |
||
466 | { |
||
467 | return $fieldCollapsing->build($this); |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Applies a custom initialized grouping to the query. |
||
472 | * |
||
473 | * @param Grouping $grouping |
||
474 | * @return $this |
||
475 | */ |
||
476 | public function useGrouping(Grouping $grouping) |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * @param Highlighting $highlighting |
||
483 | * @return $this |
||
484 | */ |
||
485 | public function useHighlighting(Highlighting $highlighting) |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * @param boolean $debugMode |
||
492 | * @return $this |
||
493 | */ |
||
494 | public function useDebug($debugMode) |
||
495 | { |
||
496 | if (!$debugMode) { |
||
497 | $this->queryToBuild->addParam('debugQuery', null); |
||
498 | $this->queryToBuild->addParam('echoParams', null); |
||
499 | return $this; |
||
500 | } |
||
501 | |||
502 | $this->queryToBuild->addParam('debugQuery', 'true'); |
||
503 | $this->queryToBuild->addParam('echoParams', 'all'); |
||
504 | |||
505 | return $this; |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * @param Elevation $elevation |
||
510 | * @return QueryBuilder |
||
511 | */ |
||
512 | public function useElevation(Elevation $elevation) |
||
513 | { |
||
514 | return $elevation->build($this); |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * @param Spellchecking $spellchecking |
||
519 | * @return $this |
||
520 | */ |
||
521 | public function useSpellchecking(Spellchecking $spellchecking) |
||
522 | { |
||
523 | return $spellchecking->build($this); |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Applies a custom configured PhraseFields to the query. |
||
528 | * |
||
529 | * @param PhraseFields $phraseFields |
||
530 | * @return $this |
||
531 | */ |
||
532 | public function usePhraseFields(PhraseFields $phraseFields) |
||
533 | { |
||
534 | return $phraseFields->build($this); |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Applies a custom configured BigramPhraseFields to the query. |
||
539 | * |
||
540 | * @param BigramPhraseFields $bigramPhraseFields |
||
541 | * @return $this |
||
542 | */ |
||
543 | public function useBigramPhraseFields(BigramPhraseFields $bigramPhraseFields) |
||
544 | { |
||
545 | return $bigramPhraseFields->build($this); |
||
546 | } |
||
547 | |||
548 | /** |
||
549 | * Applies a custom configured TrigramPhraseFields to the query. |
||
550 | * |
||
551 | * @param TrigramPhraseFields $trigramPhraseFields |
||
552 | * @return $this |
||
553 | */ |
||
554 | public function useTrigramPhraseFields(TrigramPhraseFields $trigramPhraseFields) |
||
557 | } |
||
558 | } |
||
559 |