Passed
Push — v1 ( 601be5...b67f55 )
by Andrew
03:08
created

SectionsController::actionAlerts()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 44
rs 9.456
c 0
b 0
f 0
cc 3
nc 4
nop 1
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) 2019 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\Webperf;
14
use nystudio107\webperf\assetbundles\webperf\WebperfDashboardAsset;
15
use nystudio107\webperf\helpers\MultiSite as MultiSiteHelper;
16
use nystudio107\webperf\helpers\Permission as PermissionHelper;
17
18
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...
19
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...
20
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...
21
22
use yii\base\InvalidConfigException;
0 ignored issues
show
Bug introduced by
The type yii\base\InvalidConfigException 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...
23
use yii\web\NotFoundHttpException;
0 ignored issues
show
Bug introduced by
The type yii\web\NotFoundHttpException 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...
24
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...
25
26
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
 * @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...
28
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
29
 * @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...
30
 */
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...
31
class SectionsController extends Controller
32
{
33
    // Constants
34
    // =========================================================================
35
36
    const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-webperf/';
37
38
    // Protected Properties
39
    // =========================================================================
40
41
    protected $allowAnonymous = [];
42
43
    // Public Methods
44
    // =========================================================================
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
47
     * @param string|null $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
48
     * @param bool        $showWelcome
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     *
50
     * @return Response
51
     * @throws NotFoundHttpException
52
     * @throws \yii\web\ForbiddenHttpException
53
     */
54
    public function actionDashboard(string $siteHandle = null, bool $showWelcome = false): Response
55
    {
56
        $variables = [];
57
        PermissionHelper::controllerPermissionCheck('webperf:dashboard');
58
        // Trim the statistics
59
        Webperf::$plugin->dataSamples->trimOrphanedSamples(1024);
60
        Webperf::$plugin->dataSamples->trimDataSamples();
61
        // Get the site to edit
62
        $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle);
63
        $pluginName = Webperf::$settings->pluginName;
64
        $templateTitle = Craft::t('webperf', 'Dashboard');
65
        $view = Craft::$app->getView();
66
        // Asset bundle
67
        try {
68
            $view->registerAssetBundle(WebperfDashboardAsset::class);
69
        } catch (InvalidConfigException $e) {
70
            Craft::error($e->getMessage(), __METHOD__);
71
        }
72
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
73
            '@nystudio107/webperf/assetbundles/webperf/dist',
74
            true
75
        );
76
        // Enabled sites
77
        MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables);
78
        $variables['controllerHandle'] = 'dashboard';
79
        // Basic variables
80
        $variables['fullPageForm'] = false;
81
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
82
        $variables['pluginName'] = $pluginName;
83
        $variables['title'] = $templateTitle;
84
        $siteHandleUri = Craft::$app->isMultiSite ? '/'.$siteHandle : '';
85
        $variables['crumbs'] = [
86
            [
87
                'label' => $pluginName,
88
                'url' => UrlHelper::cpUrl('webperf'),
89
            ],
90
            [
91
                'label' => $templateTitle,
92
                'url' => UrlHelper::cpUrl('webperf/dashboard'.$siteHandleUri),
93
            ],
94
        ];
95
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
96
        $variables['selectedSubnavItem'] = 'dashboard';
97
        $variables['showWelcome'] = $showWelcome;
98
        $variables['settings'] = Webperf::$settings;
99
        // Set the default date range
100
        $now = new \DateTime();
101
        $variables['end'] = $now->format('Y-m-d');
102
        $variables['start'] = $now->modify('-1 year')->format('Y-m-d');
103
104
        // Render the template
105
        return $this->renderTemplate('webperf/dashboard/index', $variables);
106
    }
107
108
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
109
     * @param string|null $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
110
     *
111
     * @return Response
112
     * @throws NotFoundHttpException
113
     * @throws \yii\web\ForbiddenHttpException
114
     */
115
    public function actionPagesIndex(string $siteHandle = null): Response
116
    {
117
        $variables = [];
118
        PermissionHelper::controllerPermissionCheck('webperf:performance');
119
        // Trim the statistics
120
        Webperf::$plugin->dataSamples->trimOrphanedSamples(1024);
121
        Webperf::$plugin->dataSamples->trimDataSamples();
122
        // Get the site to edit
123
        $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle);
124
        $pluginName = Webperf::$settings->pluginName;
125
        $templateTitle = Craft::t('webperf', 'Performance');
126
        $view = Craft::$app->getView();
127
        // Asset bundle
128
        try {
129
            $view->registerAssetBundle(WebperfDashboardAsset::class);
130
        } catch (InvalidConfigException $e) {
131
            Craft::error($e->getMessage(), __METHOD__);
132
        }
133
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
134
            '@nystudio107/webperf/assetbundles/webperf/dist',
135
            true
136
        );
137
        // Enabled sites
138
        MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables);
139
        $variables['controllerHandle'] = 'performance';
140
141
        // Basic variables
142
        $variables['fullPageForm'] = false;
143
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
144
        $variables['pluginName'] = $pluginName;
145
        $variables['title'] = $templateTitle;
146
        $siteHandleUri = Craft::$app->isMultiSite ? '/'.$siteHandle : '';
147
        $variables['crumbs'] = [
148
            [
149
                'label' => $pluginName,
150
                'url' => UrlHelper::cpUrl('webperf'),
151
            ],
152
            [
153
                'label' => $templateTitle,
154
                'url' => UrlHelper::cpUrl('webperf/performance'.$siteHandleUri),
155
            ],
156
        ];
157
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
158
        $variables['selectedSubnavItem'] = 'performance';
159
        $variables['settings'] = Webperf::$settings;
160
        // Set the default date range
161
        $now = new \DateTime();
162
        $variables['end'] = $now->format('Y-m-d');
163
        $variables['start'] = $now->modify('-1 year')->format('Y-m-d');
164
165
        // Render the template
166
        return $this->renderTemplate('webperf/performance/index', $variables);
167
    }
168
169
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
170
     * @param string      $pageUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
171
     * @param string|null $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
172
     *
173
     * @return Response
174
     * @throws NotFoundHttpException
175
     * @throws \yii\web\ForbiddenHttpException
176
     */
177
    public function actionPageDetail(string $pageUrl, string $siteHandle = null): Response
178
    {
179
        $variables = [];
180
        PermissionHelper::controllerPermissionCheck('webperf:performance-detail');
181
        // Trim the statistics
182
        Webperf::$plugin->dataSamples->trimOrphanedSamples(1024);
183
        Webperf::$plugin->dataSamples->trimDataSamples();
184
        // Get the site to edit
185
        $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle);
186
        $pluginName = Webperf::$settings->pluginName;
187
        $templateTitle = Craft::t('webperf', 'Performance Detail');
188
        $view = Craft::$app->getView();
189
        // Asset bundle
190
        try {
191
            $view->registerAssetBundle(WebperfDashboardAsset::class);
192
        } catch (InvalidConfigException $e) {
193
            Craft::error($e->getMessage(), __METHOD__);
194
        }
195
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
196
            '@nystudio107/webperf/assetbundles/webperf/dist',
197
            true
198
        );
199
        // Enabled sites
200
        MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables);
201
        $variables['controllerHandle'] = 'performance/page-detail';
202
203
        // Basic variables
204
        $variables['fullPageForm'] = false;
205
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
206
        $variables['pluginName'] = $pluginName;
207
        $variables['title'] = $templateTitle;
208
        $siteHandleUri = Craft::$app->isMultiSite ? '/'.$siteHandle : '';
209
        $variables['crumbs'] = [
210
            [
211
                'label' => $pluginName,
212
                'url' => UrlHelper::cpUrl('webperf'),
213
            ],
214
            [
215
                'label' => Craft::t('webperf', 'Performance'),
216
                'url' => UrlHelper::cpUrl('webperf/performance'.$siteHandleUri),
217
            ],
218
            [
219
                'label' => $templateTitle,
220
                'url' => UrlHelper::cpUrl('webperf/performance/page-detail'.$siteHandleUri, [
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...
221
                    'pageUrl' => $pageUrl
222
                ]),
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...
223
            ],
224
        ];
225
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
226
        $variables['selectedSubnavItem'] = 'performance';
227
        $variables['pageUrl'] = $pageUrl;
228
        $variables['pageTitle'] = Webperf::$plugin->dataSamples->pageTitle($pageUrl, $siteId);
229
        $variables['settings'] = Webperf::$settings;
230
        // Set the default date range
231
        $now = new \DateTime();
232
        $variables['end'] = $now->format('Y-m-d');
233
        $variables['start'] = $now->modify('-1 year')->format('Y-m-d');
234
235
        // Render the template
236
        return $this->renderTemplate('webperf/performance/page-detail', $variables);
237
    }
238
239
240
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
241
     * @param string|null $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
242
     *
243
     * @return Response
244
     * @throws NotFoundHttpException
245
     * @throws \yii\web\ForbiddenHttpException
246
     */
247
    public function actionErrorsIndex(string $siteHandle = null): Response
248
    {
249
        $variables = [];
250
        PermissionHelper::controllerPermissionCheck('webperf:errors');
251
        // Trim the statistics
252
        Webperf::$plugin->errorSamples->trimErrorSamples();
253
        // Get the site to edit
254
        $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle);
255
        $pluginName = Webperf::$settings->pluginName;
256
        $templateTitle = Craft::t('webperf', 'Errors');
257
        $view = Craft::$app->getView();
258
        // Asset bundle
259
        try {
260
            $view->registerAssetBundle(WebperfDashboardAsset::class);
261
        } catch (InvalidConfigException $e) {
262
            Craft::error($e->getMessage(), __METHOD__);
263
        }
264
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
265
            '@nystudio107/webperf/assetbundles/webperf/dist',
266
            true
267
        );
268
        // Enabled sites
269
        MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables);
270
        $variables['controllerHandle'] = 'errors';
271
272
        // Basic variables
273
        $variables['fullPageForm'] = false;
274
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
275
        $variables['pluginName'] = $pluginName;
276
        $variables['title'] = $templateTitle;
277
        $siteHandleUri = Craft::$app->isMultiSite ? '/'.$siteHandle : '';
278
        $variables['crumbs'] = [
279
            [
280
                'label' => $pluginName,
281
                'url' => UrlHelper::cpUrl('webperf'),
282
            ],
283
            [
284
                'label' => $templateTitle,
285
                'url' => UrlHelper::cpUrl('webperf/errors'.$siteHandleUri),
286
            ],
287
        ];
288
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
289
        $variables['selectedSubnavItem'] = 'errors';
290
        $variables['settings'] = Webperf::$settings;
291
        // Set the default date range
292
        $now = new \DateTime();
293
        $variables['end'] = $now->format('Y-m-d');
294
        $variables['start'] = $now->modify('-1 year')->format('Y-m-d');
295
296
        // Render the template
297
        return $this->renderTemplate('webperf/errors/index', $variables);
298
    }
299
300
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
301
     * @param string      $pageUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
302
     * @param string|null $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
303
     *
304
     * @return Response
305
     * @throws NotFoundHttpException
306
     * @throws \yii\web\ForbiddenHttpException
307
     */
308
    public function actionErrorsDetail(string $pageUrl, string $siteHandle = null): Response
309
    {
310
        $variables = [];
311
        PermissionHelper::controllerPermissionCheck('webperf:errors-detail');
312
        // Trim the statistics
313
        Webperf::$plugin->errorSamples->trimErrorSamples();
314
        // Get the site to edit
315
        $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle);
316
        $pluginName = Webperf::$settings->pluginName;
317
        $templateTitle = Craft::t('webperf', 'Errors Detail');
318
        $view = Craft::$app->getView();
319
        // Asset bundle
320
        try {
321
            $view->registerAssetBundle(WebperfDashboardAsset::class);
322
        } catch (InvalidConfigException $e) {
323
            Craft::error($e->getMessage(), __METHOD__);
324
        }
325
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
326
            '@nystudio107/webperf/assetbundles/webperf/dist',
327
            true
328
        );
329
        // Enabled sites
330
        MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables);
331
        $variables['controllerHandle'] = 'errors/page-detail';
332
333
        // Basic variables
334
        $variables['fullPageForm'] = false;
335
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
336
        $variables['pluginName'] = $pluginName;
337
        $variables['title'] = $templateTitle;
338
        $siteHandleUri = Craft::$app->isMultiSite ? '/'.$siteHandle : '';
339
        $variables['crumbs'] = [
340
            [
341
                'label' => $pluginName,
342
                'url' => UrlHelper::cpUrl('webperf'),
343
            ],
344
            [
345
                'label' => Craft::t('webperf', 'Errors'),
346
                'url' => UrlHelper::cpUrl('webperf/errors'.$siteHandleUri),
347
            ],
348
            [
349
                'label' => $templateTitle,
350
                'url' => UrlHelper::cpUrl('webperf/errors/page-detail'.$siteHandleUri, [
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...
351
                    'pageUrl' => $pageUrl
352
                ]),
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...
353
            ],
354
        ];
355
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
356
        $variables['selectedSubnavItem'] = 'errors';
357
        $variables['pageUrl'] = $pageUrl;
358
        $variables['pageTitle'] = Webperf::$plugin->errorSamples->pageTitle($pageUrl, $siteId);
359
        $variables['settings'] = Webperf::$settings;
360
        // Set the default date range
361
        $now = new \DateTime();
362
        $variables['end'] = $now->format('Y-m-d');
363
        $variables['start'] = $now->modify('-1 year')->format('Y-m-d');
364
365
        // Render the template
366
        return $this->renderTemplate('webperf/errors/page-detail', $variables);
367
    }
368
369
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
370
     * @param string|null $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
371
     *
372
     * @return Response
373
     * @throws NotFoundHttpException
374
     * @throws \yii\web\ForbiddenHttpException
375
     */
376
    public function actionAlerts(string $siteHandle = null): Response
377
    {
378
        $variables = [];
379
        PermissionHelper::controllerPermissionCheck('webperf:alerts');
380
        // Get the site to edit
381
        $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle);
382
        $pluginName = Webperf::$settings->pluginName;
383
        $templateTitle = Craft::t('webperf', 'Alerts');
384
        $view = Craft::$app->getView();
385
        // Asset bundle
386
        try {
387
            $view->registerAssetBundle(WebperfDashboardAsset::class);
388
        } catch (InvalidConfigException $e) {
389
            Craft::error($e->getMessage(), __METHOD__);
390
        }
391
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
392
            '@nystudio107/webperf/assetbundles/webperf/dist',
393
            true
394
        );
395
        // Enabled sites
396
        MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables);
397
        $variables['controllerHandle'] = 'alerts';
398
        // Basic variables
399
        $variables['fullPageForm'] = false;
400
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
401
        $variables['pluginName'] = $pluginName;
402
        $variables['title'] = $templateTitle;
403
        $siteHandleUri = Craft::$app->isMultiSite ? '/'.$siteHandle : '';
404
        $variables['crumbs'] = [
405
            [
406
                'label' => $pluginName,
407
                'url' => UrlHelper::cpUrl('webperf'),
408
            ],
409
            [
410
                'label' => $templateTitle,
411
                'url' => UrlHelper::cpUrl('webperf/alerts'.$siteHandleUri),
412
            ],
413
        ];
414
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
415
        $variables['selectedSubnavItem'] = 'alerts';
416
        $variables['settings'] = Webperf::$settings;
417
418
        // Render the template
419
        return $this->renderTemplate('webperf/alerts/index', $variables);
420
    }
421
}
422