Completed
Push — master ( 4e6e8c...3a71d4 )
by Xu
06:30
created

Payment::setGateways()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\payment;
9
10
use Closure;
11
use Yii;
12
use yii\base\Component;
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * Class Payment
17
 *
18
 * @author Tongle Xu <[email protected]>
19
 * @since 3.0
20
 */
21
class Payment extends Component
22
{
23
    /**
24
     * @var array gateway parameters (name => value).
25
     */
26
    public $params = [];
27
28
    /**
29
     * @var array shared gateway instances indexed by their IDs
30
     */
31
    private $_gateways = [];
32
33
    /**
34
     * @var array gateway definitions indexed by their IDs
35
     */
36
    private $_definitions = [];
37
38
    /**
39
     * Getter magic method.
40
     * This method is overridden to support accessing filesystems like reading properties.
41
     * @param string $name filesystem or property name
42
     * @return mixed the named property value
43
     * @throws InvalidConfigException
44
     * @throws \yii\base\UnknownPropertyException
45
     */
46
    public function __get($name)
47
    {
48
        if ($this->has($name)) {
49
            return $this->get($name);
50
        }
51
52
        return parent::__get($name);
53
    }
54
55
    /**
56
     * Checks if a property value is null.
57
     * This method overrides the parent implementation by checking if the named gateway is loaded.
58
     * @param string $name the property name or the event name
59
     * @return bool whether the property value is null
60
     */
61
    public function __isset($name)
62
    {
63
        if ($this->has($name)) {
64
            return true;
65
        }
66
67
        return parent::__isset($name);
68
    }
69
70
    /**
71
     * Returns a value indicating whether the locator has the specified gateway definition or has instantiated the gateway.
72
     * This method may return different results depending on the value of `$checkInstance`.
73
     *
74
     * - If `$checkInstance` is false (default), the method will return a value indicating whether the locator has the specified
75
     *   gateway definition.
76
     * - If `$checkInstance` is true, the method will return a value indicating whether the locator has
77
     *   instantiated the specified gateway.
78
     *
79
     * @param string $id gateway ID (e.g. `local`).
80
     * @param bool $checkInstance whether the method should check if the gateway is shared and instantiated.
81
     * @return bool whether the locator has the specified gateway definition or has instantiated the gateway.
82
     * @see set()
83
     */
84
    public function has($id, $checkInstance = false)
85
    {
86
        return $checkInstance ? isset($this->_gateways[$id]) : isset($this->_definitions[$id]);
87
    }
88
89
    /**
90
     * Returns the filesystem instance with the specified ID.
91
     *
92
     * @param string $id filesystem ID (e.g. `db`).
93
     * @param bool $throwException whether to throw an exception if `$id` is not registered with the locator before.
94
     * @return \League\Flysystem\Filesystem|object|null the filesystem of the specified ID. If `$throwException` is false and `$id`
95
     * is not registered before, null will be returned.
96
     * @throws InvalidConfigException if `$id` refers to a nonexistent filesystem ID
97
     * @see has()
98
     * @see set()
99
     */
100
    public function get($id, $throwException = true)
101
    {
102
        if (isset($this->_gateways[$id])) {
103
            return $this->_gateways[$id];
104
        }
105
106
        if (isset($this->_definitions[$id])) {
107
            $definition = $this->_definitions[$id];
108
            if (is_object($definition) && !$definition instanceof Closure) {
109
                return $this->_gateways[$id] = $definition;
110
            }
111
112
            return $this->_gateways[$id] = Yii::createObject($definition);
113
        } elseif ($throwException) {
114
            throw new InvalidConfigException("Unknown gateway ID: $id");
115
        }
116
117
        return null;
118
    }
119
120
    /**
121
     * Registers a filesystem definition with this locator.
122
     *
123
     * For example,
124
     *
125
     * ```php
126
     * // a class name
127
     * $locator->set('cache', 'yii\caching\FileCache');
128
     *
129
     * // a configuration array
130
     * $locator->set('db', [
131
     *     'class' => 'yii\db\Connection',
132
     *     'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
133
     *     'username' => 'root',
134
     *     'password' => '',
135
     *     'charset' => 'utf8',
136
     * ]);
137
     *
138
     * // an anonymous function
139
     * $locator->set('cache', function ($params) {
140
     *     return new \yii\caching\FileCache;
141
     * });
142
     *
143
     * // an instance
144
     * $locator->set('cache', new \yii\caching\FileCache);
145
     * ```
146
     *
147
     * If a filesystem definition with the same ID already exists, it will be overwritten.
148
     *
149
     * @param string $id filesystem ID (e.g. `db`).
150
     * @param mixed $definition the filesystem definition to be registered with this locator.
151
     * It can be one of the following:
152
     *
153
     * - a class name
154
     * - a configuration array: the array contains name-value pairs that will be used to
155
     *   initialize the property values of the newly created object when [[get()]] is called.
156
     *   The `class` element is required and stands for the the class of the object to be created.
157
     * - a PHP callable: either an anonymous function or an array representing a class method (e.g. `['Foo', 'bar']`).
158
     *   The callable will be called by [[get()]] to return an object associated with the specified filesystem ID.
159
     * - an object: When [[get()]] is called, this object will be returned.
160
     *
161
     * @throws InvalidConfigException if the definition is an invalid configuration array
162
     */
163
    public function set($id, $definition)
164
    {
165
        unset($this->_gateways[$id]);
166
167
        if ($definition === null) {
168
            unset($this->_definitions[$id]);
169
            return;
170
        }
171
172
        if (is_object($definition) || is_callable($definition, true)) {
173
            // an object, a class name, or a PHP callable
174
            $this->_definitions[$id] = $definition;
175
        } elseif (is_array($definition)) {
176
            // a configuration array
177
            if (isset($definition['class'])) {
178
                $this->_definitions[$id] = $definition;
179
            } else {
180
                throw new InvalidConfigException("The configuration for the \"$id\" gateway must contain a \"class\" element.");
181
            }
182
        } else {
183
            throw new InvalidConfigException("Unexpected configuration type for the \"$id\" gateway: " . gettype($definition));
184
        }
185
    }
186
187
    /**
188
     * Removes the gateway from the locator.
189
     * @param string $id the gateway ID
190
     */
191
    public function clear($id)
192
    {
193
        unset($this->_definitions[$id], $this->_gateways[$id]);
194
    }
195
196
    /**
197
     * Returns the list of the gateway definitions or the loaded gateway instances.
198
     * @param bool $returnDefinitions whether to return gateway definitions instead of the loaded gateway instances.
199
     * @return array the list of the gateway definitions or the loaded gateway instances (ID => definition or instance).
200
     */
201
    public function getGateways($returnDefinitions = true)
202
    {
203
        return $returnDefinitions ? $this->_definitions : $this->_gateways;
204
    }
205
206
    /**
207
     * Registers a set of gateway definitions in this locator.
208
     *
209
     * This is the bulk version of [[set()]]. The parameter should be an array
210
     * whose keys are gateway IDs and values the corresponding gateway definitions.
211
     *
212
     * For more details on how to specify gateway IDs and definitions, please refer to [[set()]].
213
     *
214
     * If a gateway definition with the same ID already exists, it will be overwritten.
215
     *
216
     * The following is an example for registering two gateway definitions:
217
     *
218
     * ```php
219
     * [
220
     *     'local' => [
221
     *         'class' => 'yuncms\payment\gateways\LocalAdapter',
222
     *         'path' => '@root/storage',
223
     *     ],
224
     * ]
225
     * ```
226
     *
227
     * @param array $gateways gateway definitions or instances
228
     * @throws InvalidConfigException
229
     */
230
    public function setGateways($gateways)
231
    {
232
        foreach ($gateways as $id => $gateway) {
233
            $this->set($id, $gateway);
234
        }
235
    }
236
}