| Total Complexity | 76 |
| Total Lines | 432 |
| 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) |
||
| 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) |
||
| 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) |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param $fields |
||
| 121 | * @throws \Exception |
||
| 122 | */ |
||
| 123 | protected function fieldsAccept($fields) |
||
| 124 | { |
||
| 125 | if (count($fields) == 0) { |
||
| 126 | throw new Exception((string) trans('repository::criteria.fields_not_accepted', ['field' => implode(',', $searchFields)])); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | protected function getSubqueryClosure($modelTableName, $field, $condition, $value) |
||
| 131 | { |
||
| 132 | switch ($condition) { |
||
| 133 | case 'in': |
||
| 134 | return function (Builder $query) use ($modelTableName, $field, $value) { |
||
| 135 | $query->whereIn($modelTableName.'.'.$field, $value); |
||
| 136 | }; |
||
| 137 | case 'between': |
||
| 138 | return function (Builder $query) use ($modelTableName, $field, $value) { |
||
| 139 | $query->whereBetween($modelTableName.'.'.$field, $value); |
||
| 140 | }; |
||
| 141 | case 'cross': |
||
| 142 | return function (Builder $query) use ($modelTableName, $field, $value) { |
||
| 143 | $query->where(function (Builder $query) use ($modelTableName, $field, $value) { |
||
| 144 | $query->where(function (Builder $query) use ($modelTableName, $field, $value) { |
||
| 145 | $query->where("{$modelTableName}.{$field}_min", '<=', $value[0]) |
||
| 146 | ->where("{$modelTableName}.{$field}_max", '>=', $value[1]); |
||
| 147 | })->orWhere(function (Builder $query) use ($modelTableName, $field, $value) { |
||
| 148 | $query->where("{$modelTableName}.{$field}_min", '<=', $value[0]) |
||
| 149 | ->where("{$modelTableName}.{$field}_max", '>=', $value[0]); |
||
| 150 | })->orWhere(function (Builder $query) use ($modelTableName, $field, $value) { |
||
| 151 | $query->where("{$modelTableName}.{$field}_min", '>=', $value[0]) |
||
| 152 | ->where("{$modelTableName}.{$field}_max", '<=', $value[1]); |
||
| 153 | })->orWhere(function (Builder $query) use ($modelTableName, $field, $value) { |
||
| 154 | $query->where("{$modelTableName}.{$field}_min", '>=', $value[0]) |
||
| 155 | ->where("{$modelTableName}.{$field}_max", '>=', $value[1]) |
||
| 156 | ->where("{$modelTableName}.{$field}_min", '<=', $value[1]); |
||
| 157 | }); |
||
| 158 | }); |
||
| 159 | }; |
||
| 160 | default: |
||
| 161 | return function (Builder $query) use ($modelTableName, $field, $condition, $value) { |
||
| 162 | $query->orWhere($modelTableName.'.'.$field, $condition, $value); |
||
| 163 | }; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | protected function getQueryClosure($field, $condition, $value) |
||
| 168 | { |
||
| 169 | switch ($condition) { |
||
| 170 | case 'in': |
||
| 171 | return function (Builder $query) use ($field, $value) { |
||
| 172 | $query->whereIn($field, $value); |
||
| 173 | }; |
||
| 174 | case 'between': |
||
| 175 | return function (Builder $query) use ($field, $value) { |
||
| 176 | $query->whereBetween($field, $value); |
||
| 177 | }; |
||
| 178 | case 'cross': |
||
| 179 | return function (Builder $query) use ($field, $value) { |
||
| 180 | $query->where(function (Builder $query) use ($field, $value) { |
||
| 181 | $query->where(function (Builder $query) use ($field, $value) { |
||
| 182 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 183 | ->where("{$field}_max", '>=', $value[1]); |
||
| 184 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 185 | $query->where("{$field}_min", '<=', $value[0]) |
||
| 186 | ->where("{$field}_max", '>=', $value[0]); |
||
| 187 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 188 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 189 | ->where("{$field}_max", '<=', $value[1]); |
||
| 190 | })->orWhere(function (Builder $query) use ($field, $value) { |
||
| 191 | $query->where("{$field}_min", '>=', $value[0]) |
||
| 192 | ->where("{$field}_max", '>=', $value[1]) |
||
| 193 | ->where("{$field}_min", '<=', $value[1]); |
||
| 194 | }); |
||
| 195 | }); |
||
| 196 | }; |
||
| 197 | default: |
||
| 198 | return function (Builder $query) use ($field, $condition, $value) { |
||
| 199 | $query->where($field, $condition, $value); |
||
| 200 | }; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | protected function getRelationQueryClosure($field, $condition, $value) |
||
| 212 | }; |
||
| 213 | } |
||
| 214 | |||
| 215 | protected function setValue($condition, $field, $search, $searchData) |
||
| 216 | { |
||
| 217 | if (isset($searchData[$field])) { |
||
| 218 | $value = $this->setSearchDataValue($condition, $field, $searchData); |
||
| 219 | } else { |
||
| 220 | $value = $this->setSearchValue($condition, $search); |
||
| 221 | } |
||
| 222 | |||
| 223 | return $value; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param \Illuminate\Database\Eloquent\Builder $model |
||
| 228 | * @return mixed |
||
| 229 | */ |
||
| 230 | protected function setOrderBy($model) |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param \Illuminate\Database\Eloquent\Builder $model |
||
| 250 | * @return mixed |
||
| 251 | */ |
||
| 252 | protected function setFilter($model) |
||
| 253 | { |
||
| 254 | $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null); |
||
| 255 | |||
| 256 | if (isset($filter) && !empty($filter)) { |
||
| 257 | if (is_string($filter)) { |
||
| 258 | $filter = explode(';', $filter); |
||
| 259 | } |
||
| 260 | |||
| 261 | $model = $model->select($filter); |
||
| 262 | } |
||
| 263 | |||
| 264 | return $model; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param \Illuminate\Database\Eloquent\Builder $model |
||
| 269 | * @return mixed |
||
| 270 | */ |
||
| 271 | protected function setWith($model) |
||
| 272 | { |
||
| 273 | $with = $this->request->get(config('repository.criteria.params.with', 'with'), null); |
||
| 274 | |||
| 275 | if ($with) { |
||
| 276 | $with = explode(';', $with); |
||
| 277 | $model = $model->with($with); |
||
| 278 | } |
||
| 279 | |||
| 280 | return $model; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param \Illuminate\Database\Eloquent\Builder $model |
||
| 285 | * @param \Prettus\Repository\Contracts\RepositoryInterface $repository |
||
| 286 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 287 | * @throws \Exception |
||
| 288 | */ |
||
| 289 | protected function setSearch($model, RepositoryInterface $repository) |
||
| 290 | { |
||
| 291 | $search = $this->request->get(config('repository.criteria.params.search', 'search'), null); |
||
| 292 | $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null); |
||
| 293 | $searchJoin = $this->request->get(config('repository.criteria.params.searchJoin', 'searchJoin'), null); |
||
| 294 | |||
| 295 | $fieldsSearchable = $repository->getFieldsSearchable(); |
||
| 296 | |||
| 297 | if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) { |
||
| 298 | |||
| 299 | $searchFields = |
||
| 300 | is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', $searchFields); |
||
| 301 | $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields); |
||
| 302 | $isFirstField = true; |
||
| 303 | $searchData = $this->parserSearchData($search); |
||
| 304 | $search = $this->parserSearchValue($search); |
||
| 305 | $modelForceAndWhere = strtolower($searchJoin) === 'and'; |
||
| 306 | |||
| 307 | $model = |
||
| 308 | $model->where(function (Builder $query) use ( |
||
| 309 | $fields, |
||
| 310 | $search, |
||
| 311 | $searchData, |
||
| 312 | $isFirstField, |
||
| 313 | $modelForceAndWhere |
||
| 314 | ) { |
||
| 315 | foreach ($fields as $field => $condition) { |
||
| 316 | if (is_numeric($field)) { |
||
| 317 | $field = $condition; |
||
| 318 | $condition = "="; |
||
| 319 | } |
||
| 320 | $condition = trim(strtolower($condition)); |
||
| 321 | $value = $this->setValue($condition, $field, $search, $searchData); |
||
| 322 | $relation = null; |
||
| 323 | if (stripos($field, '.')) { |
||
| 324 | $explode = explode('.', $field); |
||
| 325 | $field = array_pop($explode); |
||
| 326 | $relation = implode('.', $explode); |
||
| 327 | } |
||
| 328 | |||
| 329 | $query->where($this->searchQueryClosure($isFirstField, $modelForceAndWhere, $relation, $field, $condition, $value)); |
||
| 330 | } |
||
| 331 | }); |
||
| 332 | } |
||
| 333 | |||
| 334 | return $model; |
||
| 335 | } |
||
| 336 | |||
| 337 | protected function setSearchDataValue($condition, $field, $searchData) |
||
| 338 | { |
||
| 339 | $value = $searchData[$field]; |
||
| 340 | if ($condition == "like" || $condition == "ilike") { |
||
| 341 | $value = "%{$value}%"; |
||
| 342 | } |
||
| 343 | if ($condition == "in" || $condition == "between" || $condition == "cross") { |
||
| 344 | $value = explode(',', $value); |
||
| 345 | } |
||
| 346 | |||
| 347 | return $value; |
||
| 348 | } |
||
| 349 | |||
| 350 | protected function setSearchValue($condition, $search) |
||
| 365 | } |
||
| 366 | |||
| 367 | protected function parserSearchFields($fields, $searchFields, $acceptedConditions) |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param \Illuminate\Database\Eloquent\Builder $model |
||
| 400 | * @param $split |
||
| 401 | * @param $sortedBy |
||
| 402 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
||
| 403 | */ |
||
| 404 | protected function sort($model, $split, $sortedBy) |
||
| 405 | { |
||
| 406 | $table = $model->getModel()->getTable(); |
||
| 407 | $sortTable = $split[0]; |
||
| 408 | $sortColumn = $split[1]; |
||
| 409 | |||
| 410 | $split = explode(':', $sortTable); |
||
| 411 | if (count($split) > 1) { |
||
| 412 | $sortTable = $split[0]; |
||
| 413 | $keyName = $table.'.'.$split[1]; |
||
| 414 | } else { |
||
| 415 | $prefix = Str::singular($sortTable); |
||
| 416 | $keyName = $table.'.'.$prefix.'_id'; |
||
| 417 | } |
||
| 418 | |||
| 419 | return $model |
||
| 420 | ->leftJoin($sortTable, $keyName, '=', $sortTable.'.id') |
||
| 421 | ->orderBy($sortColumn, $sortedBy) |
||
| 422 | ->addSelect($table.'.*'); |
||
| 423 | } |
||
| 424 | |||
| 425 | protected function searchQueryClosure(&$isFirstField, $modelForceAndWhere, $relation, $field, $condition, $value) |
||
| 451 | } |
||
| 452 | } |
||
| 453 | } |
||
| 454 | }; |
||
| 455 | } |
||
| 456 | } |
||
| 457 |