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

SettingsController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 45
dl 0
loc 103
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A actionPluginSettings() 0 42 3
A actionSavePluginSettings() 0 27 3
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\WebperfAsset;
15
use nystudio107\webperf\helpers\Permission as PermissionHelper;
16
use nystudio107\webperf\models\Settings;
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 SettingsController 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
    /**
47
     * Plugin settings
48
     *
49
     * @param null|bool|Settings $settings
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     *
51
     * @return Response The rendered result
52
     * @throws \yii\web\ForbiddenHttpException
53
     */
54
    public function actionPluginSettings($settings = null): Response
55
    {
56
        $variables = [];
57
        PermissionHelper::controllerPermissionCheck('webperf:settings');
58
        if ($settings === null) {
59
            $settings = Webperf::$settings;
60
        }
61
        /** @var Settings $settings */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
62
        $pluginName = $settings->pluginName;
63
        $templateTitle = Craft::t('webperf', 'Settings');
64
        $view = Craft::$app->getView();
65
        // Asset bundle
66
        try {
67
            $view->registerAssetBundle(WebperfAsset::class);
68
        } catch (InvalidConfigException $e) {
69
            Craft::error($e->getMessage(), __METHOD__);
70
        }
71
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
72
            '@nystudio107/webperf/assetbundles/webperf/dist',
73
            true
74
        );
75
        // Basic variables
76
        $variables['fullPageForm'] = true;
77
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
78
        $variables['pluginName'] = $pluginName;
79
        $variables['title'] = $templateTitle;
80
        $variables['crumbs'] = [
81
            [
82
                'label' => $pluginName,
83
                'url' => UrlHelper::cpUrl('webperf'),
84
            ],
85
            [
86
                'label' => $templateTitle,
87
                'url' => UrlHelper::cpUrl('webperf/settings'),
88
            ],
89
        ];
90
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
91
        $variables['selectedSubnavItem'] = 'settings';
92
        $variables['settings'] = $settings;
93
94
        // Render the template
95
        return $this->renderTemplate('webperf/settings', $variables);
96
    }
97
98
    /**
99
     * Saves a plugin’s settings.
100
     *
101
     * @return Response|null
102
     * @throws NotFoundHttpException if the requested plugin cannot be found
103
     * @throws \yii\web\BadRequestHttpException
104
     * @throws \craft\errors\MissingComponentException
105
     * @throws \yii\web\ForbiddenHttpException
106
     */
107
    public function actionSavePluginSettings()
108
    {
109
        PermissionHelper::controllerPermissionCheck('webperf:settings');
110
        $this->requirePostRequest();
111
        $pluginHandle = Craft::$app->getRequest()->getRequiredBodyParam('pluginHandle');
112
        $settings = Craft::$app->getRequest()->getBodyParam('settings', []);
113
        $plugin = Craft::$app->getPlugins()->getPlugin($pluginHandle);
114
115
        if ($plugin === null) {
116
            throw new NotFoundHttpException('Plugin not found');
117
        }
118
119
        if (!Craft::$app->getPlugins()->savePluginSettings($plugin, $settings)) {
120
            Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save plugin settings."));
121
122
            // Send the plugin back to the template
123
            Craft::$app->getUrlManager()->setRouteParams([
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...
124
                'plugin' => $plugin,
125
            ]);
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...
126
127
            return null;
128
        }
129
130
        Webperf::$plugin->clearAllCaches();
131
        Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.'));
132
133
        return $this->redirectToPostedUrl();
134
    }
135
}
136