Issues (1783)

src/controllers/SettingsController.php (28 issues)

1
<?php
2
/**
3
 * Retour plugin for Craft CMS
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
7
 *
8
 * @link      https://nystudio107.com/
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\retour\controllers;
13
14
use Craft;
15
use craft\errors\MissingComponentException;
16
use craft\helpers\UrlHelper;
17
use craft\web\Controller;
18
use craft\web\UrlManager;
19
use nystudio107\retour\assetbundles\retour\RetourAsset;
20
use nystudio107\retour\helpers\Permission as PermissionHelper;
21
use nystudio107\retour\models\Settings;
22
use nystudio107\retour\Retour;
23
use yii\base\InvalidConfigException;
24
use yii\web\BadRequestHttpException;
25
use yii\web\ForbiddenHttpException;
26
use yii\web\NotFoundHttpException;
27
use yii\web\Response;
28
29
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
30
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
31
 * @package   Retour
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
32
 * @since     3.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
33
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
34
class SettingsController extends Controller
35
{
36
    // Constants
37
    // =========================================================================
38
39
    protected const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-retour/';
40
41
    // Protected Properties
42
    // =========================================================================
43
44
    protected array|bool|int $allowAnonymous = [];
45
46
    // Public Methods
47
    // =========================================================================
48
49
    /**
50
     * Plugin settings
51
     *
52
     * @param Settings|bool|null $settings
0 ignored issues
show
Missing parameter comment
Loading history...
53
     *
54
     * @return Response The rendered result
55
     * @throws ForbiddenHttpException
56
     */
57
    public function actionPluginSettings(Settings|bool|null $settings = null): Response
58
    {
59
        $variables = [];
60
        PermissionHelper::controllerPermissionCheck('retour:settings');
61
        if ($settings === null) {
62
            $settings = Retour::$settings;
63
        }
64
        /** @var Settings $settings */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
65
        $pluginName = $settings->pluginName;
66
        $templateTitle = Craft::t('retour', 'Settings');
67
        $view = Craft::$app->getView();
68
        // Asset bundle
69
        try {
70
            $view->registerAssetBundle(RetourAsset::class);
71
        } catch (InvalidConfigException $e) {
72
            Craft::error($e->getMessage(), __METHOD__);
73
        }
74
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
75
            '@nystudio107/retour/web/assets/dist',
76
            true
0 ignored issues
show
The call to yii\web\AssetManager::getPublishedUrl() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        /** @scrutinizer ignore-call */ 
77
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
77
        );
78
        // Basic variables
79
        $variables['fullPageForm'] = true;
80
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
81
        $variables['pluginName'] = $pluginName;
82
        $variables['title'] = $templateTitle;
83
        $variables['crumbs'] = [
84
            [
85
                'label' => $pluginName,
86
                'url' => UrlHelper::cpUrl('retour'),
87
            ],
88
            [
89
                'label' => $templateTitle,
90
                'url' => UrlHelper::cpUrl('retour/settings'),
91
            ],
92
        ];
93
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
94
        $variables['selectedSubnavItem'] = 'settings';
95
        $variables['settings'] = $settings;
96
97
        // Render the template
98
        return $this->renderTemplate('retour/settings', $variables);
99
    }
100
101
    /**
102
     * Saves a plugin’s settings.
103
     *
104
     * @return Response|null
105
     * @throws NotFoundHttpException if the requested plugin cannot be found
106
     * @throws BadRequestHttpException
107
     * @throws MissingComponentException
108
     * @throws ForbiddenHttpException
109
     */
110
    public function actionSavePluginSettings(): ?Response
111
    {
112
        PermissionHelper::controllerPermissionCheck('retour:settings');
113
        $this->requirePostRequest();
114
        $pluginHandle = Craft::$app->getRequest()->getRequiredBodyParam('pluginHandle');
115
        $settings = Craft::$app->getRequest()->getBodyParam('settings', []);
116
        $plugin = Craft::$app->getPlugins()->getPlugin($pluginHandle);
117
118
        if ($plugin === null) {
119
            throw new NotFoundHttpException('Plugin not found');
120
        }
121
122
        if (!is_array($settings['additionalHeaders'])) {
123
            $settings['additionalHeaders'] = [];
124
        }
125
        if (!is_array($settings['excludePatterns'])) {
126
            $settings['excludePatterns'] = [];
127
        }
128
        if (!Craft::$app->getPlugins()->savePluginSettings($plugin, $settings)) {
129
            Craft::$app->getSession()->setError(Craft::t('app', "Couldn't save plugin settings."));
130
131
            // Send the plugin back to the template
132
            /** @var UrlManager $urlManager */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
133
            $urlManager = Craft::$app->getUrlManager();
134
            $urlManager->setRouteParams([
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
135
                'plugin' => $plugin,
136
            ]);
0 ignored issues
show
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...
137
138
            return null;
139
        }
140
141
        Retour::$plugin->clearAllCaches();
0 ignored issues
show
The method clearAllCaches() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        Retour::$plugin->/** @scrutinizer ignore-call */ 
142
                         clearAllCaches();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
        Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.'));
143
144
        return $this->redirectToPostedUrl();
145
    }
146
}
147