RichVariables   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 21
eloc 93
c 8
b 0
f 0
dl 0
loc 237
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
B installEventListeners() 0 35 7
A createSettingsModel() 0 3 1
A customFrontendRoutes() 0 3 1
A settingsHtml() 0 25 4
A installCpEventListeners() 0 8 1
A installRedactorPlugin() 0 26 4
A init() 0 15 1
A __construct() 0 17 1
A installSiteEventListeners() 0 15 1
1
<?php
2
/**
3
 * Rich Variables plugin for Craft CMS 3.x
4
 *
5
 * Allows you to easily use Craft Globals as variables in Rich Text fields
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) 2017 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\richvariables;
12
13
use Composer\Semver\Comparator;
14
use Craft;
15
use craft\base\Plugin;
16
use craft\events\PluginEvent;
17
use craft\events\RegisterUrlRulesEvent;
18
use craft\helpers\UrlHelper;
19
use craft\redactor\events\RegisterPluginPathsEvent;
0 ignored issues
show
Bug introduced by
The type craft\redactor\events\RegisterPluginPathsEvent 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\redactor\Field as RichText;
0 ignored issues
show
Bug introduced by
The type craft\redactor\Field 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 craft\services\Plugins;
22
use craft\web\twig\variables\CraftVariable;
23
use craft\web\UrlManager;
24
use Exception;
25
use nystudio107\pluginvite\services\VitePluginService;
26
use nystudio107\richvariables\assetbundles\richvariables\RichVariablesAsset;
27
use nystudio107\richvariables\models\Settings;
28
use nystudio107\richvariables\variables\RichVariablesVariable;
29
use Twig\Error\LoaderError;
30
use yii\base\Event;
31
use yii\base\InvalidConfigException;
32
33
/**
34
 * Class RichVariables
35
 *
36
 * @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 for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
37
 * @package   RichVariables
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
38
 * @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 for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
39
 *
40
 * @property VitePluginService $vite
41
 */
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...
42
class RichVariables extends Plugin
43
{
44
    // Static Properties
45
    // =========================================================================
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @var RichVariables
49
     */
50
    public static $plugin;
51
52
    // Static Methods
53
    // =========================================================================
54
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
55
     * @var string
56
     */
57
    public $schemaVersion = '1.0.0';
58
59
    // Public Properties
60
    // =========================================================================
61
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
62
     * @var bool
63
     */
64
    public $hasCpSection = false;
65
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
66
     * @var bool
67
     */
68
    public $hasCpSettings = true;
69
70
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $id should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
71
     * @inheritdoc
72
     */
73
    public function __construct($id, $parent = null, array $config = [])
74
    {
75
        $config['components'] = [
76
            // Register the vite service
77
            'vite' => [
78
                'class' => VitePluginService::class,
79
                'assetClass' => RichVariablesAsset::class,
80
                'useDevServer' => true,
81
                'devServerPublic' => 'http://localhost:3001',
82
                'serverPublic' => 'http://localhost:8000',
83
                'errorEntry' => 'src/js/app.ts',
84
                'devServerInternal' => 'http://craft-richvariables-buildchain:3001',
85
                'checkDevServer' => true,
86
            ],
87
        ];
88
89
        parent::__construct($id, $parent, $config);
90
    }
91
92
93
    // Public Methods
94
    // =========================================================================
95
96
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
97
     * @inheritdoc
98
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
99
    public function init()
100
    {
101
        parent::init();
102
        self::$plugin = $this;
103
104
        // Add in our event listeners that are needed for every request
105
        $this->installEventListeners();
106
        // We're loaded!
107
        Craft::info(
108
            Craft::t(
109
                'rich-variables',
110
                '{name} plugin loaded',
111
                ['name' => $this->name]
112
            ),
113
            __METHOD__
114
        );
115
    }
116
117
    // Protected Methods
118
    // =========================================================================
119
120
    /**
121
     * Install our event listeners
122
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
123
    protected function installEventListeners()
124
    {
125
        Event::on(
126
            CraftVariable::class,
127
            CraftVariable::EVENT_INIT,
128
            function (Event $event) {
129
                /** @var CraftVariable $variable */
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...
130
                $variable = $event->sender;
131
                $variable->set('richVariables', [
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...
132
                    'class' => RichVariablesVariable::class,
133
                    'viteService' => $this->vite,
134
                ]);
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...
135
            }
136
        );
137
        // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
138
        Event::on(
139
            Plugins::class,
140
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
141
            function (PluginEvent $event) {
142
                if ($event->plugin === $this) {
143
                    $request = Craft::$app->getRequest();
144
                    if ($request->isCpRequest) {
145
                        Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('rich-variables/welcome'))->send();
146
                    }
147
                }
148
            }
149
        );
150
        $request = Craft::$app->getRequest();
151
        // Install only for non-console site requests
152
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
153
            $this->installSiteEventListeners();
154
        }
155
        // Install only for non-console Control Panel requests
156
        if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
157
            $this->installCpEventListeners();
158
        }
159
    }
160
161
    /**
162
     * Install site event listeners for site requests only
163
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
164
    protected function installSiteEventListeners()
165
    {
166
        // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES
167
        Event::on(
168
            UrlManager::class,
169
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
170
            function (RegisterUrlRulesEvent $event) {
171
                Craft::debug(
172
                    'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
173
                    __METHOD__
174
                );
175
                // Register our Control Panel routes
176
                $event->rules = array_merge(
177
                    $event->rules,
178
                    $this->customFrontendRoutes()
179
                );
180
            }
181
        );
182
    }
183
184
    /**
185
     * Return the custom frontend routes
186
     *
187
     * @return array
188
     */
189
    protected function customFrontendRoutes(): array
190
    {
191
        return [
192
        ];
193
    }
194
195
    /**
196
     * Install site event listeners for Control Panel requests only
197
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
198
    protected function installCpEventListeners()
199
    {
200
        // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
201
        Event::on(
202
            Plugins::class,
203
            Plugins::EVENT_AFTER_LOAD_PLUGINS,
204
            function () {
205
                $this->installRedactorPlugin();
206
            }
207
        );
208
    }
209
210
    /**
211
     * Install our Redactor plugin
212
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
213
    protected function installRedactorPlugin()
214
    {
215
        // Make sure the Redactor plugin is installed
216
        $redactor = Craft::$app->getPlugins()->getPlugin('redactor');
217
        if ($redactor) {
218
            // Event handler: RichText::EVENT_REGISTER_PLUGIN_PATHS
219
            Event::on(
220
                RichText::class,
221
                RichText::EVENT_REGISTER_PLUGIN_PATHS,
222
                function (RegisterPluginPathsEvent $event) {
223
                    /** @var Plugin $redactor */
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...
224
                    $redactor = Craft::$app->getPlugins()->getPlugin('redactor');
225
                    $versionDir = 'v1/';
226
                    if (Comparator::greaterThanOrEqualTo($redactor->version, '2.0.0')) {
227
                        $versionDir = 'v2/';
228
                    }
229
                    // Add the path to our Redactor plugin
230
                    $src = Craft::getAlias('@nystudio107/richvariables/redactor/plugins/' . $versionDir);
231
                    $event->paths[] = $src;
232
                }
233
            );
234
            // Register our asset bundle
235
            try {
236
                Craft::$app->getView()->registerAssetBundle(RichVariablesAsset::class);
237
            } catch (InvalidConfigException $e) {
238
                Craft::error($e->getMessage(), __METHOD__);
239
            }
240
        }
241
    }
242
243
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
244
     * @inheritdoc
245
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
246
    protected function createSettingsModel()
247
    {
248
        return new Settings();
249
    }
250
251
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
252
     * @inheritdoc
253
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
254
    protected function settingsHtml(): string
255
    {
256
        // Get all of the globals sets
257
        $globalsHandles = [];
258
        $allGlobalsSets = Craft::$app->getGlobals()->getAllSets();
259
        foreach ($allGlobalsSets as $globalsSet) {
260
            $globalsHandles[$globalsSet->handle] = $globalsSet->name;
261
        }
262
263
        // Render our settings template
264
        try {
265
            return Craft::$app->view->renderTemplate(
266
                'rich-variables/settings',
267
                [
268
                    'settings' => $this->getSettings(),
269
                    'globalsSets' => $globalsHandles,
270
                ]
271
            );
272
        } catch (LoaderError $e) {
273
            Craft::error($e->getMessage(), __METHOD__);
274
        } catch (Exception $e) {
275
            Craft::error($e->getMessage(), __METHOD__);
276
        }
277
278
        return '';
279
    }
280
}
281