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