| Conditions | 7 |
| Paths | 32 |
| Total Lines | 61 |
| Code Lines | 47 |
| 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 |
||
| 54 | public function actionDashboard(string $range = 'day'): Response |
||
| 55 | { |
||
| 56 | PermissionHelper::controllerPermissionCheck('retour:dashboard'); |
||
| 57 | $data = []; |
||
| 58 | $days = 1; |
||
| 59 | switch ($range) { |
||
| 60 | case 'day': |
||
| 61 | $days = 1; |
||
| 62 | break; |
||
| 63 | case 'week': |
||
| 64 | $days = 7; |
||
| 65 | break; |
||
| 66 | case 'month': |
||
| 67 | $days = 30; |
||
| 68 | break; |
||
| 69 | } |
||
| 70 | // Different dbs do it different ways |
||
| 71 | $stats = null; |
||
| 72 | $db = Craft::$app->getDb(); |
||
| 73 | if ($db->getIsMysql()) { |
||
| 74 | // Query the db |
||
| 75 | $stats = (new Query()) |
||
| 76 | ->from('{{%retour_stats}}') |
||
| 77 | ->select([ |
||
| 78 | "DATE_FORMAT(hitLastTime, '%Y-%m-%d') AS date_formatted", |
||
| 79 | 'COUNT(redirectSrcUrl) AS cnt', |
||
| 80 | 'COUNT(handledByRetour = 1 or null) as handled_cnt', |
||
| 81 | ]) |
||
| 82 | ->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )") |
||
| 83 | ->orderBy('date_formatted ASC') |
||
| 84 | ->groupBy('date_formatted') |
||
| 85 | ->all(); |
||
| 86 | } |
||
| 87 | if ($db->getIsPgsql()) { |
||
| 88 | // Query the db |
||
| 89 | $stats = (new Query()) |
||
| 90 | ->from('{{%retour_stats}}') |
||
| 91 | ->select([ |
||
| 92 | "to_char(\"hitLastTime\", 'yyyy-mm-dd') AS date_formatted", |
||
| 93 | "COUNT(\"redirectSrcUrl\") AS cnt", |
||
| 94 | "COUNT(CASE WHEN \"handledByRetour\" = true THEN 1 END) as handled_cnt", |
||
| 95 | ]) |
||
| 96 | ->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )") |
||
| 97 | ->orderBy('date_formatted ASC') |
||
| 98 | ->groupBy('date_formatted') |
||
| 99 | ->all(); |
||
| 100 | } |
||
| 101 | if ($stats) { |
||
| 102 | $data[] = [ |
||
| 103 | 'name' => '404 hits', |
||
| 104 | 'data' => array_merge(['0'], ArrayHelper::getColumn($stats, 'cnt')), |
||
| 105 | 'labels' => array_merge(['-'], ArrayHelper::getColumn($stats, 'date_formatted')), |
||
| 106 | ]; |
||
| 107 | $data[] = [ |
||
| 108 | 'name' => 'Handled 404 hits', |
||
| 109 | 'data' => array_merge(['0'], ArrayHelper::getColumn($stats, 'handled_cnt')), |
||
| 110 | 'labels' => array_merge(['-'], ArrayHelper::getColumn($stats, 'date_formatted')), |
||
| 111 | ]; |
||
| 112 | } |
||
| 113 | |||
| 114 | return $this->asJson($data); |
||
| 115 | } |
||
| 150 |