Conditions | 36 |
Paths | 240 |
Total Lines | 149 |
Code Lines | 83 |
Lines | 0 |
Ratio | 0 % |
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 |
||
40 | public function apply($model, RepositoryInterface $repository) |
||
41 | { |
||
42 | /** |
||
43 | * @var $search |
||
44 | * @var $searchFields |
||
45 | * @var $filter |
||
46 | * @var $orderBy |
||
47 | * @var $sortedBy |
||
48 | * @var $with |
||
49 | * @var $searchJoin |
||
50 | */ |
||
51 | extract($this->getConfigs()); |
||
52 | |||
53 | $fieldsSearchable = $repository->getFieldsSearchable(); |
||
54 | $sortedBy = !empty($sortedBy) ? $sortedBy : 'asc'; |
||
55 | |||
56 | if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) { |
||
57 | |||
58 | $searchFields = |
||
59 | is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', $searchFields); |
||
60 | $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields); |
||
61 | $isFirstField = true; |
||
62 | $searchData = $this->parserSearchData($search); |
||
63 | $search = $this->parserSearchValue($search); |
||
64 | $modelForceAndWhere = strtolower($searchJoin) === 'and'; |
||
65 | |||
66 | $model = |
||
67 | $model->where(function ($query) use ( |
||
68 | $fields, |
||
69 | $search, |
||
70 | $searchData, |
||
71 | $isFirstField, |
||
72 | $modelForceAndWhere |
||
73 | ) { |
||
74 | /** @var Builder $query */ |
||
75 | |||
76 | foreach ($fields as $field => $condition) { |
||
77 | |||
78 | if (is_numeric($field)) { |
||
79 | $field = $condition; |
||
80 | $condition = "="; |
||
81 | } |
||
82 | |||
83 | $value = null; |
||
84 | |||
85 | $condition = trim(strtolower($condition)); |
||
86 | |||
87 | if (isset($searchData[$field])) { |
||
88 | $value = $searchData[$field]; |
||
89 | if ($condition == "like" || $condition == "ilike") { |
||
90 | $value = "%{$value}%"; |
||
91 | } |
||
92 | if ($condition == "in" || $condition == "between" || $condition == "cross") { |
||
93 | $value = explode(',', $value); |
||
94 | } |
||
95 | } else { |
||
96 | if (!is_null($search)) { |
||
97 | $value = $search; |
||
98 | if ($condition == "like" || $condition == "ilike") { |
||
99 | $value = "%{$value}%"; |
||
100 | } |
||
101 | if ($condition == "in" || $condition == "between" || $condition == "cross") { |
||
102 | $value = explode(',', $value); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $relation = null; |
||
108 | if (stripos($field, '.')) { |
||
109 | $explode = explode('.', $field); |
||
110 | $field = array_pop($explode); |
||
111 | $relation = implode('.', $explode); |
||
112 | } |
||
113 | $modelTableName = $query->getModel()->getTable(); |
||
114 | if ($isFirstField || $modelForceAndWhere) { |
||
115 | if (!is_null($value)) { |
||
116 | if (!is_null($relation)) { |
||
117 | $query->whereHas($relation, $this->getRelationQueryClosure($field, $condition, $value)); |
||
118 | } else { |
||
119 | $query->where($this->getQueryClosure($field, $condition, $value)); |
||
120 | } |
||
121 | $isFirstField = false; |
||
122 | } |
||
123 | } else { |
||
124 | if (!is_null($value)) { |
||
125 | if (!is_null($relation)) { |
||
126 | $query->orWhereHas($relation, $this->getRelationQueryClosure($field, $condition, $value)); |
||
127 | } else { |
||
128 | $query->orWhere($this->getSubqueryClosure($modelTableName, $field, $condition, $value)); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | }); |
||
134 | } |
||
135 | |||
136 | if (isset($orderBy) && !empty($orderBy)) { |
||
137 | $split = explode('|', $orderBy); |
||
138 | if (count($split) > 1) { |
||
139 | /* |
||
140 | * ex. |
||
141 | * products|description -> join products on current_table.product_id = products.id order by description |
||
142 | * |
||
143 | * products:custom_id|products.description -> join products on current_table.custom_id = products.id order |
||
144 | * by products.description (in case both tables have same column name) |
||
145 | */ |
||
146 | $table = $model->getModel()->getTable(); |
||
147 | $sortTable = $split[0]; |
||
148 | $sortColumn = $split[1]; |
||
149 | |||
150 | $split = explode(':', $sortTable); |
||
151 | if (count($split) > 1) { |
||
152 | $sortTable = $split[0]; |
||
153 | $keyName = $table.'.'.$split[1]; |
||
154 | } else { |
||
155 | /* |
||
156 | * If you do not define which column to use as a joining column on current table, it will |
||
157 | * use a singular of a join table appended with _id |
||
158 | * |
||
159 | * ex. |
||
160 | * products -> product_id |
||
161 | */ |
||
162 | $prefix = Str::singular($sortTable); |
||
163 | $keyName = $table.'.'.$prefix.'_id'; |
||
164 | } |
||
165 | |||
166 | $model = $model |
||
167 | ->leftJoin($sortTable, $keyName, '=', $sortTable.'.id') |
||
168 | ->orderBy($sortColumn, $sortedBy) |
||
169 | ->addSelect($table.'.*'); |
||
170 | } else { |
||
171 | $model = $model->orderBy($orderBy, $sortedBy); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | if (isset($filter) && !empty($filter)) { |
||
176 | if (is_string($filter)) { |
||
177 | $filter = explode(';', $filter); |
||
178 | } |
||
179 | |||
180 | $model = $model->select($filter); |
||
181 | } |
||
182 | |||
183 | if ($with) { |
||
184 | $with = explode(';', $with); |
||
185 | $model = $model->with($with); |
||
186 | } |
||
187 | |||
188 | return $model; |
||
189 | } |
||
384 |