Passed
Push — develop ( 04e54c...316322 )
by Andrew
04:08
created

SectionsController::actionPageDetail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 48
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 48
rs 9.424
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\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
80
        // Basic variables
81
        $variables['fullPageForm'] = false;
82
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
83
        $variables['pluginName'] = $pluginName;
84
        $variables['title'] = $templateTitle;
85
        $variables['crumbs'] = [
86
            [
87
                'label' => $pluginName,
88
                'url' => UrlHelper::cpUrl('webperf'),
89
            ],
90
            [
91
                'label' => $templateTitle,
92
                'url' => UrlHelper::cpUrl('webperf/dashboard'),
93
            ],
94
        ];
95
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
96
        $variables['selectedSubnavItem'] = 'dashboard';
97
        $variables['showWelcome'] = $showWelcome;
98
        $variables['settings'] = Webperf::$settings;
99
100
        // Render the template
101
        return $this->renderTemplate('webperf/dashboard/index', $variables);
102
    }
103
104
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
105
     * @param string|null $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
106
     *
107
     * @return Response
108
     * @throws NotFoundHttpException
109
     * @throws \yii\web\ForbiddenHttpException
110
     */
111
    public function actionPagesIndex(string $siteHandle = null): Response
112
    {
113
        $variables = [];
114
        PermissionHelper::controllerPermissionCheck('webperf:pages');
115
        // Trim the statistics
116
        Webperf::$plugin->dataSamples->trimOrphanedSamples(1024);
117
        Webperf::$plugin->dataSamples->trimDataSamples();
118
        // Get the site to edit
119
        $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle);
120
        $pluginName = Webperf::$settings->pluginName;
121
        $templateTitle = Craft::t('webperf', 'Pages');
122
        $view = Craft::$app->getView();
123
        // Asset bundle
124
        try {
125
            $view->registerAssetBundle(WebperfDashboardAsset::class);
126
        } catch (InvalidConfigException $e) {
127
            Craft::error($e->getMessage(), __METHOD__);
128
        }
129
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
130
            '@nystudio107/webperf/assetbundles/webperf/dist',
131
            true
132
        );
133
        // Enabled sites
134
        MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables);
135
        $variables['controllerHandle'] = 'pages-index';
136
137
        // Basic variables
138
        $variables['fullPageForm'] = false;
139
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
140
        $variables['pluginName'] = $pluginName;
141
        $variables['title'] = $templateTitle;
142
        $variables['crumbs'] = [
143
            [
144
                'label' => $pluginName,
145
                'url' => UrlHelper::cpUrl('webperf'),
146
            ],
147
            [
148
                'label' => $templateTitle,
149
                'url' => UrlHelper::cpUrl('webperf/pages'),
150
            ],
151
        ];
152
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
153
        $variables['selectedSubnavItem'] = 'pages';
154
        $variables['settings'] = Webperf::$settings;
155
156
        // Render the template
157
        return $this->renderTemplate('webperf/pages/index', $variables);
158
    }
159
160
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
161
     * @param string      $pageUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
162
     * @param string|null $siteHandle
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
163
     *
164
     * @return Response
165
     * @throws NotFoundHttpException
166
     * @throws \yii\web\ForbiddenHttpException
167
     */
168
    public function actionPageDetail(string $pageUrl, string $siteHandle = null): Response
169
    {
170
        $variables = [];
171
        PermissionHelper::controllerPermissionCheck('webperf:pages');
172
        // Trim the statistics
173
        Webperf::$plugin->dataSamples->trimOrphanedSamples(1024);
174
        Webperf::$plugin->dataSamples->trimDataSamples();
175
        // Get the site to edit
176
        $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle);
177
        $pluginName = Webperf::$settings->pluginName;
178
        $templateTitle = Craft::t('webperf', 'Pages');
179
        $view = Craft::$app->getView();
180
        // Asset bundle
181
        try {
182
            $view->registerAssetBundle(WebperfDashboardAsset::class);
183
        } catch (InvalidConfigException $e) {
184
            Craft::error($e->getMessage(), __METHOD__);
185
        }
186
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
187
            '@nystudio107/webperf/assetbundles/webperf/dist',
188
            true
189
        );
190
        // Enabled sites
191
        MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables);
192
        $variables['controllerHandle'] = 'pages-detail';
193
194
        // Basic variables
195
        $variables['fullPageForm'] = false;
196
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
197
        $variables['pluginName'] = $pluginName;
198
        $variables['title'] = $templateTitle;
199
        $variables['crumbs'] = [
200
            [
201
                'label' => $pluginName,
202
                'url' => UrlHelper::cpUrl('webperf'),
203
            ],
204
            [
205
                'label' => $templateTitle,
206
                'url' => UrlHelper::cpUrl('webperf/pages'),
207
            ],
208
        ];
209
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
210
        $variables['selectedSubnavItem'] = 'pages';
211
        $variables['pageUrl'] = $pageUrl;
212
        $variables['settings'] = Webperf::$settings;
213
214
        // Render the template
215
        return $this->renderTemplate('webperf/pages/page-detail', $variables);
216
    }
217
}
218