Passed
Push — develop ( 792131...c9d188 )
by Andrew
03:44
created

RichVariables::installCpEventListeners()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
16
use Craft;
17
use craft\base\Plugin;
18
use craft\events\PluginEvent;
19
use craft\events\RegisterUrlRulesEvent;
20
use craft\helpers\UrlHelper;
21
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...
22
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...
23
use craft\services\Plugins;
24
use craft\web\UrlManager;
25
26
use yii\base\Event;
27
use yii\base\Exception;
28
use yii\base\InvalidConfigException;
29
30
use Composer\Semver\Comparator;
31
32
/**
33
 * Class RichVariables
34
 *
35
 * @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...
36
 * @package   RichVariables
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
37
 * @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...
38
 */
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...
39
class RichVariables extends Plugin
40
{
41
    // Static Properties
42
    // =========================================================================
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @var RichVariables
46
     */
47
    public static $plugin;
48
49
    // Public Methods
50
    // =========================================================================
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
53
     * @inheritdoc
54
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
55
    public function init()
56
    {
57
        parent::init();
58
        self::$plugin = $this;
59
60
        // Add in our event listeners that are needed for every request
61
        $this->installEventListeners();
62
        // We're loaded!
63
        Craft::info(
64
            Craft::t(
65
                'rich-variables',
66
                '{name} plugin loaded',
67
                ['name' => $this->name]
68
            ),
69
            __METHOD__
70
        );
71
    }
72
73
    // Protected Methods
74
    // =========================================================================
75
76
    /**
77
     * Install our event listeners
78
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
79
    protected function installEventListeners()
80
    {
81
        // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
82
        Event::on(
83
            Plugins::class,
84
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
85
            function (PluginEvent $event) {
86
                if ($event->plugin === $this) {
87
                    $request = Craft::$app->getRequest();
88
                    if ($request->isCpRequest) {
89
                        Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('rich-variables/welcome'))->send();
90
                    }
91
                }
92
            }
93
        );
94
        $request = Craft::$app->getRequest();
95
        // Install only for non-console site requests
96
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
97
            $this->installSiteEventListeners();
98
        }
99
        // Install only for non-console AdminCP requests
100
        if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
101
            $this->installCpEventListeners();
102
        }
103
    }
104
105
    /**
106
     * Install site event listeners for site requests only
107
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
108
    protected function installSiteEventListeners()
109
    {
110
        // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES
111
        Event::on(
112
            UrlManager::class,
113
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
114
            function (RegisterUrlRulesEvent $event) {
115
                Craft::debug(
116
                    'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
117
                    __METHOD__
118
                );
119
                // Register our AdminCP routes
120
                $event->rules = array_merge(
121
                    $event->rules,
122
                    $this->customFrontendRoutes()
123
                );
124
            }
125
        );
126
    }
127
128
    /**
129
     * Install site event listeners for AdminCP requests only
130
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
131
    protected function installCpEventListeners()
132
    {
133
        // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
134
        Event::on(
135
            Plugins::class,
136
            Plugins::EVENT_AFTER_LOAD_PLUGINS,
137
            function () {
138
                $this->installRedactorPlugin();
139
            }
140
        );
141
    }
142
143
    /**
144
     * Return the custom frontend routes
145
     *
146
     * @return array
147
     */
148
    protected function customFrontendRoutes(): array
149
    {
150
        return [
151
            // Make webpack async bundle loading work out of published AssetBundles
152
            '/cpresources/rich-variables/<resourceType:{handle}>/<fileName>' => 'rich-variables/cp-nav/resource',
153
        ];
154
    }
155
156
    /**
157
     * Install our Redactor plugin
158
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
159
    protected function installRedactorPlugin()
160
    {
161
        // Make sure the Redactor plugin is installed
162
        $redactor = Craft::$app->getPlugins()->getPlugin('redactor');
163
        if ($redactor) {
164
            // Event handler: RichText::EVENT_REGISTER_PLUGIN_PATHS
165
            Event::on(
166
                RichText::class,
167
                RichText::EVENT_REGISTER_PLUGIN_PATHS,
168
                function (RegisterPluginPathsEvent $event) {
169
                    /** @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...
170
                    $redactor = Craft::$app->getPlugins()->getPlugin('redactor');
171
                    $versionDir = 'v1/';
172
                    if (Comparator::greaterThanOrEqualTo($redactor->version, '2.0.0')) {
173
                        $versionDir = 'v2/';
174
                    }
175
                    // Add the path to our Redactor plugin
176
                    $src = Craft::getAlias('@nystudio107/richvariables/redactor/plugins/'.$versionDir);
177
                    $event->paths[] = $src;
178
                }
179
            );
180
            // Register our asset bundle
181
            try {
182
                Craft::$app->getView()->registerAssetBundle(RichVariablesAsset::class);
183
            } catch (InvalidConfigException $e) {
184
                Craft::error($e->getMessage(), __METHOD__);
185
            }
186
        }
187
    }
188
189
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
190
     * @inheritdoc
191
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
192
    protected function createSettingsModel()
193
    {
194
        return new Settings();
195
    }
196
197
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
198
     * @inheritdoc
199
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
200
    protected function settingsHtml(): string
201
    {
202
        // Get all of the globals sets
203
        $globalsHandles = [];
204
        $allGlobalsSets = Craft::$app->getGlobals()->getAllSets();
205
        foreach ($allGlobalsSets as $globalsSet) {
206
            $globalsHandles[$globalsSet->handle] = $globalsSet->name;
207
        }
208
209
        // Render our settings template
210
        try {
211
            return Craft::$app->view->renderTemplate(
212
                'rich-variables/settings',
213
                [
214
                    'settings' => $this->getSettings(),
215
                    'globalsSets' => $globalsHandles,
216
                ]
217
            );
218
        } catch (\Twig_Error_Loader $e) {
219
            Craft::error($e->getMessage(), __METHOD__);
220
        } catch (Exception $e) {
221
            Craft::error($e->getMessage(), __METHOD__);
222
        }
223
224
        return '';
225
    }
226
}
227