Total Complexity | 77 |
Total Lines | 401 |
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 | * @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder |
||
27 | */ |
||
28 | private $model; |
||
29 | private $search; |
||
30 | private $searchData; |
||
31 | private $searchFields; |
||
32 | private $isFirstField = true; |
||
33 | private $modelForceAndWhere; |
||
34 | private $fieldsSearchable; |
||
35 | private $fields; |
||
36 | private $filter; |
||
37 | private $orderBy; |
||
38 | private $sortedBy; |
||
39 | private $with; |
||
40 | private $searchJoin; |
||
41 | private $acceptedConditions; |
||
42 | private $originalFields; |
||
43 | |||
44 | |||
45 | public function __construct(Request $request) |
||
46 | { |
||
47 | $this->request = $request; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Apply criteria in query repository |
||
52 | * |
||
53 | * @param Builder|Model $model |
||
54 | * @param RepositoryInterface $repository |
||
55 | * |
||
56 | * @return mixed |
||
57 | * @throws \Exception |
||
58 | */ |
||
59 | public function apply($model, RepositoryInterface $repository) |
||
60 | { |
||
61 | $this->model = $model; |
||
62 | $this->fieldsSearchable = $repository->getFieldsSearchable(); |
||
63 | $this->search = $this->request->get(config('repository.criteria.params.search', 'search'), null); |
||
64 | $this->searchFields = |
||
65 | $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null); |
||
66 | $this->filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null); |
||
67 | $this->orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null); |
||
68 | $this->sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc'); |
||
69 | $this->with = $this->request->get(config('repository.criteria.params.with', 'with'), null); |
||
70 | $this->searchJoin = $this->request->get(config('repository.criteria.params.searchJoin', 'searchJoin'), null); |
||
71 | $this->sortedBy = !empty($this->sortedBy) ? $this->sortedBy : 'asc'; |
||
72 | $this->acceptedConditions = config('repository.criteria.acceptedConditions', ['=', 'like']); |
||
73 | |||
74 | $this->parserSearch(); |
||
75 | $this->parserOrderBy(); |
||
76 | $this->parserFilter(); |
||
77 | $this->parserWith(); |
||
78 | |||
79 | return $this->model; |
||
80 | } |
||
81 | |||
82 | protected function parserSearchData() |
||
83 | { |
||
84 | $searchData = []; |
||
85 | |||
86 | if (stripos($this->search, ':')) { |
||
87 | $fields = explode(';', $this->search); |
||
88 | |||
89 | foreach ($fields as $row) { |
||
90 | try { |
||
91 | list($field, $value) = explode(':', $row); |
||
92 | $searchData[$field] = $value; |
||
93 | } catch (Exception $e) { |
||
94 | //Surround offset error |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | $this->searchData = $searchData; |
||
100 | } |
||
101 | |||
102 | protected function parserSearchValue() |
||
103 | { |
||
104 | if (stripos($this->search, ';') || stripos($this->search, ':')) { |
||
105 | $values = explode(';', $this->search); |
||
106 | foreach ($values as $value) { |
||
107 | $s = explode(':', $value); |
||
108 | if (count($s) == 1) { |
||
109 | $this->search = $s[0]; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | $this->search = null; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @throws \Exception |
||
119 | */ |
||
120 | protected function parserFieldsSearch() |
||
148 | } |
||
149 | |||
150 | protected function parserOrderBy() |
||
151 | { |
||
152 | if (isset($this->orderBy) && !empty($this->orderBy)) { |
||
153 | $split = explode('|', $this->orderBy); |
||
154 | if (count($split) > 1) { |
||
155 | $table = $this->model->getModel()->getTable(); |
||
156 | $sortTable = $split[0]; |
||
157 | $sortColumn = $split[1]; |
||
158 | |||
159 | $split = explode(':', $sortTable); |
||
160 | if (count($split) > 1) { |
||
161 | $sortTable = $split[0]; |
||
162 | $keyName = $table.'.'.$split[1]; |
||
163 | } else { |
||
164 | $prefix = Str::singular($sortTable); |
||
165 | $keyName = $table.'.'.$prefix.'_id'; |
||
166 | } |
||
167 | |||
168 | $this->model = $this->model->leftJoin($sortTable, $keyName, '=', $sortTable.'.id') |
||
169 | ->orderBy($sortColumn, $this->sortedBy) |
||
170 | ->addSelect($table.'.*'); |
||
171 | } else { |
||
172 | $this->model = $this->model->orderBy($this->orderBy, $this->sortedBy); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | protected function parserFilter() |
||
185 | } |
||
186 | } |
||
187 | |||
188 | protected function parserWith() |
||
189 | { |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @throws \Exception |
||
198 | */ |
||
199 | protected function parserSearch() |
||
200 | { |
||
201 | if ($this->search && is_array($this->fieldsSearchable) && count($this->fieldsSearchable)) { |
||
202 | |||
203 | $this->parserFieldsSearch(); |
||
204 | $this->parserSearchData(); |
||
205 | $this->parserSearchValue(); |
||
206 | |||
207 | $this->modelForceAndWhere = strtolower($this->searchJoin) === 'and'; |
||
208 | |||
209 | foreach ($this->fields as $field => $condition) { |
||
210 | |||
211 | if (is_numeric($field)) { |
||
212 | $field = $condition; |
||
213 | $condition = "="; |
||
214 | } |
||
215 | |||
216 | $value = $this->parserValue($condition, $field); |
||
217 | |||
218 | $relation = null; |
||
219 | if (stripos($field, '.')) { |
||
220 | $explode = explode('.', $field); |
||
221 | $field = array_pop($explode); |
||
222 | $relation = implode('.', $explode); |
||
223 | } |
||
224 | if ($this->isFirstField || $this->modelForceAndWhere) { |
||
225 | $this->parserSearchAndWhere($value, $relation, $field, $condition); |
||
226 | } else { |
||
227 | $this->parserSearchOrWhere($value, $relation, $field, $condition); |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | |||
233 | protected function parserSearchFields() |
||
234 | { |
||
235 | if (!is_array($this->searchFields) && !is_null($this->searchFields)) { |
||
236 | $this->searchFields = explode(';', $this->searchFields); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | protected function parserOriginalFields() |
||
241 | { |
||
242 | $this->originalFields = $this->fieldsSearchable; |
||
243 | |||
244 | foreach ($this->searchFields as $index => $field) { |
||
245 | $field_parts = explode(':', $field); |
||
246 | $temporaryIndex = array_search($field_parts[0], $this->originalFields); |
||
247 | |||
248 | if (count($field_parts) == 2) { |
||
249 | if (in_array($field_parts[1], $this->acceptedConditions)) { |
||
250 | unset($this->originalFields[$temporaryIndex]); |
||
251 | $field = $field_parts[0]; |
||
252 | $condition = $field_parts[1]; |
||
253 | $this->originalFields[$field] = $condition; |
||
254 | $this->searchFields[$index] = $field; |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
260 | protected function parserSearchAndWhere($value, $relation, $field, $condition) |
||
326 | } |
||
327 | } |
||
328 | |||
329 | private function parserSearchOrWhere($value, $relation, $field, $condition) |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | } |
||
392 | |||
393 | protected function parserValue($condition, $field) |
||
422 |