Gateways::getGateways()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\services;
9
10
use Craft;
11
use dukt\videos\base\Gateway;
12
use dukt\videos\events\RegisterGatewayTypesEvent;
13
use dukt\videos\Plugin;
14
use yii\base\Component;
15
use dukt\videos\gateways\Vimeo;
16
use dukt\videos\gateways\YouTube;
17
18
/**
19
 * Class Gateways service.
20
 *
21
 * An instance of the Gateways service is globally accessible via [[Plugin::gateways `Videos::$plugin->getGateways()`]].
22
 *
23
 * @author Dukt <[email protected]>
24
 * @since  2.0
25
 */
26
class Gateways extends Component
27
{
28
    // Constants
29
    // =========================================================================
30
    /**
31
     * @event RegisterLoginProviderTypesEvent The event that is triggered when registering login providers.
32
     * @var string
33
     */
34
    public const EVENT_REGISTER_GATEWAY_TYPES = 'registerGatewayTypes';
35
36
    // Properties
37
    // =========================================================================
38
39
    /**
40
     * @var array
41
     */
42
    private $_gateways = [];
43
44
    /**
45
     * @var array
46
     */
47
    private $_allGateways = [];
48
49
    /**
50
     * @var bool
51
     */
52
    private $_gatewaysLoaded = false;
53
54
    // Public Methods
55
    // =========================================================================
56
    /**
57
     * Get gateway by handle.
58
     *
59
     * @param      $gatewayHandle
60
     *
61
     * @return Gateway|null
62
     */
63
    public function getGateway($gatewayHandle, bool $enabledOnly = true)
64
    {
65
        $this->loadGateways();
66
67
        $gateways = $enabledOnly ? $this->_gateways : $this->_allGateways;
68
69
        foreach ($gateways as $g) {
70
            if ($g->getHandle() === $gatewayHandle) {
71
                return $g;
72
            }
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * Get gateways.
80
     *
81
     *
82
     * @return Gateway[]
83
     */
84
    public function getGateways(bool $enabledOnly = true): array
85
    {
86
        $this->loadGateways();
87
88
        if ($enabledOnly) {
89
            return $this->_gateways;
90
        }
91
92
        return $this->_allGateways;
93
    }
94
95
    // Private Methods
96
    // =========================================================================
97
98
    /**
99
     * Load gateways.
100
     *
101
     * @return null
102
     * @throws \yii\base\InvalidConfigException
103
     */
104
    private function loadGateways()
105
    {
106
        if ($this->_gatewaysLoaded) {
107
            return null;
108
        }
109
110
        foreach ($this->_getGateways() as $gateway) {
111
            if ($gateway->enableOauthFlow()) {
112
                $gatewayHandle = $gateway->getHandle();
113
114
                $token = Plugin::getInstance()->getTokens()->getToken($gatewayHandle);
115
116
                if ($token !== null) {
117
                    $this->_gateways[] = $gateway;
118
                }
119
            } else {
120
                $this->_gateways[] = $gateway;
121
            }
122
123
            $this->_allGateways[] = $gateway;
124
        }
125
126
        $this->_gatewaysLoaded = true;
127
128
        return null;
129
    }
130
131
    /**
132
     * Returns all gateway instances.
133
     *
134
     * @return array
135
     */
136
    private function _getGateways(): array
137
    {
138
        $gatewayTypes = $this->_getGatewayTypes();
139
140
        $gateways = [];
141
142
        foreach ($gatewayTypes as $gatewayType) {
143
            $gateways[$gatewayType] = $this->_createGateway($gatewayType);
144
        }
145
146
        ksort($gateways);
147
148
        return $gateways;
149
    }
150
151
    /**
152
     * Returns gateway types.
153
     *
154
     * @return array
155
     */
156
    private function _getGatewayTypes(): array
157
    {
158
        $gatewayTypes = [
159
            Vimeo::class,
160
            YouTube::class,
161
        ];
162
163
        $eventName = self::EVENT_REGISTER_GATEWAY_TYPES;
164
165
        $event = new RegisterGatewayTypesEvent([
166
            'gatewayTypes' => $gatewayTypes
167
        ]);
168
169
        $this->trigger($eventName, $event);
170
171
        return $event->gatewayTypes;
172
    }
173
174
    /**
175
     * Instantiates a gateway.
176
     *
177
     * @param $gatewayType
178
     *
179
     * @return mixed
180
     */
181
    private function _createGateway($gatewayType): object
182
    {
183
        return new $gatewayType;
184
    }
185
}
186