| Conditions | 30 |
| Paths | 240 |
| Total Lines | 132 |
| Code Lines | 80 |
| Lines | 21 |
| Ratio | 15.91 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 37 | public function apply($model, RepositoryInterface $repository) |
||
| 38 | { |
||
| 39 | $fieldsSearchable = $repository->getFieldsSearchable(); |
||
| 40 | $search = $this->request->get(config('repository.criteria.params.search', 'search'), null); |
||
| 41 | $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null); |
||
| 42 | $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null); |
||
| 43 | $orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null); |
||
| 44 | $sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc'); |
||
| 45 | $with = $this->request->get(config('repository.criteria.params.with', 'with'), null); |
||
| 46 | $sortedBy = ! empty($sortedBy) ? $sortedBy : 'asc'; |
||
| 47 | |||
| 48 | if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) { |
||
| 49 | $searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', |
||
| 50 | $searchFields); |
||
| 51 | $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields); |
||
| 52 | $isFirstField = true; |
||
| 53 | $searchData = $this->parserSearchData($search); |
||
| 54 | $search = $this->parserSearchValue($search); |
||
| 55 | $modelForceAndWhere = false; |
||
| 56 | |||
| 57 | $model = $model->where(function ($query) use ( |
||
| 58 | $fields, |
||
| 59 | $search, |
||
| 60 | $searchData, |
||
| 61 | $isFirstField, |
||
| 62 | $modelForceAndWhere |
||
| 63 | ) { |
||
| 64 | /* @var Builder $query */ |
||
| 65 | |||
| 66 | foreach ($fields as $field => $condition) { |
||
| 67 | if (is_numeric($field)) { |
||
| 68 | $field = $condition; |
||
| 69 | $condition = '='; |
||
| 70 | } |
||
| 71 | |||
| 72 | $value = null; |
||
| 73 | |||
| 74 | $condition = trim(strtolower($condition)); |
||
| 75 | |||
| 76 | if (isset($searchData[$field])) { |
||
| 77 | $value = ($condition == 'like' || $condition == 'ilike') ? "%{$searchData[$field]}%" : $searchData[$field]; |
||
| 78 | } else { |
||
| 79 | if (! is_null($search)) { |
||
| 80 | $value = ($condition == 'like' || $condition == 'ilike') ? "%{$search}%" : $search; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | $relation = null; |
||
| 85 | if (stripos($field, '.')) { |
||
| 86 | $explode = explode('.', $field); |
||
| 87 | $field = array_pop($explode); |
||
| 88 | $relation = implode('.', $explode); |
||
| 89 | } |
||
| 90 | $modelTableName = $query->getModel()->getTable(); |
||
| 91 | if ($isFirstField || $modelForceAndWhere) { |
||
| 92 | View Code Duplication | if (! is_null($value)) { |
|
| 93 | if (! is_null($relation)) { |
||
| 94 | $query->whereHas($relation, function ($query) use ($field, $condition, $value) { |
||
| 95 | $query->where($field, $condition, $value); |
||
| 96 | }); |
||
| 97 | } else { |
||
| 98 | $query->where($modelTableName.'.'.$field, $condition, $value); |
||
| 99 | } |
||
| 100 | $isFirstField = false; |
||
| 101 | } |
||
| 102 | View Code Duplication | } else { |
|
| 103 | if (! is_null($value)) { |
||
| 104 | if (! is_null($relation)) { |
||
| 105 | $query->orWhereHas($relation, function ($query) use ($field, $condition, $value) { |
||
| 106 | $query->where($field, $condition, $value); |
||
| 107 | }); |
||
| 108 | } else { |
||
| 109 | $query->orWhere($modelTableName.'.'.$field, $condition, $value); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | }); |
||
| 115 | } |
||
| 116 | |||
| 117 | if (isset($orderBy) && ! empty($orderBy)) { |
||
| 118 | $split = explode('|', $orderBy); |
||
| 119 | if (count($split) > 1) { |
||
| 120 | /* |
||
| 121 | * ex. |
||
| 122 | * products|description -> join products on current_table.product_id = products.id order by description |
||
| 123 | * |
||
| 124 | * products:custom_id|products.description -> join products on current_table.custom_id = products.id order |
||
| 125 | * by products.description (in case both tables have same column name) |
||
| 126 | */ |
||
| 127 | $table = $model->getModel()->getTable(); |
||
| 128 | $sortTable = $split[0]; |
||
| 129 | $sortColumn = $split[1]; |
||
| 130 | |||
| 131 | $split = explode(':', $sortTable); |
||
| 132 | if (count($split) > 1) { |
||
| 133 | $sortTable = $split[0]; |
||
| 134 | $keyName = $table.'.'.$split[1]; |
||
| 135 | } else { |
||
| 136 | /* |
||
| 137 | * If you do not define which column to use as a joining column on current table, it will |
||
| 138 | * use a singular of a join table appended with _id |
||
| 139 | * |
||
| 140 | * ex. |
||
| 141 | * products -> product_id |
||
| 142 | */ |
||
| 143 | $prefix = rtrim($sortTable, 's'); |
||
| 144 | $keyName = $table.'.'.$prefix.'_id'; |
||
| 145 | } |
||
| 146 | |||
| 147 | $model = $model->leftJoin($sortTable, $keyName, '=', $sortTable.'.id')->orderBy($sortColumn, |
||
| 148 | $sortedBy)->addSelect($table.'.*'); |
||
| 149 | } else { |
||
| 150 | $model = $model->orderBy($orderBy, $sortedBy); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | if (isset($filter) && ! empty($filter)) { |
||
| 155 | if (is_string($filter)) { |
||
| 156 | $filter = explode(';', $filter); |
||
| 157 | } |
||
| 158 | |||
| 159 | $model = $model->select($filter); |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($with) { |
||
| 163 | $with = explode(';', $with); |
||
| 164 | $model = $model->with($with); |
||
| 165 | } |
||
| 166 | |||
| 167 | return $model; |
||
| 168 | } |
||
| 169 | |||
| 268 |