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