Total Complexity | 71 |
Total Lines | 392 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RequestCriteria 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 RequestCriteria, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class RequestCriteria implements CriteriaInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var \Illuminate\Http\Request |
||
23 | */ |
||
24 | protected $request; |
||
25 | |||
26 | public function __construct(Request $request) |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Apply criteria in query repository |
||
33 | * |
||
34 | * @param Builder|Model $model |
||
35 | * @param RepositoryInterface $repository |
||
36 | * |
||
37 | * @return mixed |
||
38 | * @throws \Exception |
||
39 | */ |
||
40 | public function apply($model, RepositoryInterface $repository) |
||
41 | { |
||
42 | $model = $this->setSearch($model, $repository); |
||
|
|||
43 | $model = $this->setOrderBy($model); |
||
44 | $model = $this->setFilter($model); |
||
45 | $model = $this->setWith($model); |
||
46 | |||
47 | return $model; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param $search |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | protected function parserSearchData($search) |
||
56 | { |
||
57 | $searchData = []; |
||
58 | |||
59 | if (stripos($search, ':')) { |
||
60 | $fields = explode(';', $search); |
||
61 | |||
62 | foreach ($fields as $row) { |
||
63 | try { |
||
64 | list($field, $value) = explode(':', $row); |
||
65 | $searchData[$field] = $value; |
||
66 | } catch (Exception $e) { |
||
67 | //Surround offset error |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $searchData; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param $search |
||
77 | * @return string|null |
||
78 | */ |
||
79 | protected function parserSearchValue($search) |
||
80 | { |
||
81 | |||
82 | if (stripos($search, ';') || stripos($search, ':')) { |
||
83 | $values = explode(';', $search); |
||
84 | foreach ($values as $value) { |
||
85 | $s = explode(':', $value); |
||
86 | if (count($s) == 1) { |
||
87 | return $s[0]; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | return null; |
||
92 | } |
||
93 | |||
94 | return $search; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param array $fields |
||
99 | * @param array|null $searchFields |
||
100 | * @return array |
||
101 | * @throws \Exception |
||
102 | */ |
||
103 | protected function parserFieldsSearch(array $fields = [], array $searchFields = null) |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param $fields |
||
144 | * @throws \Exception |
||
145 | */ |
||
146 | protected function fieldsAccept($fields) |
||
147 | { |
||
148 | if (count($fields) == 0) { |
||
149 | throw new Exception((string) trans('repository::criteria.fields_not_accepted', ['field' => implode(',', $searchFields)])); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | protected function getSubqueryClosure($modelTableName, $field, $condition, $value) |
||
154 | { |
||
155 | switch ($condition) { |
||
156 | case 'in': |
||
157 | return function (Builder $query) use ($modelTableName, $field, $value) { |
||
158 | $query->whereIn($modelTableName.'.'.$field, $value); |
||
159 | }; |
||
160 | case 'between': |
||
161 | return function (Builder $query) use ($modelTableName, $field, $value) { |
||
162 | $query->whereBetween($modelTableName.'.'.$field, $value); |
||
163 | }; |
||
164 | case 'cross': |
||
165 | return function (Builder $query) use ($modelTableName, $field, $value) { |
||
166 | $query->where(function (Builder $query) use ($modelTableName, $field, $value) { |
||
167 | $query->where(function (Builder $query) use ($modelTableName, $field, $value) { |
||
168 | $query->where("{$modelTableName}.{$field}_min", '<=', $value[0]) |
||
169 | ->where("{$modelTableName}.{$field}_max", '>=', $value[1]); |
||
170 | })->orWhere(function (Builder $query) use ($modelTableName, $field, $value) { |
||
171 | $query->where("{$modelTableName}.{$field}_min", '<=', $value[0]) |
||
172 | ->where("{$modelTableName}.{$field}_max", '>=', $value[0]); |
||
173 | })->orWhere(function (Builder $query) use ($modelTableName, $field, $value) { |
||
174 | $query->where("{$modelTableName}.{$field}_min", '>=', $value[0]) |
||
175 | ->where("{$modelTableName}.{$field}_max", '<=', $value[1]); |
||
176 | })->orWhere(function (Builder $query) use ($modelTableName, $field, $value) { |
||
177 | $query->where("{$modelTableName}.{$field}_min", '>=', $value[0]) |
||
178 | ->where("{$modelTableName}.{$field}_max", '>=', $value[1]) |
||
179 | ->where("{$modelTableName}.{$field}_min", '<=', $value[1]); |
||
180 | }); |
||
181 | }); |
||
182 | }; |
||
183 | default: |
||
184 | return function (Builder $query) use ($modelTableName, $field, $condition, $value) { |
||
185 | $query->orWhere($modelTableName.'.'.$field, $condition, $value); |
||
186 | }; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | protected function getQueryClosure($field, $condition, $value) |
||
191 | { |
||
192 | switch ($condition) { |
||
193 | case 'in': |
||
194 | return function (Builder $query) use ($field, $value) { |
||
195 | $query->whereIn($field, $value); |
||
196 | }; |
||
197 | case 'between': |
||
198 | return function (Builder $query) use ($field, $value) { |
||
199 | $query->whereBetween($field, $value); |
||
200 | }; |
||
201 | case 'cross': |
||
202 | return function (Builder $query) use ($field, $value) { |
||
203 | $query->where(function (Builder $query) use ($field, $value) { |
||
204 | $query->where(function (Builder $query) use ($field, $value) { |
||
205 | $query->where("{$field}_min", '<=', $value[0]) |
||
206 | ->where("{$field}_max", '>=', $value[1]); |
||
207 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
208 | $query->where("{$field}_min", '<=', $value[0]) |
||
209 | ->where("{$field}_max", '>=', $value[0]); |
||
210 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
211 | $query->where("{$field}_min", '>=', $value[0]) |
||
212 | ->where("{$field}_max", '<=', $value[1]); |
||
213 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
214 | $query->where("{$field}_min", '>=', $value[0]) |
||
215 | ->where("{$field}_max", '>=', $value[1]) |
||
216 | ->where("{$field}_min", '<=', $value[1]); |
||
217 | }); |
||
218 | }); |
||
219 | }; |
||
220 | default: |
||
221 | return function (Builder $query) use ($field, $condition, $value) { |
||
222 | $query->where($field, $condition, $value); |
||
223 | }; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | protected function getRelationQueryClosure($field, $condition, $value) |
||
235 | }; |
||
236 | } |
||
237 | |||
238 | protected function setValue($condition, $field, $search, $searchData) |
||
239 | { |
||
240 | $value = null; |
||
241 | |||
242 | if (isset($searchData[$field])) { |
||
243 | $value = $searchData[$field]; |
||
244 | if ($condition == "like" || $condition == "ilike") { |
||
245 | $value = "%{$value}%"; |
||
246 | } |
||
247 | if ($condition == "in" || $condition == "between" || $condition == "cross") { |
||
248 | $value = explode(',', $value); |
||
249 | } |
||
250 | } else { |
||
251 | if (!is_null($search)) { |
||
252 | $value = $search; |
||
253 | if ($condition == "like" || $condition == "ilike") { |
||
254 | $value = "%{$value}%"; |
||
255 | } |
||
256 | if ($condition == "in" || $condition == "between" || $condition == "cross") { |
||
257 | $value = explode(',', $value); |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | |||
262 | return $value; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @param \Illuminate\Database\Eloquent\Builder $model |
||
267 | * @return mixed |
||
268 | */ |
||
269 | protected function setOrderBy($model) |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param \Illuminate\Database\Eloquent\Builder $model |
||
305 | * @return mixed |
||
306 | */ |
||
307 | protected function setFilter($model) |
||
308 | { |
||
309 | $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null); |
||
310 | |||
311 | if (isset($filter) && !empty($filter)) { |
||
312 | if (is_string($filter)) { |
||
313 | $filter = explode(';', $filter); |
||
314 | } |
||
315 | |||
316 | $model = $model->select($filter); |
||
317 | } |
||
318 | |||
319 | return $model; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @param \Illuminate\Database\Eloquent\Builder $model |
||
324 | * @return mixed |
||
325 | */ |
||
326 | protected function setWith($model) |
||
327 | { |
||
328 | $with = $this->request->get(config('repository.criteria.params.with', 'with'), null); |
||
329 | |||
330 | if ($with) { |
||
331 | $with = explode(';', $with); |
||
332 | $model = $model->with($with); |
||
333 | } |
||
334 | |||
335 | return $model; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @param \Illuminate\Database\Eloquent\Builder $model |
||
340 | * @param \Prettus\Repository\Contracts\RepositoryInterface $repository |
||
341 | * @return \Illuminate\Database\Eloquent\Builder |
||
342 | * @throws \Exception |
||
343 | */ |
||
344 | protected function setSearch($model, RepositoryInterface $repository) |
||
411 | } |
||
412 | } |
||
413 |