Passed
Push — v1 ( 262f67...f3f274 )
by Andrew
09:29 queued 05:21
created

TablesController::actionPagesIndex()   B

Complexity

Conditions 10
Paths 60

Size

Total Lines 79
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

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