Passed
Push — develop ( 3338d0...daa41e )
by Benjamin
03:49
created

Plugin::getOauthProviderOptions()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 32
nop 1
dl 0
loc 36
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link      https://dukt.net/videos/
4
 * @copyright Copyright (c) 2019, 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
     * @var bool
43
     */
44
    public $hasCpSettings = true;
45
46
    /**
47
     * @var \dukt\videos\Plugin The plugin instance.
48
     */
49
    public static $plugin;
50
51
    // Public Methods
52
    // =========================================================================
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function init()
58
    {
59
        parent::init();
60
        self::$plugin = $this;
61
62
        $this->_setPluginComponents();
63
        $this->_registerCpRoutes();
64
        $this->_registerFieldTypes();
65
        $this->_registerCacheOptions();
66
        $this->_registerVariable();
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getSettingsResponse()
73
    {
74
        $url = UrlHelper::cpUrl('videos/settings');
75
76
        return Craft::$app->controller->redirect($url);
77
    }
78
79
    /**
80
     * Get OAuth provider options.
81
     *
82
     * @param string $gatewayHandle
83
     * @return null|array
84
     * @throws \yii\base\InvalidConfigException
85
     */
86
    public function getOauthProviderOptions(string $gatewayHandle)
87
    {
88
        $options = null;
89
90
        $configSettings = Craft::$app->config->getConfigFromFile($this->id);
91
92
        if (isset($configSettings['oauthProviderOptions'][$gatewayHandle])) {
93
            $options = $configSettings['oauthProviderOptions'][$gatewayHandle];
94
        }
95
96
        $storedSettings = Craft::$app->plugins->getStoredPluginInfo($this->id)['settings'];
97
98
        if ($options === null && isset($storedSettings['oauthProviderOptions'][$gatewayHandle])) {
99
            $options = $storedSettings['oauthProviderOptions'][$gatewayHandle];
100
        }
101
102
        if (!isset($options['redirectUri'])) {
103
            $gateway = $this->getGateways()->getGateway($gatewayHandle, false);
104
            $options['redirectUri'] = $gateway->getRedirectUri();
105
        }
106
107
        // check if there is an env variable defined for each option
108
        foreach ($options as $key => $option) {
109
            // check if the option potentially contains an env variable
110
            if (substr($option, 0, 1) === '$') {
111
                $envName = substr($option, 1);
112
                $envVariable = getenv($envName);
113
114
                if ($envVariable !== false) {
115
                    // replace option value with env variable
116
                    $options[$key] = $envVariable;
117
                }
118
            }
119
        }
120
121
        return $options;
122
    }
123
124
    // Protected Methods
125
    // =========================================================================
126
127
    /**
128
     * @inheritdoc
129
     */
130
    protected function createSettingsModel()
131
    {
132
        return new Settings();
133
    }
134
135
    // Protected Methods
136
    // =========================================================================
137
138
    /**
139
     * Set plugin components.
140
     */
141
    private function _setPluginComponents()
142
    {
143
        $this->setComponents([
144
            'videos' => \dukt\videos\services\Videos::class,
145
            'cache' => \dukt\videos\services\Cache::class,
146
            'gateways' => \dukt\videos\services\Gateways::class,
147
            'oauth' => \dukt\videos\services\Oauth::class,
148
            'tokens' => \dukt\videos\services\Tokens::class,
149
        ]);
150
    }
151
152
    /**
153
     * Register CP routes.
154
     */
155
    private function _registerCpRoutes()
156
    {
157
        Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
158
            $rules = [
159
                'videos/settings' => 'videos/settings/index',
160
                'videos/settings/<gatewayHandle:{handle}>' => 'videos/settings/gateway',
161
                'videos/settings/<gatewayHandle:{handle}>/oauth' => 'videos/settings/gateway-oauth',
162
            ];
163
164
            $event->rules = array_merge($event->rules, $rules);
165
        });
166
    }
167
168
    /**
169
     * Register field types.
170
     */
171
    private function _registerFieldTypes()
172
    {
173
        Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
174
            $event->types[] = VideoField::class;
175
        });
176
    }
177
178
    /**
179
     * Register cache options.
180
     */
181
    private function _registerCacheOptions()
182
    {
183
        Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, function(RegisterCacheOptionsEvent $event) {
184
            $event->options[] = [
185
                'key' => 'videos-caches',
186
                'label' => Craft::t('videos', 'Videos caches'),
187
                'action' => Craft::$app->path->getRuntimePath().'/videos'
188
            ];
189
        });
190
    }
191
192
    /**
193
     * Register template variable.
194
     */
195
    private function _registerVariable()
196
    {
197
        Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) {
198
            /** @var CraftVariable $variable */
199
            $variable = $event->sender;
200
            $variable->set('videos', VideosVariable::class);
201
        });
202
    }
203
204
}
205