Total Complexity | 63 |
Total Lines | 586 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TablesController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TablesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class TablesController extends Controller |
||
30 | { |
||
31 | // Constants |
||
32 | // ========================================================================= |
||
33 | |||
34 | // Protected Properties |
||
35 | // ========================================================================= |
||
36 | |||
37 | /** |
||
38 | * @var bool|array |
||
39 | */ |
||
40 | protected $allowAnonymous = []; |
||
41 | |||
42 | // Public Methods |
||
43 | // ========================================================================= |
||
44 | |||
45 | /** |
||
46 | * Handle requests for the performance index table |
||
47 | * |
||
48 | * @param string $sort |
||
49 | * @param int $page |
||
50 | * @param int $per_page |
||
51 | * @param string $filter |
||
52 | * @param string $start |
||
53 | * @param string $end |
||
54 | * @param int $siteId |
||
55 | * |
||
56 | * @return Response |
||
57 | * @throws ForbiddenHttpException |
||
58 | */ |
||
59 | public function actionPagesIndex( |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Handle requests for the performance detail table |
||
199 | * |
||
200 | * @param string $sort |
||
201 | * @param int $page |
||
202 | * @param int $per_page |
||
203 | * @param string $filter |
||
204 | * @param string $pageUrl |
||
205 | * @param string $start |
||
206 | * @param string $end |
||
207 | * @param int $siteId |
||
208 | * |
||
209 | * @return Response |
||
210 | * @throws ForbiddenHttpException |
||
211 | */ |
||
212 | public function actionPageDetail( |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Handle requests for the pages index table |
||
334 | * |
||
335 | * @param string $sort |
||
336 | * @param int $page |
||
337 | * @param int $per_page |
||
338 | * @param string $filter |
||
339 | * @param string $start |
||
340 | * @param string $end |
||
341 | * @param int $siteId |
||
342 | * |
||
343 | * @return Response |
||
344 | * @throws ForbiddenHttpException |
||
345 | */ |
||
346 | public function actionErrorsIndex( |
||
347 | string $start = '', |
||
348 | string $end = '', |
||
349 | string $sort = 'url|DESC', |
||
350 | int $page = 1, |
||
351 | int $per_page = 20, |
||
352 | $filter = '', |
||
353 | $siteId = 0 |
||
354 | ): Response { |
||
355 | PermissionHelper::controllerPermissionCheck('webperf:errors'); |
||
356 | $data = []; |
||
357 | $sortField = 'url'; |
||
358 | $sortType = 'DESC'; |
||
359 | // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
||
360 | $end = date('Y-m-d', strtotime($end.'+1 day')); |
||
361 | // Figure out the sorting type |
||
362 | if ($sort !== '') { |
||
363 | if (strpos($sort, '|') === false) { |
||
364 | $sortField = $sort; |
||
365 | } else { |
||
366 | list($sortField, $sortType) = explode('|', $sort); |
||
367 | } |
||
368 | } |
||
369 | $db = Craft::$app->getDb(); |
||
370 | // Query the db table |
||
371 | $offset = ($page - 1) * $per_page; |
||
372 | $query = (new Query()) |
||
373 | ->select([ |
||
374 | '[[url]]', |
||
375 | 'MIN([[title]]) as [[title]]', |
||
376 | 'MAX([[dateCreated]]) as [[latestErrorDate]]', |
||
377 | 'SUM([[type]] = \'craft\') as [[craftCount]]', |
||
378 | 'SUM([[type]] = \'boomerang\') as [[boomerangCount]]', |
||
379 | 'COUNT([[url]]) AS cnt', |
||
380 | ]) |
||
381 | ->from(['{{%webperf_error_samples}}']) |
||
382 | ->offset($offset) |
||
383 | ->where(['between', 'dateCreated', $start, $end]) |
||
384 | ; |
||
385 | if ($db->getIsMysql()) { |
||
386 | $query |
||
387 | ->addSelect([ |
||
388 | 'SUM([[type]] = \'craft\') as [[craftCount]]', |
||
389 | 'SUM([[type]] = \'boomerang\') as [[boomerangCount]]', |
||
390 | ]); |
||
391 | } |
||
392 | if ($db->getIsPgsql()) { |
||
393 | $query |
||
394 | ->addSelect([ |
||
395 | 'SUM(case when [[type]] = \'craft\' then 1 else 0 end) as [[craftCount]]', |
||
396 | 'SUM(case when [[type]] = \'boomerang\' then 1 else 0 end) as [[boomerangCount]]', |
||
397 | ]); |
||
398 | } |
||
399 | if ((int)$siteId !== 0) { |
||
400 | $query->andWhere(['siteId' => $siteId]); |
||
401 | } |
||
402 | if ($filter !== '') { |
||
403 | $query |
||
404 | ->andWhere(['like', 'url', $filter]) |
||
405 | ->orWhere(['like', 'title', $filter]) |
||
406 | ->orWhere(['like', 'pageErrors', $filter]) |
||
407 | ; |
||
408 | } |
||
409 | $query |
||
410 | ->orderBy("[[{$sortField}]] {$sortType}") |
||
411 | ->groupBy('url') |
||
412 | ->limit($per_page) |
||
413 | ; |
||
414 | |||
415 | $stats = $query->all(); |
||
416 | if ($stats) { |
||
417 | $user = Craft::$app->getUser()->getIdentity(); |
||
418 | // Massage the stats |
||
419 | foreach ($stats as &$stat) { |
||
420 | $stat['cnt'] = (int)$stat['cnt']; |
||
421 | $stat['craftCount'] = (int)$stat['craftCount']; |
||
422 | $stat['boomerangCount'] = (int)$stat['boomerangCount']; |
||
423 | // Decode any emojis in the title |
||
424 | if (!empty($stat['title'])) { |
||
425 | $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
||
426 | } |
||
427 | // Set up the appropriate helper links |
||
428 | $stat['deleteLink'] = UrlHelper::actionUrl('webperf/error-samples/delete-samples-by-url', [ |
||
429 | 'pageUrl' => $stat['url'], |
||
430 | 'siteId' => $siteId |
||
431 | ]); |
||
432 | $stat['detailPageUrl'] = UrlHelper::cpUrl('webperf/errors/page-detail', [ |
||
433 | 'pageUrl' => $stat['url'], |
||
434 | 'siteId' => $siteId, |
||
435 | ]); |
||
436 | // Override based on permissions |
||
437 | if (!$user->can('webperf:delete-error-samples')) { |
||
438 | $stat['deleteLink'] = ''; |
||
439 | } |
||
440 | if (!$user->can('webperf:errors-detail')) { |
||
441 | $stat['detailPageUrl'] = ''; |
||
442 | } |
||
443 | } |
||
444 | // Format the data for the API |
||
445 | $data['data'] = $stats; |
||
446 | $query = (new Query()) |
||
447 | ->select(['[[url]]']) |
||
448 | ->from(['{{%webperf_error_samples}}']) |
||
449 | ->groupBy('[[url]]') |
||
450 | ->where(['between', 'dateCreated', $start, $end]) |
||
451 | ; |
||
452 | if ($filter !== '') { |
||
453 | $query |
||
454 | ->andWhere(['like', 'url', $filter]) |
||
455 | ->orWhere(['like', 'title', $filter]) |
||
456 | ->orWhere(['like', 'pageErrors', $filter]) |
||
457 | ; |
||
458 | } |
||
459 | $count = $query->count(); |
||
460 | $data['links']['pagination'] = [ |
||
461 | 'total' => $count, |
||
462 | 'per_page' => $per_page, |
||
463 | 'current_page' => $page, |
||
464 | 'last_page' => ceil($count / $per_page), |
||
465 | 'next_page_url' => null, |
||
466 | 'prev_page_url' => null, |
||
467 | 'from' => $offset + 1, |
||
468 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
469 | ]; |
||
470 | } |
||
471 | |||
472 | return $this->asJson($data); |
||
473 | } |
||
474 | |||
475 | |||
476 | /** |
||
477 | * Handle requests for the performance detail table |
||
478 | * |
||
479 | * @param string $sort |
||
480 | * @param int $page |
||
481 | * @param int $per_page |
||
482 | * @param string $filter |
||
483 | * @param string $pageUrl |
||
484 | * @param string $start |
||
485 | * @param string $end |
||
486 | * @param int $siteId |
||
487 | * |
||
488 | * @return Response |
||
489 | * @throws ForbiddenHttpException |
||
490 | */ |
||
491 | public function actionErrorsDetail( |
||
615 | } |
||
616 | |||
620 |