Conditions | 9 |
Paths | 120 |
Total Lines | 62 |
Code Lines | 42 |
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 |
||
146 | public function actionRedirects( |
||
147 | string $sort = 'hitCount|desc', |
||
148 | int $page = 1, |
||
149 | int $per_page = 20, |
||
150 | $filter = '', |
||
151 | $siteId = 0 |
||
152 | ): Response { |
||
153 | PermissionHelper::controllerPermissionCheck('webperf:redirects'); |
||
154 | $data = []; |
||
155 | $sortField = 'hitCount'; |
||
156 | $sortType = 'DESC'; |
||
157 | // Figure out the sorting type |
||
158 | if ($sort !== '') { |
||
159 | if (strpos($sort, '|') === false) { |
||
160 | $sortField = $sort; |
||
161 | } else { |
||
162 | list($sortField, $sortType) = explode('|', $sort); |
||
163 | } |
||
164 | } |
||
165 | // Query the db table |
||
166 | $offset = ($page - 1) * $per_page; |
||
167 | $query = (new Query()) |
||
168 | ->from(['{{%retour_static_redirects}}']) |
||
169 | ->offset($offset) |
||
170 | ->limit($per_page) |
||
171 | ->orderBy("{$sortField} {$sortType}"); |
||
172 | if ((int)$siteId !== 0) { |
||
173 | $query->where(['siteId' => $siteId]); |
||
174 | } |
||
175 | if ($filter !== '') { |
||
176 | $query->where(['like', 'redirectSrcUrl', $filter]); |
||
177 | $query->orWhere(['like', 'redirectDestUrl', $filter]); |
||
178 | } |
||
179 | $redirects = $query->all(); |
||
180 | // Add in the `deleteLink` field |
||
181 | foreach ($redirects as &$redirect) { |
||
182 | $redirect['deleteLink'] = UrlHelper::cpUrl('retour/delete-redirect/'.$redirect['id']); |
||
183 | $redirect['editLink'] = UrlHelper::cpUrl('retour/edit-redirect/'.$redirect['id']); |
||
184 | } |
||
185 | // Format the data for the API |
||
186 | if ($redirects) { |
||
187 | $data['data'] = $redirects; |
||
188 | $query = (new Query()) |
||
189 | ->from(['{{%retour_static_redirects}}']); |
||
190 | if ($filter !== '') { |
||
191 | $query->where(['like', 'redirectSrcUrl', $filter]); |
||
192 | $query->orWhere(['like', 'redirectDestUrl', $filter]); |
||
193 | } |
||
194 | $count = $query->count(); |
||
195 | $data['links']['pagination'] = [ |
||
196 | 'total' => $count, |
||
197 | 'per_page' => $per_page, |
||
198 | 'current_page' => $page, |
||
199 | 'last_page' => ceil($count / $per_page), |
||
200 | 'next_page_url' => null, |
||
201 | 'prev_page_url' => null, |
||
202 | 'from' => $offset + 1, |
||
203 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
204 | ]; |
||
205 | } |
||
206 | |||
207 | return $this->asJson($data); |
||
208 | } |
||
213 |