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

ChartsController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 84
dl 0
loc 143
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B actionDashboard() 0 69 9
A actionWidget() 0 39 4
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 Craft;
0 ignored issues
show
Bug introduced by
The type Craft 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...
14
use nystudio107\webperf\helpers\Permission as PermissionHelper;
15
16
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...
17
use craft\helpers\ArrayHelper;
0 ignored issues
show
Bug introduced by
The type craft\helpers\ArrayHelper 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
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...
19
20
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...
21
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...
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @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...
25
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
26
 * @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...
27
 */
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...
28
class ChartsController extends Controller
29
{
30
    // Constants
31
    // =========================================================================
32
33
    // Protected Properties
34
    // =========================================================================
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @var    bool|array
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 4
Loading history...
38
     */
39
    protected $allowAnonymous = [
40
    ];
41
42
    // Public Methods
43
    // =========================================================================
44
45
    /**
46
     * The Dashboard chart
47
     *
48
     * @param string $range
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     * @param int    $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     *
51
     * @return Response
52
     * @throws ForbiddenHttpException
53
     */
54
    public function actionDashboard(string $range = 'day', int $siteId = 0): Response
55
    {
56
        PermissionHelper::controllerPermissionCheck('webperf:dashboard');
57
        $data = [];
58
        $days = 1;
59
        switch ($range) {
60
            case 'day':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
61
                $days = 1;
62
                break;
63
            case 'week':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
64
                $days = 7;
65
                break;
66
            case 'month':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
67
                $days = 30;
68
                break;
69
        }
70
        // Different dbs do it different ways
71
        $stats = null;
72
        $db = Craft::$app->getDb();
73
        if ($db->getIsMysql()) {
74
            // Query the db
75
            $query = (new Query())
76
                ->from('{{%retour_stats}}')
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
                    "DATE_FORMAT(hitLastTime, '%Y-%m-%d') AS date_formatted",
79
                    'COUNT(redirectSrcUrl) AS cnt',
80
                    'COUNT(handledByRetour = 1 or null) as handled_cnt',
81
                ])
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...
82
                ->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )");
83
            if ((int)$siteId !== 0) {
84
                $query->andWhere(['siteId' => $siteId]);
85
            }
86
            $query
87
                ->orderBy('date_formatted ASC')
88
                ->groupBy('date_formatted');
89
            $stats = $query->all();
90
        }
91
        if ($db->getIsPgsql()) {
92
            // Query the db
93
            $query = (new Query())
94
                ->from('{{%retour_stats}}')
95
                ->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...
96
                    "to_char(\"hitLastTime\", 'yyyy-mm-dd') AS date_formatted",
97
                    "COUNT(\"redirectSrcUrl\") AS cnt",
98
                    "COUNT(CASE WHEN \"handledByRetour\" = true THEN 1 END) as handled_cnt",
99
                ])
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...
100
                ->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )");
101
            if ((int)$siteId !== 0) {
102
                $query->andWhere(['siteId' => $siteId]);
103
            }
104
            $query
105
                ->orderBy('date_formatted ASC')
106
                ->groupBy('date_formatted');
107
            $stats = $query->all();
108
        }
109
        if ($stats) {
110
            $data[] = [
111
                'name' => '404 hits',
112
                'data' => array_merge(['0'], ArrayHelper::getColumn($stats, 'cnt')),
113
                'labels' => array_merge(['-'], ArrayHelper::getColumn($stats, 'date_formatted')),
114
            ];
115
            $data[] = [
116
                'name' => 'Handled 404 hits',
117
                'data' => array_merge(['0'], ArrayHelper::getColumn($stats, 'handled_cnt')),
118
                'labels' => array_merge(['-'], ArrayHelper::getColumn($stats, 'date_formatted')),
119
            ];
120
        }
121
122
        return $this->asJson($data);
123
    }
124
125
    /**
126
     * The Dashboard chart
127
     *
128
     * @param int $days
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     *
130
     * @return Response
131
     */
132
    public function actionWidget($days = 1): Response
133
    {
134
        $data = [];
135
        // Different dbs do it different ways
136
        $stats = null;
137
        $handledStats = null;
138
        $db = Craft::$app->getDb();
139
        if ($db->getIsMysql()) {
140
            // Query the db
141
            $stats = (new Query())
142
                ->from('{{%retour_stats}}')
143
                ->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )")
144
                ->count();
145
            $handledStats = (new Query())
146
                ->from('{{%retour_stats}}')
147
                ->where("hitLastTime >= ( CURDATE() - INTERVAL '{$days}' DAY )")
148
                ->andWhere('handledByRetour is TRUE')
149
                ->count();
150
        }
151
        if ($db->getIsPgsql()) {
152
            // Query the db
153
            $stats = (new Query())
154
                ->from('{{%retour_stats}}')
155
                ->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )")
156
                ->count();
157
            $handledStats = (new Query())
158
                ->from('{{%retour_stats}}')
159
                ->where("\"hitLastTime\" >= ( CURRENT_TIMESTAMP - INTERVAL '{$days} days' )")
160
                ->andWhere('"handledByRetour" = TRUE')
161
                ->count();
162
        }
163
        if ($stats) {
164
            $data = [
165
                (int)$stats,
166
                (int)$handledStats,
167
            ];
168
        }
169
170
        return $this->asJson($data);
171
    }
172
173
    // Protected Methods
174
    // =========================================================================
175
}
176