Conditions | 15 |
Paths | 39 |
Total Lines | 89 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
91 | public function actionDashboard( |
||
92 | string $sort = 'hitCount|desc', |
||
93 | int $page = 1, |
||
94 | int $per_page = 20, |
||
95 | $filter = '', |
||
96 | $siteId = 0, |
||
97 | $handled = 'all', |
||
98 | ): Response { |
||
99 | PermissionHelper::controllerPermissionCheck('retour:dashboard'); |
||
100 | $data = []; |
||
101 | $sortField = 'hitCount'; |
||
102 | $sortType = 'DESC'; |
||
103 | // Figure out the sorting type |
||
104 | if ($sort !== '') { |
||
105 | if (strpos($sort, '|') === false) { |
||
106 | $sortField = $sort; |
||
107 | } else { |
||
108 | list($sortField, $sortType) = explode('|', $sort); |
||
109 | } |
||
110 | } |
||
111 | $sortType = strtoupper($sortType); |
||
112 | $sortType = self::SORT_MAP[$sortType] ?? self::SORT_MAP['DESC']; |
||
113 | // Validate untrusted data |
||
114 | if (!in_array($sortField, self::ALLOWED_STATS_SORT_FIELDS, true)) { |
||
115 | throw new BadRequestHttpException(Craft::t('retour', 'Invalid sort field specified.')); |
||
116 | } |
||
117 | // Query the db table |
||
118 | $offset = ($page - 1) * $per_page; |
||
119 | $query = (new Query()) |
||
120 | ->from(['{{%retour_stats}}']) |
||
121 | ->offset($offset) |
||
122 | ->limit($per_page) |
||
123 | ->orderBy([$sortField => $sortType]) |
||
124 | ->filterWhere(['like', 'redirectSrcUrl', $filter]) |
||
125 | ->orFilterWhere(['like', 'referrerUrl', $filter]); |
||
126 | if ((int)$siteId !== 0) { |
||
127 | $query->andWhere(['siteId' => $siteId]); |
||
128 | } |
||
129 | if ($handled !== 'all') { |
||
130 | $query->andWhere(['handledByRetour' => self::HANDLED_MAP[$handled]]); |
||
131 | } |
||
132 | $stats = $query->all(); |
||
133 | if ($stats) { |
||
134 | // Add in the `addLink` field |
||
135 | foreach ($stats as &$stat) { |
||
136 | // Normalize the `redirectSrcUrl` to point to a valid frontend site URL |
||
137 | $stat['redirectSrcUrlFull'] = $stat['redirectSrcUrl']; |
||
138 | if (!UrlHelper::isAbsoluteUrl($stat['redirectSrcUrlFull'])) { |
||
139 | $sites = Craft::$app->getSites(); |
||
140 | $site = $sites->getSiteById($stat['siteId'], true); |
||
141 | if ($site) { |
||
142 | $stat['redirectSrcUrlFull'] = UrlHelper::mergeUrlWithPath($site->baseUrl, $stat['redirectSrcUrlFull']); |
||
143 | } |
||
144 | } |
||
145 | $stat['addLink'] = ''; |
||
146 | if (!$stat['handledByRetour']) { |
||
147 | $encodedUrl = urlencode('/' . ltrim($stat['redirectSrcUrl'], '/')); |
||
148 | // Add the siteId to the URL, but keep the current behavior of passing in siteId=0 for "all" |
||
149 | $statSiteId = $stat['siteId'] ?? 0; |
||
150 | try { |
||
151 | $primarySite = Craft::$app->getSites()->getPrimarySite(); |
||
152 | } catch (SiteNotFoundException $e) { |
||
153 | $primarySite = null; |
||
154 | } |
||
155 | if ($primarySite !== null && $statSiteId == (int)$primarySite->id) { |
||
156 | $statSiteId = 0; |
||
157 | } |
||
158 | $stat['addLink'] = UrlHelper::cpUrl('retour/add-redirect', [ |
||
159 | 'defaultUrl' => $encodedUrl, |
||
160 | 'siteId' => $statSiteId, |
||
161 | ]); |
||
162 | } |
||
163 | } |
||
164 | // Format the data for the API |
||
165 | $data['data'] = $stats; |
||
166 | $count = $query->count(); |
||
167 | $data['links']['pagination'] = [ |
||
168 | 'total' => $count, |
||
169 | 'per_page' => $per_page, |
||
170 | 'current_page' => $page, |
||
171 | 'last_page' => ceil($count / $per_page), |
||
172 | 'next_page_url' => null, |
||
173 | 'prev_page_url' => null, |
||
174 | 'from' => $offset + 1, |
||
175 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
176 | ]; |
||
177 | } |
||
178 | |||
179 | return $this->asJson($data); |
||
180 | } |
||
296 |