| Conditions | 16 |
| Paths | 1335 |
| Total Lines | 96 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 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 |
||
| 195 | public function actionRedirects( |
||
| 196 | string $sort = 'hitCount|desc', |
||
| 197 | int $page = 1, |
||
| 198 | int $per_page = 20, |
||
| 199 | $filter = '', |
||
| 200 | $siteId = 0, |
||
| 201 | $shortLinks = false, |
||
| 202 | ): Response { |
||
| 203 | PermissionHelper::controllerPermissionCheck('retour:redirects'); |
||
| 204 | $data = []; |
||
| 205 | $sortField = 'hitCount'; |
||
| 206 | $sortType = 'DESC'; |
||
| 207 | // Figure out the sorting type |
||
| 208 | if ($sort !== '') { |
||
| 209 | if (strpos($sort, '|') === false) { |
||
| 210 | $sortField = $sort; |
||
| 211 | } else { |
||
| 212 | list($sortField, $sortType) = explode('|', $sort); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | $sortType = strtoupper($sortType); |
||
| 216 | $sortType = self::SORT_MAP[$sortType] ?? self::SORT_MAP['DESC']; |
||
| 217 | // Validate untrusted data |
||
| 218 | if (!in_array($sortField, self::ALLOWED_REDIRECTS_SORT_FIELDS, true)) { |
||
| 219 | throw new BadRequestHttpException(Craft::t('retour', 'Invalid sort field specified.')); |
||
| 220 | } |
||
| 221 | // Query the db table |
||
| 222 | $offset = ($page - 1) * $per_page; |
||
| 223 | $query = (new Query()) |
||
| 224 | ->from(['{{%retour_static_redirects}}']) |
||
| 225 | ->offset($offset) |
||
| 226 | ->limit($per_page) |
||
| 227 | ->orderBy([$sortField => $sortType]) |
||
| 228 | ->filterWhere(['like', 'redirectSrcUrl', $filter]) |
||
| 229 | ->orFilterWhere(['like', 'redirectDestUrl', $filter]); |
||
| 230 | if ((int)$siteId !== 0) { |
||
| 231 | $query->andWhere(['siteId' => $siteId]); |
||
| 232 | } |
||
| 233 | if ($shortLinks) { |
||
| 234 | $query->andWhere(['not', ['associatedElementId' => 0]]); |
||
| 235 | } else { |
||
| 236 | $query->andWhere(['associatedElementId' => 0]); |
||
| 237 | } |
||
| 238 | $redirects = $query->all(); |
||
| 239 | // Add in the `deleteLink` field and clean up the redirects |
||
| 240 | foreach ($redirects as &$redirect) { |
||
| 241 | // Handle short links by adding the element's title and CP URL |
||
| 242 | if ($shortLinks) { |
||
| 243 | $redirect['elementTitle'] = ''; |
||
| 244 | $redirect['elementCpUrl'] = ''; |
||
| 245 | $elementId = $redirect['associatedElementId'] ?? null; |
||
| 246 | $elementSiteId = $redirect['siteId'] ?? null; |
||
| 247 | if (!empty($elementId)) { |
||
| 248 | $element = Craft::$app->getElements()->getElementById($elementId, null, $elementSiteId); |
||
| 249 | if ($element) { |
||
| 250 | $element = ElementHelper::rootElement($element); |
||
| 251 | $redirect['elementTitle'] = $element->title; |
||
| 252 | $redirect['elementCpUrl'] = $element->getCpEditUrl(); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | // Make sure the destination URL is not a regex |
||
| 257 | if ($redirect['redirectMatchType'] !== 'exactmatch') { |
||
| 258 | if (preg_match("/\$\d+/", $redirect['redirectDestUrl'])) { |
||
| 259 | $redirect['redirectDestUrl'] = ''; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | // Handle extracting the site name |
||
| 263 | $redirect['siteName'] = Craft::t('retour', 'All Sites'); |
||
| 264 | if ($redirect['siteId']) { |
||
| 265 | $sites = Craft::$app->getSites(); |
||
| 266 | $site = $sites->getSiteById($redirect['siteId']); |
||
| 267 | if ($site) { |
||
| 268 | $redirect['siteName'] = $site->name; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | $redirect['editLink'] = UrlHelper::cpUrl('retour/edit-redirect/' . $redirect['id']); |
||
| 273 | } |
||
| 274 | // Format the data for the API |
||
| 275 | if ($redirects) { |
||
| 276 | $data['data'] = $redirects; |
||
| 277 | $count = $query->count(); |
||
| 278 | $data['links']['pagination'] = [ |
||
| 279 | 'total' => $count, |
||
| 280 | 'per_page' => $per_page, |
||
| 281 | 'current_page' => $page, |
||
| 282 | 'last_page' => ceil($count / $per_page), |
||
| 283 | 'next_page_url' => null, |
||
| 284 | 'prev_page_url' => null, |
||
| 285 | 'from' => $offset + 1, |
||
| 286 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
| 287 | ]; |
||
| 288 | } |
||
| 289 | |||
| 290 | return $this->asJson($data); |
||
| 291 | } |
||
| 296 |