| Total Complexity | 52 |
| Total Lines | 240 |
| 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 |
||
| 21 | class RequestCriteria extends \Prettus\Repository\Criteria\RequestCriteria |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Apply criteria in query repository. |
||
| 25 | * |
||
| 26 | * @param Builder|Model $model |
||
| 27 | * @param RepositoryInterface $repository |
||
| 28 | * |
||
| 29 | * @throws \Exception |
||
| 30 | * |
||
| 31 | * @return mixed |
||
| 32 | */ |
||
| 33 | public function apply($model, RepositoryInterface $repository) |
||
| 34 | { |
||
| 35 | $fieldsSearchable = $repository->getFieldsSearchable(); |
||
| 36 | list($querySearch, $querySearchFields) = $this->parserQueryToSearch($fieldsSearchable); |
||
| 37 | $search = $this->request->get(config('repository.criteria.params.search', 'search'), $querySearch); |
||
| 38 | $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), $querySearchFields); |
||
| 39 | $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null); |
||
| 40 | $orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null); |
||
| 41 | $sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc'); |
||
| 42 | $with = $this->request->get(config('repository.criteria.params.with', 'with'), null); |
||
| 43 | $sortedBy = !empty($sortedBy) ? $sortedBy : 'asc'; |
||
| 44 | |||
| 45 | if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) { |
||
| 46 | $searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', $searchFields); |
||
| 47 | $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields); |
||
| 48 | $isFirstField = true; |
||
| 49 | $searchData = $this->parserSearchData($search); |
||
| 50 | $search = $this->parserSearchValue($search); |
||
| 51 | $modelForceAndWhere = method_exists($repository, 'getIsSearchableForceAndWhere') ? $repository->getIsSearchableForceAndWhere() : false; |
||
| 52 | |||
| 53 | $model = $model->where(function ($query) use ($fields, $search, $searchData, $isFirstField, $modelForceAndWhere) { |
||
| 54 | /* @var Builder $query */ |
||
| 55 | |||
| 56 | foreach ($fields as $field => $condition) { |
||
| 57 | if (is_numeric($field)) { |
||
| 58 | $field = $condition; |
||
| 59 | $condition = '='; |
||
| 60 | } |
||
| 61 | |||
| 62 | $value = null; |
||
| 63 | |||
| 64 | $condition = trim(strtolower($condition)); |
||
| 65 | |||
| 66 | if (isset($searchData[$field])) { |
||
| 67 | if ($condition == 'like_raw') { |
||
| 68 | $condition = 'like'; |
||
| 69 | $value = $searchData[$field]; |
||
| 70 | } elseif ($condition == 'like_safe') { |
||
| 71 | $condition = 'like'; |
||
| 72 | if (str_contains_ascii_only($searchData[$field])) { |
||
| 73 | $value = "%{$searchData[$field]}%"; |
||
| 74 | } else { |
||
| 75 | $value = null; |
||
| 76 | } |
||
| 77 | } else { |
||
| 78 | $value = ($condition == 'like' || $condition == 'ilike') ? "%{$searchData[$field]}%" : $searchData[$field]; |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | if (!is_null($search)) { |
||
| 82 | if ($condition == 'like_raw') { |
||
| 83 | $condition = 'like'; |
||
| 84 | $value = $searchData[$field]; |
||
| 85 | } elseif ($condition == 'like_safe') { |
||
| 86 | $condition = 'like'; |
||
| 87 | if (str_contains_ascii_only($search)) { |
||
| 88 | $value = "%{$search}%"; |
||
| 89 | } else { |
||
| 90 | $value = null; |
||
| 91 | } |
||
| 92 | } else { |
||
| 93 | $value = ($condition == 'like' || $condition == 'ilike') ? "%{$search}%" : $search; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | $relation = null; |
||
| 99 | if (stripos($field, '.')) { |
||
| 100 | $explode = explode('.', $field); |
||
| 101 | $field = array_pop($explode); |
||
| 102 | $relation = implode('.', $explode); |
||
| 103 | } |
||
| 104 | $modelTableName = $query->getModel()->getTable(); |
||
| 105 | if ($isFirstField || $modelForceAndWhere) { |
||
| 106 | if (!is_null($value)) { |
||
| 107 | if (!is_null($relation)) { |
||
| 108 | $query->whereHas($relation, function ($query) use ($field, $condition, $value) { |
||
| 109 | $query->where($field, $condition, $value); |
||
| 110 | }); |
||
| 111 | } else { |
||
| 112 | $query->where($modelTableName.'.'.$field, $condition, $value); |
||
| 113 | } |
||
| 114 | $isFirstField = false; |
||
| 115 | } |
||
| 116 | } else { |
||
| 117 | if (!is_null($value)) { |
||
| 118 | if (!is_null($relation)) { |
||
| 119 | $query->orWhereHas($relation, function ($query) use ($field, $condition, $value) { |
||
| 120 | $query->where($field, $condition, $value); |
||
| 121 | }); |
||
| 122 | } else { |
||
| 123 | $query->orWhere($modelTableName.'.'.$field, $condition, $value); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | }); |
||
| 129 | } |
||
| 130 | |||
| 131 | if (isset($orderBy) && !empty($orderBy)) { |
||
| 132 | $split = explode('|', $orderBy); |
||
| 133 | if (count($split) > 1) { |
||
| 134 | /* |
||
| 135 | * ex. |
||
| 136 | * products|description -> join products on current_table.product_id = products.id order by description |
||
| 137 | * |
||
| 138 | * products:custom_id|products.description -> join products on current_table.custom_id = products.id order |
||
| 139 | * by products.description (in case both tables have same column name) |
||
| 140 | */ |
||
| 141 | $table = $model->getModel()->getTable(); |
||
| 142 | $sortTable = $split[0]; |
||
| 143 | $sortColumn = $split[1]; |
||
| 144 | |||
| 145 | $split = explode(':', $sortTable); |
||
| 146 | if (count($split) > 1) { |
||
| 147 | $sortTable = $split[0]; |
||
| 148 | $keyName = $table.'.'.$split[1]; |
||
| 149 | } else { |
||
| 150 | /* |
||
| 151 | * If you do not define which column to use as a joining column on current table, it will |
||
| 152 | * use a singular of a join table appended with _id |
||
| 153 | * |
||
| 154 | * ex. |
||
| 155 | * products -> product_id |
||
| 156 | */ |
||
| 157 | $prefix = rtrim($sortTable, 's'); |
||
| 158 | $keyName = $table.'.'.$prefix.'_id'; |
||
| 159 | } |
||
| 160 | |||
| 161 | $model = $model |
||
| 162 | ->leftJoin($sortTable, $keyName, '=', $sortTable.'.id') |
||
| 163 | ->orderBy($sortColumn, $sortedBy) |
||
| 164 | ->addSelect($table.'.*'); |
||
| 165 | } else { |
||
| 166 | $model = $model->orderBy($orderBy, $sortedBy); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | if (isset($filter) && !empty($filter)) { |
||
| 171 | if (is_string($filter)) { |
||
| 172 | $filter = explode(';', $filter); |
||
| 173 | } |
||
| 174 | |||
| 175 | $model = $model->select($filter); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($with) { |
||
| 179 | $with = explode(';', $with); |
||
| 180 | $model = $model->with($with); |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->request->offsetUnset(config('repository.criteria.params.search', 'search')); |
||
| 184 | $this->request->offsetUnset(config('repository.criteria.params.searchFields', 'searchFields')); |
||
| 185 | |||
| 186 | return $model; |
||
| 187 | } |
||
| 188 | |||
| 189 | protected function parserFieldsSearch(array $fields = [], array $searchFields = null) |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Parser query to Search String. |
||
| 235 | * |
||
| 236 | * @param $columns |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | protected function parserQueryToSearch($columns) |
||
| 263 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths