Passed
Push — v1 ( 887199...d7f64b )
by Andrew
17:42 queued 08:53
created

TablesController::actionRedirects()   B

Complexity

Conditions 9
Paths 120

Size

Total Lines 62
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 62
rs 7.5591
c 0
b 0
f 0
cc 9
nc 120
nop 5

How to fix   Long Method   

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
41
    // Public Methods
42
    // =========================================================================
43
44
    /**
45
     * Handle requests for the dashboard statistics table
46
     *
47
     * @param string $sort
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
48
     * @param int    $page
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     * @param int    $per_page
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     * @param string $filter
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
51
     * @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...
52
     *
53
     * @return Response
54
     * @throws ForbiddenHttpException
55
     */
56
    public function actionDashboard(
57
        string $sort = 'hitCount|desc',
58
        int $page = 1,
59
        int $per_page = 20,
60
        $filter = '',
61
        $siteId = 0
62
    ): Response {
63
        PermissionHelper::controllerPermissionCheck('webperf:dashboard');
64
        $data = [];
65
        $sortField = 'hitCount';
66
        $sortType = 'DESC';
67
        // Figure out the sorting type
68
        if ($sort !== '') {
69
            if (strpos($sort, '|') === false) {
70
                $sortField = $sort;
71
            } else {
72
                list($sortField, $sortType) = explode('|', $sort);
73
            }
74
        }
75
        // Query the db table
76
        $offset = ($page - 1) * $per_page;
77
        $query = (new Query())
78
            ->from(['{{%retour_stats}}'])
79
            ->offset($offset)
80
            ->limit($per_page)
81
            ->orderBy("{$sortField} {$sortType}");
82
        if ((int)$siteId !== 0) {
83
            $query->where(['siteId' => $siteId]);
84
        }
85
        if ($filter !== '') {
86
            $query->where(['like', 'redirectSrcUrl', $filter]);
87
            $query->orWhere(['like', 'referrerUrl', $filter]);
88
        }
89
        $stats = $query->all();
90
        if ($stats) {
91
            // Add in the `addLink` field
92
            foreach ($stats as &$stat) {
93
                $stat['addLink'] = '';
94
                if (!$stat['handledByRetour']) {
95
                    $encodedUrl = urlencode('/'.ltrim($stat['redirectSrcUrl'], '/'));
96
                    $stat['addLink'] = UrlHelper::cpUrl('retour/add-redirect', [
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...
97
                        'defaultUrl' => $encodedUrl
98
                    ]);
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...
99
                }
100
            }
101
            // Format the data for the API
102
            $data['data'] = $stats;
103
            $query = (new Query())
104
                ->from(['{{%retour_stats}}']);
105
            if ($filter !== '') {
106
                $query->where(['like', 'redirectSrcUrl', $filter]);
107
                $query->orWhere(['like', 'referrerUrl', $filter]);
108
            }
109
            $count = $query->count();
110
            $data['links']['pagination'] = [
111
                'total' => $count,
112
                'per_page' => $per_page,
113
                'current_page' => $page,
114
                'last_page' => ceil($count / $per_page),
115
                'next_page_url' => null,
116
                'prev_page_url' => null,
117
                'from' => $offset + 1,
118
                'to' => $offset + ($count > $per_page ? $per_page : $count),
119
            ];
120
        }
121
122
        return $this->asJson($data);
123
    }
124
125
    /**
126
     * Handle requests for the dashboard redirects table
127
     *
128
     * @param string $sort
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     * @param int    $page
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
130
     * @param int    $per_page
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
131
     * @param string $filter
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
132
     * @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...
133
     *
134
     * @return Response
135
     * @throws ForbiddenHttpException
136
     */
137
    public function actionRedirects(
138
        string $sort = 'hitCount|desc',
139
        int $page = 1,
140
        int $per_page = 20,
141
        $filter = '',
142
        $siteId = 0
143
    ): Response {
144
        PermissionHelper::controllerPermissionCheck('webperf:redirects');
145
        $data = [];
146
        $sortField = 'hitCount';
147
        $sortType = 'DESC';
148
        // Figure out the sorting type
149
        if ($sort !== '') {
150
            if (strpos($sort, '|') === false) {
151
                $sortField = $sort;
152
            } else {
153
                list($sortField, $sortType) = explode('|', $sort);
154
            }
155
        }
156
        // Query the db table
157
        $offset = ($page - 1) * $per_page;
158
        $query = (new Query())
159
            ->from(['{{%retour_static_redirects}}'])
160
            ->offset($offset)
161
            ->limit($per_page)
162
            ->orderBy("{$sortField} {$sortType}");
163
        if ((int)$siteId !== 0) {
164
            $query->where(['siteId' => $siteId]);
165
        }
166
        if ($filter !== '') {
167
            $query->where(['like', 'redirectSrcUrl', $filter]);
168
            $query->orWhere(['like', 'redirectDestUrl', $filter]);
169
        }
170
        $redirects = $query->all();
171
        // Add in the `deleteLink` field
172
        foreach ($redirects as &$redirect) {
173
            $redirect['deleteLink'] = UrlHelper::cpUrl('retour/delete-redirect/'.$redirect['id']);
174
            $redirect['editLink'] = UrlHelper::cpUrl('retour/edit-redirect/'.$redirect['id']);
175
        }
176
        // Format the data for the API
177
        if ($redirects) {
178
            $data['data'] = $redirects;
179
            $query = (new Query())
180
                ->from(['{{%retour_static_redirects}}']);
181
            if ($filter !== '') {
182
                $query->where(['like', 'redirectSrcUrl', $filter]);
183
                $query->orWhere(['like', 'redirectDestUrl', $filter]);
184
            }
185
            $count = $query->count();
186
            $data['links']['pagination'] = [
187
                'total' => $count,
188
                'per_page' => $per_page,
189
                'current_page' => $page,
190
                'last_page' => ceil($count / $per_page),
191
                'next_page_url' => null,
192
                'prev_page_url' => null,
193
                'from' => $offset + 1,
194
                'to' => $offset + ($count > $per_page ? $per_page : $count),
195
            ];
196
        }
197
198
        return $this->asJson($data);
199
    }
200
201
    // Protected Methods
202
    // =========================================================================
203
}
204