Passed
Push — master ( 1bbfaf...74d8e9 )
by Benjamin
11:44 queued 05:15
created

Plugin::_setPluginComponents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link      https://dukt.net/videos/
4
 * @copyright Copyright (c) 2018, 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
    public function getOauthProviderOptions($gatewayHandle)
80
    {
81
        $options = null;
82
83
        $configSettings = Craft::$app->config->getConfigFromFile($this->id);
84
85
        if (isset($configSettings['oauthProviderOptions'][$gatewayHandle])) {
86
            $options = $configSettings['oauthProviderOptions'][$gatewayHandle];
87
        }
88
89
        $storedSettings = Craft::$app->plugins->getStoredPluginInfo($this->id)['settings'];
90
91
        if ($options === null && isset($storedSettings['oauthProviderOptions'][$gatewayHandle])) {
92
            $options = $storedSettings['oauthProviderOptions'][$gatewayHandle];
93
        }
94
95
        if (!isset($options['redirectUri'])) {
96
            $gateway = $this->getGateways()->getGateway($gatewayHandle, false);
97
            $options['redirectUri'] = $gateway->getRedirectUri();
98
        }
99
100
        return $options;
101
    }
102
103
    // Protected Methods
104
    // =========================================================================
105
106
    /**
107
     * @inheritdoc
108
     */
109
    protected function createSettingsModel()
110
    {
111
        return new Settings();
112
    }
113
114
    // Protected Methods
115
    // =========================================================================
116
117
    /**
118
     * Set plugin components.
119
     */
120
    private function _setPluginComponents()
121
    {
122
        $this->setComponents([
123
            'videos' => \dukt\videos\services\Videos::class,
124
            'cache' => \dukt\videos\services\Cache::class,
125
            'gateways' => \dukt\videos\services\Gateways::class,
126
            'oauth' => \dukt\videos\services\Oauth::class,
127
        ]);
128
    }
129
130
    /**
131
     * Register CP routes.
132
     */
133
    private function _registerCpRoutes()
134
    {
135
        Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
136
            $rules = [
137
                'videos/settings' => 'videos/settings/index',
138
                'videos/settings/<gatewayHandle:{handle}>' => 'videos/settings/gateway',
139
                'videos/settings/<gatewayHandle:{handle}>/oauth' => 'videos/settings/gateway-oauth',
140
            ];
141
142
            $event->rules = array_merge($event->rules, $rules);
143
        });
144
    }
145
146
    /**
147
     * Register field types.
148
     */
149
    private function _registerFieldTypes()
150
    {
151
        Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
152
            $event->types[] = VideoField::class;
153
        });
154
    }
155
156
    /**
157
     * Register cache options.
158
     */
159
    private function _registerCacheOptions()
160
    {
161
        Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, function(RegisterCacheOptionsEvent $event) {
162
            $event->options[] = [
163
                'key' => 'videos-caches',
164
                'label' => Craft::t('videos', 'Videos caches'),
165
                'action' => Craft::$app->path->getRuntimePath().'/videos'
166
            ];
167
        });
168
    }
169
170
    /**
171
     * Register template variable.
172
     */
173
    private function _registerVariable()
174
    {
175
        Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) {
176
            /** @var CraftVariable $variable */
177
            $variable = $event->sender;
178
            $variable->set('videos', VideosVariable::class);
179
        });
180
    }
181
182
}
183