Passed
Push — develop ( 1b86c8...9e8a83 )
by Andrew
05:21
created

RichVariables::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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