Issues (47)

src/Plugin.php (3 issues)

Labels
Severity
1
<?php
2
/**
3
 * @link      https://dukt.net/videos/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://github.com/dukt/videos/blob/v2/LICENSE.md
6
 */
7
8
namespace dukt\videos;
9
10
use Craft;
11
use craft\events\RegisterCacheOptionsEvent;
12
use craft\events\RegisterComponentTypesEvent;
13
use craft\events\RegisterUrlRulesEvent;
14
use craft\helpers\UrlHelper;
15
use craft\services\Fields;
16
use craft\utilities\ClearCaches;
17
use craft\web\twig\variables\CraftVariable;
18
use craft\web\UrlManager;
19
use dukt\videos\base\PluginTrait;
20
use dukt\videos\fields\Video as VideoField;
21
use dukt\videos\models\Settings;
22
use dukt\videos\web\twig\variables\VideosVariable;
23
use yii\base\Event;
24
25
/**
26
 * Videos plugin class.
27
 *
28
 * @author  Dukt <[email protected]>
29
 * @since   1.0
30
 */
31
class Plugin extends \craft\base\Plugin
32
{
33
    // Traits
34
    // =========================================================================
35
36
    use PluginTrait;
37
38
    // Properties
39
    // =========================================================================
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public $schemaVersion = '1.0.3';
45
46
    /**
47
     * @var bool
48
     */
49
    public $hasCpSettings = true;
50
51
    /**
52
     * @var \dukt\videos\Plugin The plugin instance.
53
     */
54
    public static $plugin;
55
56
    // Public Methods
57
    // =========================================================================
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function init(): void
63
    {
64
        parent::init();
65
        self::$plugin = $this;
66
67
        $this->_setPluginComponents();
68
        $this->_registerCpRoutes();
69
        $this->_registerFieldTypes();
70
        $this->_registerCacheOptions();
71
        $this->_registerVariable();
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function getSettingsResponse()
78
    {
79
        $url = UrlHelper::cpUrl('videos/settings');
80
81
        return Craft::$app->controller->redirect($url);
82
    }
83
84
    /**
85
     * Get OAuth provider options.
86
     *
87
     * @param string $gatewayHandle
88
     * @param bool $parse
89
     * @return null|array
90
     * @throws \yii\base\InvalidConfigException
91
     */
92
    public function getOauthProviderOptions(string $gatewayHandle, bool $parse = true)
93
    {
94
        $options = null;
95
96
        $configSettings = Craft::$app->config->getConfigFromFile($this->id);
0 ignored issues
show
The method getConfigFromFile() 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

96
        /** @scrutinizer ignore-call */ 
97
        $configSettings = Craft::$app->config->getConfigFromFile($this->id);

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...
97
98
        if (isset($configSettings['oauthProviderOptions'][$gatewayHandle])) {
99
            $options = $configSettings['oauthProviderOptions'][$gatewayHandle];
100
        }
101
102
        $storedSettings = Craft::$app->plugins->getStoredPluginInfo($this->id)['settings'];
0 ignored issues
show
The method getStoredPluginInfo() 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

102
        $storedSettings = Craft::$app->plugins->/** @scrutinizer ignore-call */ getStoredPluginInfo($this->id)['settings'];

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...
103
104
        if ($options === null && isset($storedSettings['oauthProviderOptions'][$gatewayHandle])) {
105
            $options = $storedSettings['oauthProviderOptions'][$gatewayHandle];
106
        }
107
108
        if (!isset($options['redirectUri'])) {
109
            $gateway = $this->getGateways()->getGateway($gatewayHandle, false);
110
            $options['redirectUri'] = $gateway->getRedirectUri();
111
        }
112
113
        return $parse ? array_map('Craft::parseEnv', $options) : $options;
114
    }
115
116
    // Protected Methods
117
    // =========================================================================
118
119
    /**
120
     * @inheritdoc
121
     */
122
    protected function createSettingsModel(): \dukt\videos\models\Settings
123
    {
124
        return new Settings();
125
    }
126
127
    // Protected Methods
128
    // =========================================================================
129
130
    /**
131
     * Set plugin components.
132
     */
133
    private function _setPluginComponents(): void
134
    {
135
        $this->setComponents([
136
            'videos' => \dukt\videos\services\Videos::class,
137
            'cache' => \dukt\videos\services\Cache::class,
138
            'gateways' => \dukt\videos\services\Gateways::class,
139
            'oauth' => \dukt\videos\services\Oauth::class,
140
            'tokens' => \dukt\videos\services\Tokens::class,
141
        ]);
142
    }
143
144
    /**
145
     * Register CP routes.
146
     */
147
    private function _registerCpRoutes(): void
148
    {
149
        Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event): void {
150
            $rules = [
151
                'videos/settings' => 'videos/settings/index',
152
                'videos/settings/<gatewayHandle:{handle}>' => 'videos/settings/gateway',
153
                'videos/settings/<gatewayHandle:{handle}>/oauth' => 'videos/settings/gateway-oauth',
154
            ];
155
156
            $event->rules = array_merge($event->rules, $rules);
157
        });
158
    }
159
160
    /**
161
     * Register field types.
162
     */
163
    private function _registerFieldTypes(): void
164
    {
165
        Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event): void {
166
            $event->types[] = VideoField::class;
167
        });
168
    }
169
170
    /**
171
     * Register cache options.
172
     */
173
    private function _registerCacheOptions(): void
174
    {
175
        Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, function(RegisterCacheOptionsEvent $event): void {
176
            $event->options[] = [
177
                'key' => 'videos-caches',
178
                'label' => Craft::t('videos', 'Videos caches'),
179
                'action' => Craft::$app->path->getRuntimePath() . '/videos'
0 ignored issues
show
The method getRuntimePath() 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

179
                'action' => Craft::$app->path->/** @scrutinizer ignore-call */ getRuntimePath() . '/videos'

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...
180
            ];
181
        });
182
    }
183
184
    /**
185
     * Register template variable.
186
     */
187
    private function _registerVariable(): void
188
    {
189
        Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event): void {
190
            /** @var CraftVariable $variable */
191
            $variable = $event->sender;
192
            $variable->set('videos', VideosVariable::class);
193
        });
194
    }
195
196
}
197