Passed
Push — develop ( 204b7a...d9d9af )
by Andrew
03:18
created

TablesController::actionDashboard()   B

Complexity

Conditions 10
Paths 60

Size

Total Lines 67
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 67
rs 7.3333
c 0
b 0
f 0
cc 10
nc 60
nop 5

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Webperf plugin for Craft CMS 3.x
4
 *
5
 * Monitor the performance of your webpages through real-world user timing data
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\webperf\controllers;
12
13
use nystudio107\webperf\helpers\Permission as PermissionHelper;
14
15
use craft\db\Query;
0 ignored issues
show
Bug introduced by
The type craft\db\Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use craft\helpers\UrlHelper;
0 ignored issues
show
Bug introduced by
The type craft\helpers\UrlHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use craft\web\Controller;
0 ignored issues
show
Bug introduced by
The type craft\web\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
use yii\web\ForbiddenHttpException;
0 ignored issues
show
Bug introduced by
The type yii\web\ForbiddenHttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use yii\web\Response;
0 ignored issues
show
Bug introduced by
The type yii\web\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
24
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
25
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
26
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
27
class TablesController extends Controller
28
{
29
    // Constants
30
    // =========================================================================
31
32
    // Protected Properties
33
    // =========================================================================
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
     * @var    bool|array
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 4
Loading history...
37
     */
38
    protected $allowAnonymous = [];
39
40
    // Public Methods
41
    // =========================================================================
42
43
    /**
44
     * Handle requests for the dashboard statistics table
45
     *
46
     * @param string $sort
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
47
     * @param int    $page
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
48
     * @param int    $per_page
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     * @param string $filter
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     * @param null   $siteId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $siteId is correct as it would always require null to be passed?
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
51
     *
52
     * @return Response
53
     * @throws ForbiddenHttpException
54
     */
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
        // Query the db table
75
        $offset = ($page - 1) * $per_page;
76
        $query = (new Query())
77
            ->select([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
78
                'url',
79
                'MIN(title) AS title',
80
                'COUNT(url) AS cnt',
81
                'AVG(pageLoad) AS pageLoad',
82
                'AVG(craftDbCnt) AS craftDbCnt',
83
                'AVG(craftTwigCnt) AS craftTwigCnt',
84
                'AVG(craftOtherCnt) AS craftOtherCnt',
85
                'AVG(craftTotalMemory) AS craftTotalMemory',
86
            ])
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
87
            ->from(['{{%webperf_data_samples}}'])
88
            ->offset($offset);
89
        if ((int)$siteId !== 0) {
90
            $query->where(['siteId' => $siteId]);
91
        }
92
        if ($filter !== '') {
93
            $query->where(['like', 'url', $filter]);
94
            $query->orWhere(['like', 'title', $filter]);
95
        }
96
        $query
97
            ->orderBy("{$sortField} {$sortType}")
98
            ->groupBy('url')
99
            ->limit($per_page);
100
101
        $stats = $query->all();
102
        if ($stats) {
103
            // Decode any emojis in the title
104
            foreach ($stats as &$stat) {
105
                if (!empty($stat['title'])) {
106
                    $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8');
107
                }
108
            }
109
            // Format the data for the API
110
            $data['data'] = $stats;
111
            $query = (new Query())
112
                ->from(['{{%webperf_data_samples}}'])
113
                ->groupBy('url');
114
            if ($filter !== '') {
115
                $query->where(['like', 'url', $filter]);
116
                $query->orWhere(['like', 'title', $filter]);
117
            }
118
            $count = $query->count();
119
            $data['links']['pagination'] = [
120
                'total' => $count,
121
                'per_page' => $per_page,
122
                'current_page' => $page,
123
                'last_page' => ceil($count / $per_page),
124
                'next_page_url' => null,
125
                'prev_page_url' => null,
126
                'from' => $offset + 1,
127
                'to' => $offset + ($count > $per_page ? $per_page : $count),
128
            ];
129
        }
130
131
        return $this->asJson($data);
132
    }
133
134
    /**
135
     * Handle requests for the dashboard redirects table
136
     *
137
     * @param string $sort
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
138
     * @param int    $page
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
139
     * @param int    $per_page
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
140
     * @param string $filter
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
141
     * @param null   $siteId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $siteId is correct as it would always require null to be passed?
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
142
     *
143
     * @return Response
144
     * @throws ForbiddenHttpException
145
     */
146
    public function actionRedirects(
147
        string $sort = 'hitCount|desc',
148
        int $page = 1,
149
        int $per_page = 20,
150
        $filter = '',
151
        $siteId = 0
152
    ): Response {
153
        PermissionHelper::controllerPermissionCheck('webperf:redirects');
154
        $data = [];
155
        $sortField = 'hitCount';
156
        $sortType = 'DESC';
157
        // Figure out the sorting type
158
        if ($sort !== '') {
159
            if (strpos($sort, '|') === false) {
160
                $sortField = $sort;
161
            } else {
162
                list($sortField, $sortType) = explode('|', $sort);
163
            }
164
        }
165
        // Query the db table
166
        $offset = ($page - 1) * $per_page;
167
        $query = (new Query())
168
            ->from(['{{%retour_static_redirects}}'])
169
            ->offset($offset)
170
            ->limit($per_page)
171
            ->orderBy("{$sortField} {$sortType}");
172
        if ((int)$siteId !== 0) {
173
            $query->where(['siteId' => $siteId]);
174
        }
175
        if ($filter !== '') {
176
            $query->where(['like', 'redirectSrcUrl', $filter]);
177
            $query->orWhere(['like', 'redirectDestUrl', $filter]);
178
        }
179
        $redirects = $query->all();
180
        // Add in the `deleteLink` field
181
        foreach ($redirects as &$redirect) {
182
            $redirect['deleteLink'] = UrlHelper::cpUrl('retour/delete-redirect/'.$redirect['id']);
183
            $redirect['editLink'] = UrlHelper::cpUrl('retour/edit-redirect/'.$redirect['id']);
184
        }
185
        // Format the data for the API
186
        if ($redirects) {
187
            $data['data'] = $redirects;
188
            $query = (new Query())
189
                ->from(['{{%retour_static_redirects}}']);
190
            if ($filter !== '') {
191
                $query->where(['like', 'redirectSrcUrl', $filter]);
192
                $query->orWhere(['like', 'redirectDestUrl', $filter]);
193
            }
194
            $count = $query->count();
195
            $data['links']['pagination'] = [
196
                'total' => $count,
197
                'per_page' => $per_page,
198
                'current_page' => $page,
199
                'last_page' => ceil($count / $per_page),
200
                'next_page_url' => null,
201
                'prev_page_url' => null,
202
                'from' => $offset + 1,
203
                'to' => $offset + ($count > $per_page ? $per_page : $count),
204
            ];
205
        }
206
207
        return $this->asJson($data);
208
    }
209
210
    // Protected Methods
211
    // =========================================================================
212
}
213