Total Complexity | 41 |
Total Lines | 224 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CustomQueryBuilder 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 CustomQueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class CustomQueryBuilder |
||
6 | { |
||
7 | private $relation_counter = 0; |
||
8 | public function apply($query, $data) { |
||
9 | $keyword = request('q'); |
||
10 | $data_filters = $data['allowedFilters']; |
||
11 | |||
12 | $filter_prep = [ |
||
13 | 'column' => '', |
||
14 | 'operator' => 'contains', |
||
15 | 'query_1' => '', |
||
16 | 'match' => 'or', |
||
17 | ]; |
||
18 | |||
19 | foreach ($data_filters as $filter) { |
||
20 | $filter_prep['column'] = $filter; |
||
21 | $filter_prep['query_1'] = $keyword; |
||
22 | $this->makeFilter($query, $filter_prep); |
||
23 | } |
||
24 | |||
25 | if(isset($data['f'])) { |
||
26 | foreach ($data['f'] as $filter) { |
||
27 | $filter['match'] = $data['filter_match'] ?? 'and'; |
||
28 | $this->makeFilter($query, $filter); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | // dd($this->relation_counter); |
||
33 | return $query; |
||
34 | } |
||
35 | |||
36 | public function makeFilter($query, $filter) { |
||
37 | if(strpos($filter['column'], '.') !== false) { |
||
38 | // nested column |
||
39 | list($relation, $filter['column']) = explode('.', $filter['column']); |
||
40 | $filter['match'] = 'and'; |
||
41 | // $filter['match'] = 'or'; |
||
42 | |||
43 | if($filter['column'] == 'count') { |
||
44 | $this->{camel_case($filter['operator'])}($filter, $query, $relation); |
||
45 | |||
46 | } else { |
||
47 | if($this->relation_counter == 0) { |
||
48 | $query->whereHas($relation, function($q) use ($filter) { |
||
49 | $this->{camel_case($filter['operator'])}($filter, $q); |
||
50 | }); |
||
51 | } else { |
||
52 | $query->orWhereHas($relation, function($q) use ($filter) { |
||
53 | $this->{camel_case($filter['operator'])}($filter, $q); |
||
54 | }); |
||
55 | } |
||
56 | $this->relation_counter++; |
||
57 | } |
||
58 | } else { |
||
59 | $this->relation_counter++; |
||
60 | // normal column |
||
61 | $this->{camel_case($filter['operator'])}($filter, $query); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | public function equalTo($filter, $query) { |
||
66 | return $query->where($filter['column'], '=', $filter['query_1'], $filter['match']); |
||
67 | } |
||
68 | |||
69 | public function notEqualTo($filter, $query) { |
||
70 | return $query->where($filter['column'], '<>', $filter['query_1'], $filter['match']); |
||
71 | } |
||
72 | |||
73 | public function lessThan($filter, $query) { |
||
74 | return $query->where($filter['column'], '<', $filter['query_1'], $filter['match']); |
||
75 | } |
||
76 | |||
77 | public function greaterThan($filter, $query) { |
||
78 | return $query->where($filter['column'], '>', $filter['query_1'], $filter['match']); |
||
79 | } |
||
80 | |||
81 | public function between($filter, $query) { |
||
82 | return $query->whereBetween($filter['column'], [ |
||
83 | $filter['query_1'], $filter['query_2'] |
||
84 | ], $filter['match']); |
||
85 | } |
||
86 | |||
87 | public function notBetween($filter, $query) { |
||
88 | return $query->whereNotBetween($filter['column'], [ |
||
89 | $filter['query_1'], $filter['query_2'] |
||
90 | ], $filter['match']); |
||
91 | } |
||
92 | |||
93 | public function contains($filter, $query) { |
||
94 | return $query->where($filter['column'], 'like', '%'.$filter['query_1'].'%', $filter['match']); |
||
95 | } |
||
96 | |||
97 | public function startsWith($filter, $query) { |
||
98 | return $query->where($filter['column'], 'like', $filter['query_1'].'%', $filter['match']); |
||
99 | } |
||
100 | |||
101 | public function endsWith($filter, $query) { |
||
102 | return $query->where($filter['column'], 'like', '%'.$filter['query_1'], $filter['match']); |
||
103 | } |
||
104 | |||
105 | public function inThePast($filter, $query) { |
||
106 | $end = now()->endOfDay(); |
||
107 | $begin = now(); |
||
108 | |||
109 | switch ($filter['query_2']) { |
||
110 | case 'hours': |
||
111 | $begin->subHours($filter['query_1']); |
||
112 | break; |
||
113 | case 'days': |
||
114 | $begin->subDays($filter['query_1'])->startOfDay(); |
||
115 | break; |
||
116 | case 'months': |
||
117 | $begin->subMonths($filter['query_1'])->startOfDay(); |
||
118 | break; |
||
119 | case 'years': |
||
120 | $begin->subYears($filter['query_1'])->startOfDay(); |
||
121 | break; |
||
122 | default: |
||
123 | $begin->subDays($filter['query_1'])->startOfDay(); |
||
124 | break; |
||
125 | } |
||
126 | |||
127 | return $query->whereBetween($filter['column'], [$begin, $end], $filter['match']); |
||
128 | } |
||
129 | |||
130 | public function inTheNext($filter, $query) { |
||
131 | $begin = now()->startOfDay(); |
||
132 | $end = now(); |
||
133 | |||
134 | switch ($filter['query_2']) { |
||
135 | case 'hours': |
||
136 | $begin->addHours($filter['query_1']); |
||
137 | break; |
||
138 | case 'days': |
||
139 | $begin->addDays($filter['query_1'])->endOfDay(); |
||
140 | break; |
||
141 | case 'months': |
||
142 | $begin->addMonths($filter['query_1'])->endOfDay(); |
||
143 | break; |
||
144 | case 'years': |
||
145 | $begin->addYears($filter['query_1'])->endOfDay(); |
||
146 | break; |
||
147 | default: |
||
148 | $begin->addDays($filter['query_1'])->endOfDay(); |
||
149 | break; |
||
150 | } |
||
151 | |||
152 | return $query->whereBetween($filter['column'], [$begin, $end], $filter['match']); |
||
153 | } |
||
154 | |||
155 | public function inThePeriod($filter, $query) |
||
212 | |||
213 | } |
||
214 | |||
215 | public function equalToCount($filter, $query, $relation) { |
||
216 | return $query->has($relation, '=', $filter['query_1']); |
||
217 | } |
||
218 | |||
219 | public function notEqualToCount($filter, $query, $relation) { |
||
220 | return $query->has($relation, '<>', $filter['query_1']); |
||
221 | } |
||
222 | |||
223 | public function lessThanCount($filter, $query, $relation) { |
||
225 | } |
||
226 | |||
227 | public function greaterThanCount($filter, $query, $relation) { |
||
228 | return $query->has($relation, '>', $filter['query_1']); |
||
229 | } |
||
230 | } |