| Conditions | 19 |
| Paths | 120 |
| Total Lines | 124 |
| Code Lines | 86 |
| 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 |
||
| 55 | public function actionPagesIndex( |
||
| 56 | string $sort = 'pageLoad|desc', |
||
| 57 | int $page = 1, |
||
| 58 | int $per_page = 20, |
||
| 59 | $filter = '', |
||
| 60 | $siteId = 0 |
||
| 61 | ): Response { |
||
| 62 | PermissionHelper::controllerPermissionCheck('webperf:pages'); |
||
| 63 | $data = []; |
||
| 64 | $sortField = 'pageLoad'; |
||
| 65 | $sortType = 'DESC'; |
||
| 66 | // Figure out the sorting type |
||
| 67 | if ($sort !== '') { |
||
| 68 | if (strpos($sort, '|') === false) { |
||
| 69 | $sortField = $sort; |
||
| 70 | } else { |
||
| 71 | list($sortField, $sortType) = explode('|', $sort); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | if ($sortField === 'totalPageLoad') { |
||
| 75 | $sortField = 'pageLoad'; |
||
| 76 | } |
||
| 77 | // Query the db table |
||
| 78 | $offset = ($page - 1) * $per_page; |
||
| 79 | $query = (new Query()) |
||
| 80 | ->select([ |
||
| 81 | 'url', |
||
| 82 | 'MIN(title) AS title', |
||
| 83 | 'COUNT(url) AS cnt', |
||
| 84 | 'AVG(pageLoad) AS pageLoad', |
||
| 85 | 'AVG(domInteractive) AS domInteractive', |
||
| 86 | 'AVG(firstContentfulPaint) AS firstContentfulPaint', |
||
| 87 | 'AVG(firstPaint) AS firstPaint', |
||
| 88 | 'AVG(firstByte) AS firstByte', |
||
| 89 | 'AVG(connect) AS connect', |
||
| 90 | 'AVG(dns) AS dns', |
||
| 91 | 'AVG(craftTotalMs) AS craftTotalMs', |
||
| 92 | 'AVG(craftDbCnt) AS craftDbCnt', |
||
| 93 | 'AVG(craftDbMs) AS craftDbMs', |
||
| 94 | 'AVG(craftTwigCnt) AS craftTwigCnt', |
||
| 95 | 'AVG(craftTwigMs) AS craftTwigMs', |
||
| 96 | 'AVG(craftTotalMemory) AS craftTotalMemory', |
||
| 97 | ]) |
||
| 98 | ->from(['{{%webperf_data_samples}}']) |
||
| 99 | ->offset($offset) |
||
| 100 | ; |
||
| 101 | if ((int)$siteId !== 0) { |
||
| 102 | $query->where(['siteId' => $siteId]); |
||
| 103 | } |
||
| 104 | if ($filter !== '') { |
||
| 105 | $query |
||
| 106 | ->where(['like', 'url', $filter]) |
||
| 107 | ->orWhere(['like', 'title', $filter]) |
||
| 108 | ; |
||
| 109 | } |
||
| 110 | $query |
||
| 111 | ->orderBy("{$sortField} {$sortType}") |
||
| 112 | ->groupBy('url') |
||
| 113 | ->limit($per_page) |
||
| 114 | ; |
||
| 115 | |||
| 116 | $stats = $query->all(); |
||
| 117 | if ($stats) { |
||
| 118 | // Compute the largest page load time |
||
| 119 | $maxTotalPageLoad = 0; |
||
| 120 | foreach ($stats as &$stat) { |
||
| 121 | // Determine the stat type |
||
| 122 | if (!empty($stat['pageLoad']) && !empty($stat['craftTotalMs'])) { |
||
| 123 | $stat['type'] = 'both'; |
||
| 124 | } |
||
| 125 | if (empty($stat['pageLoad'])) { |
||
| 126 | $stat['type'] = 'craft'; |
||
| 127 | } |
||
| 128 | if (empty($stat['craftTotalMs'])) { |
||
| 129 | $stat['type'] = 'frontend'; |
||
| 130 | } |
||
| 131 | if (empty($stat['pageLoad'])) { |
||
| 132 | $pageLoad = $stat['craftTotalMs']; |
||
| 133 | } else { |
||
| 134 | $pageLoad = $stat['pageLoad']; |
||
| 135 | } |
||
| 136 | if ($pageLoad > $maxTotalPageLoad) { |
||
| 137 | $maxTotalPageLoad = $pageLoad; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | // Massage the stats |
||
| 141 | $index = 1; |
||
| 142 | foreach ($stats as &$stat) { |
||
| 143 | $stat['id'] = $index++; |
||
| 144 | $stat['maxTotalPageLoad'] = (int)$maxTotalPageLoad; |
||
| 145 | // If there is no frontend beacon timing, use the Craft timing |
||
| 146 | if (empty($stat['pageLoad'])) { |
||
| 147 | $stat['totalPageLoad'] = (int)$stat['craftTotalMs']; |
||
| 148 | } else { |
||
| 149 | $stat['totalPageLoad'] = (int)$stat['pageLoad']; |
||
| 150 | } |
||
| 151 | // Decode any emojis in the title |
||
| 152 | if (!empty($stat['title'])) { |
||
| 153 | $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | // Format the data for the API |
||
| 157 | $data['data'] = $stats; |
||
| 158 | $query = (new Query()) |
||
| 159 | ->from(['{{%webperf_data_samples}}']) |
||
| 160 | ->groupBy('url'); |
||
| 161 | if ($filter !== '') { |
||
| 162 | $query->where(['like', 'url', $filter]); |
||
| 163 | $query->orWhere(['like', 'title', $filter]); |
||
| 164 | } |
||
| 165 | $count = $query->count(); |
||
| 166 | $data['links']['pagination'] = [ |
||
| 167 | 'total' => $count, |
||
| 168 | 'per_page' => $per_page, |
||
| 169 | 'current_page' => $page, |
||
| 170 | 'last_page' => ceil($count / $per_page), |
||
| 171 | 'next_page_url' => null, |
||
| 172 | 'prev_page_url' => null, |
||
| 173 | 'from' => $offset + 1, |
||
| 174 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
| 175 | ]; |
||
| 176 | } |
||
| 177 | |||
| 178 | return $this->asJson($data); |
||
| 179 | } |
||
| 314 |