|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/vuongxuongminh/yii2-gatewayclients |
|
4
|
|
|
* @copyright Copyright (c) 2018 Vuong Xuong Minh |
|
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
namespace vxm\gatewayclients; |
|
10
|
|
|
|
|
11
|
|
|
use Yii; |
|
12
|
|
|
|
|
13
|
|
|
use yii\base\Component; |
|
14
|
|
|
use yii\base\InvalidArgumentException; |
|
15
|
|
|
use yii\helpers\ArrayHelper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class GatewayCollection. This is the collection of [[\vxm\gatewayclients\GatewayInterface]], it may set to application components for easily control gateways. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Vuong Minh <[email protected]> |
|
21
|
|
|
* @since 1.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class GatewayCollection extends Component |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array Common config gateways when init it use to config gateway object. |
|
28
|
|
|
*/ |
|
29
|
|
|
public $gatewayConfig = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array|GatewayInterface[] the gateways list. |
|
33
|
|
|
*/ |
|
34
|
|
|
private $_gateways = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Return an array gateway lists. |
|
38
|
|
|
* |
|
39
|
|
|
* @return GatewayInterface[] the gateway lists. |
|
40
|
|
|
* @throws \yii\base\InvalidConfigException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getGateways(): array |
|
43
|
|
|
{ |
|
44
|
|
|
$gateways = []; |
|
45
|
|
|
|
|
46
|
|
|
foreach ($this->_gateways as $id => $gateway) { |
|
47
|
|
|
$gateways[$id] = $this->getGateway($id); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $gateways; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array|GatewayInterface[] $gateways |
|
55
|
|
|
* @return bool Return true when gateways has been set. |
|
56
|
|
|
*/ |
|
57
|
3 |
|
public function setGateways(array $gateways): bool |
|
58
|
|
|
{ |
|
59
|
3 |
|
foreach ($gateways as $name => $gateway) { |
|
60
|
3 |
|
$this->setGateway($name, $gateway); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get gateway by id given. |
|
68
|
|
|
* |
|
69
|
|
|
* @param int|string $id An id of gateway need to get. |
|
70
|
|
|
* @return GatewayInterface object gateway define by arg name. |
|
71
|
|
|
* @throws \yii\base\InvalidConfigException |
|
72
|
|
|
*/ |
|
73
|
18 |
|
public function getGateway($id): GatewayInterface |
|
74
|
|
|
{ |
|
75
|
18 |
|
$gateway = $this->_gateways[$id] ?? null; |
|
76
|
|
|
|
|
77
|
18 |
|
if ($gateway === null) { |
|
78
|
|
|
throw new InvalidArgumentException("Gateway: `$id` not exist!"); |
|
79
|
18 |
|
} elseif (!$gateway instanceof GatewayInterface) { |
|
80
|
|
|
|
|
81
|
18 |
|
if (is_string($gateway)) { |
|
82
|
6 |
|
$gateway = ['class' => $gateway]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
18 |
|
if (is_array($gateway)) { |
|
86
|
12 |
|
$gateway = ArrayHelper::merge($this->gatewayConfig, $gateway); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
18 |
|
$gateway = $this->_gateways[$id] = Yii::createObject($gateway); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
18 |
|
return $gateway; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set gateway in to the gateway lists. This method called by [[setGateways()]]. |
|
97
|
|
|
* |
|
98
|
|
|
* @param int|string $id An id of gateway in the gateway lists. |
|
99
|
|
|
* @param string|array|GatewayInterface $gateway config or object gateway value define by name. |
|
100
|
|
|
* @return bool Return TRUE when gateway has been set. |
|
101
|
|
|
*/ |
|
102
|
18 |
|
public function setGateway($id, $gateway): bool |
|
103
|
|
|
{ |
|
104
|
18 |
|
$this->_gateways[$id] = $gateway; |
|
105
|
|
|
|
|
106
|
18 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Indicates if the gateway id has been set. |
|
111
|
|
|
* |
|
112
|
|
|
* @param $id |
|
113
|
|
|
* @return bool Return TRUE when gateway id exist. |
|
114
|
|
|
*/ |
|
115
|
9 |
|
public function hasGateway($id) |
|
116
|
|
|
{ |
|
117
|
9 |
|
return array_key_exists($id, $this->_gateways); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Magic getter method provide access gateway by unknown property name. |
|
122
|
|
|
* |
|
123
|
|
|
* @inheritdoc |
|
124
|
|
|
* @throws \yii\base\InvalidConfigException|\yii\base\UnknownPropertyException |
|
125
|
|
|
*/ |
|
126
|
3 |
|
public function __get($name) |
|
127
|
|
|
{ |
|
128
|
3 |
|
if ($this->hasGateway($name)) { |
|
129
|
3 |
|
return $this->getGateway($name); |
|
130
|
|
|
} else { |
|
131
|
|
|
parent::__get($name); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* This method is an alias of [[vxm\gatewayclients\GatewayInterface::request()]]. |
|
137
|
|
|
* |
|
138
|
|
|
* @param int|string $command The command of request |
|
139
|
|
|
* @param array $data An array data use to send to gateway server api. |
|
140
|
|
|
* @param int|string $gatewayId An id of gateway in the gateway lists. |
|
141
|
|
|
* @param null|int|string $clientId An id client of gateway in the client lists. If not set default client of gateway will be use to make request. |
|
142
|
|
|
* @return DataInterface An object data get from gateway server api. |
|
143
|
|
|
* @throws \yii\base\InvalidConfigException |
|
144
|
|
|
*/ |
|
145
|
9 |
|
public function request($command, array $data, $gatewayId, $clientId = null): DataInterface |
|
146
|
|
|
{ |
|
147
|
9 |
|
return $this->getGateway($gatewayId)->request($command, $data, $clientId); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|