| 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 |
||
| 185 | public function actionRedirects( |
||
| 186 | string $sort = 'hitCount|desc', |
||
| 187 | int $page = 1, |
||
| 188 | int $per_page = 20, |
||
| 189 | $filter = '', |
||
| 190 | $siteId = 0 |
||
| 191 | ): Response { |
||
| 192 | PermissionHelper::controllerPermissionCheck('webperf:redirects'); |
||
| 193 | $data = []; |
||
| 194 | $sortField = 'hitCount'; |
||
| 195 | $sortType = 'DESC'; |
||
| 196 | // Figure out the sorting type |
||
| 197 | if ($sort !== '') { |
||
| 198 | if (strpos($sort, '|') === false) { |
||
| 199 | $sortField = $sort; |
||
| 200 | } else { |
||
| 201 | list($sortField, $sortType) = explode('|', $sort); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | // Query the db table |
||
| 205 | $offset = ($page - 1) * $per_page; |
||
| 206 | $query = (new Query()) |
||
| 207 | ->from(['{{%retour_static_redirects}}']) |
||
| 208 | ->offset($offset) |
||
| 209 | ->limit($per_page) |
||
| 210 | ->orderBy("{$sortField} {$sortType}"); |
||
| 211 | if ((int)$siteId !== 0) { |
||
| 212 | $query->where(['siteId' => $siteId]); |
||
| 213 | } |
||
| 214 | if ($filter !== '') { |
||
| 215 | $query->where(['like', 'redirectSrcUrl', $filter]); |
||
| 216 | $query->orWhere(['like', 'redirectDestUrl', $filter]); |
||
| 217 | } |
||
| 218 | $redirects = $query->all(); |
||
| 219 | // Add in the `deleteLink` field |
||
| 220 | foreach ($redirects as &$redirect) { |
||
| 221 | $redirect['deleteLink'] = UrlHelper::cpUrl('retour/delete-redirect/'.$redirect['id']); |
||
| 222 | $redirect['editLink'] = UrlHelper::cpUrl('retour/edit-redirect/'.$redirect['id']); |
||
| 223 | } |
||
| 224 | // Format the data for the API |
||
| 225 | if ($redirects) { |
||
| 226 | $data['data'] = $redirects; |
||
| 227 | $query = (new Query()) |
||
| 228 | ->from(['{{%retour_static_redirects}}']); |
||
| 229 | if ($filter !== '') { |
||
| 230 | $query->where(['like', 'redirectSrcUrl', $filter]); |
||
| 231 | $query->orWhere(['like', 'redirectDestUrl', $filter]); |
||
| 232 | } |
||
| 233 | $count = $query->count(); |
||
| 234 | $data['links']['pagination'] = [ |
||
| 235 | 'total' => $count, |
||
| 236 | 'per_page' => $per_page, |
||
| 237 | 'current_page' => $page, |
||
| 238 | 'last_page' => ceil($count / $per_page), |
||
| 239 | 'next_page_url' => null, |
||
| 240 | 'prev_page_url' => null, |
||
| 241 | 'from' => $offset + 1, |
||
| 242 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
| 243 | ]; |
||
| 244 | } |
||
| 245 | |||
| 246 | return $this->asJson($data); |
||
| 247 | } |
||
| 252 |