Conditions | 10 |
Paths | 60 |
Total Lines | 67 |
Code Lines | 45 |
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 |
||
56 | public function actionDashboard( |
||
57 | string $sort = 'hitCount|desc', |
||
58 | int $page = 1, |
||
59 | int $per_page = 20, |
||
60 | $filter = '', |
||
61 | $siteId = 0 |
||
62 | ): Response { |
||
63 | PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
||
64 | $data = []; |
||
65 | $sortField = 'hitCount'; |
||
66 | $sortType = 'DESC'; |
||
67 | // Figure out the sorting type |
||
68 | if ($sort !== '') { |
||
69 | if (strpos($sort, '|') === false) { |
||
70 | $sortField = $sort; |
||
71 | } else { |
||
72 | list($sortField, $sortType) = explode('|', $sort); |
||
73 | } |
||
74 | } |
||
75 | // Query the db table |
||
76 | $offset = ($page - 1) * $per_page; |
||
77 | $query = (new Query()) |
||
78 | ->from(['{{%retour_stats}}']) |
||
79 | ->offset($offset) |
||
80 | ->limit($per_page) |
||
81 | ->orderBy("{$sortField} {$sortType}"); |
||
82 | if ((int)$siteId !== 0) { |
||
83 | $query->where(['siteId' => $siteId]); |
||
84 | } |
||
85 | if ($filter !== '') { |
||
86 | $query->where(['like', 'redirectSrcUrl', $filter]); |
||
87 | $query->orWhere(['like', 'referrerUrl', $filter]); |
||
88 | } |
||
89 | $stats = $query->all(); |
||
90 | if ($stats) { |
||
91 | // Add in the `addLink` field |
||
92 | foreach ($stats as &$stat) { |
||
93 | $stat['addLink'] = ''; |
||
94 | if (!$stat['handledByRetour']) { |
||
95 | $encodedUrl = urlencode('/'.ltrim($stat['redirectSrcUrl'], '/')); |
||
96 | $stat['addLink'] = UrlHelper::cpUrl('retour/add-redirect', [ |
||
97 | 'defaultUrl' => $encodedUrl |
||
98 | ]); |
||
99 | } |
||
100 | } |
||
101 | // Format the data for the API |
||
102 | $data['data'] = $stats; |
||
103 | $query = (new Query()) |
||
104 | ->from(['{{%retour_stats}}']); |
||
105 | if ($filter !== '') { |
||
106 | $query->where(['like', 'redirectSrcUrl', $filter]); |
||
107 | $query->orWhere(['like', 'referrerUrl', $filter]); |
||
108 | } |
||
109 | $count = $query->count(); |
||
110 | $data['links']['pagination'] = [ |
||
111 | 'total' => $count, |
||
112 | 'per_page' => $per_page, |
||
113 | 'current_page' => $page, |
||
114 | 'last_page' => ceil($count / $per_page), |
||
115 | 'next_page_url' => null, |
||
116 | 'prev_page_url' => null, |
||
117 | 'from' => $offset + 1, |
||
118 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
119 | ]; |
||
120 | } |
||
121 | |||
122 | return $this->asJson($data); |
||
123 | } |
||
204 |