1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\BearyChat\Laravel; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use ElfSundae\BearyChat\Client; |
8
|
|
|
|
9
|
|
|
class ClientManager |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The default client name. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $defaultName; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The defaults for all clients. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $clientsDefaults = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The clients config. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $clientsConfig = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The array of resolved BearyChat clients. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $clients = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The registered custom HTTP client creator. |
41
|
|
|
* |
42
|
|
|
* @var \Closure |
43
|
|
|
*/ |
44
|
|
|
protected $httpClientCreator; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the default client name. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
4 |
|
public function getDefaultName() |
52
|
|
|
{ |
53
|
4 |
|
return $this->defaultName ?: $this->getFirstClientName(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the first client name. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
4 |
|
protected function getFirstClientName() |
62
|
|
|
{ |
63
|
4 |
|
reset($this->clientsConfig); |
64
|
|
|
|
65
|
4 |
|
return key($this->clientsConfig); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the default client name. |
70
|
|
|
* |
71
|
|
|
* @param string $name |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
1 |
|
public function setDefaultName($name) |
75
|
|
|
{ |
76
|
1 |
|
$this->defaultName = $name; |
77
|
|
|
|
78
|
1 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the clients defaults. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
1 |
|
public function getClientsDefaults() |
87
|
|
|
{ |
88
|
1 |
|
return $this->clientsDefaults; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the clients defaults. |
93
|
|
|
* |
94
|
|
|
* @param array $defaults |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
2 |
|
public function setClientsDefaults($defaults) |
98
|
|
|
{ |
99
|
2 |
|
if (is_array($defaults)) { |
100
|
2 |
|
$this->clientsDefaults = $defaults; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the clients config. |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
1 |
|
public function getClientsConfig() |
112
|
|
|
{ |
113
|
1 |
|
return $this->clientsConfig; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set the clients config. |
118
|
|
|
* |
119
|
|
|
* @param array $config |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
4 |
|
public function setClientsConfig($config) |
123
|
|
|
{ |
124
|
4 |
|
if (is_array($config)) { |
125
|
4 |
|
$this->clientsConfig = $config; |
126
|
|
|
} |
127
|
|
|
|
128
|
4 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get a client instance. |
133
|
|
|
* |
134
|
|
|
* @param string|null $name |
135
|
|
|
* @return \ElfSundae\BearyChat\Client |
136
|
|
|
*/ |
137
|
3 |
|
public function client($name = null) |
138
|
|
|
{ |
139
|
3 |
|
if (is_null($name)) { |
140
|
3 |
|
$name = $this->getDefaultName(); |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
if (! isset($this->clients[$name])) { |
144
|
3 |
|
$this->clients[$name] = new Client( |
145
|
3 |
|
$this->getWebhookForClient($name), |
146
|
3 |
|
$this->getMessageDefaultsForClient($name), |
147
|
3 |
|
$this->getHttpClientForClient($name) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
return $this->clients[$name]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the webhook for the given client. |
156
|
|
|
* |
157
|
|
|
* @param string $name |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
3 |
|
public function getWebhookForClient($name) |
161
|
|
|
{ |
162
|
3 |
|
return Arr::get($this->clientsConfig[$name], 'webhook') ?: |
163
|
3 |
|
Arr::get($this->clientsDefaults, 'webhook'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the message defaults for the given client. |
168
|
|
|
* |
169
|
|
|
* @param string $name |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
3 |
|
public function getMessageDefaultsForClient($name) |
173
|
|
|
{ |
174
|
3 |
|
return array_merge( |
175
|
3 |
|
Arr::get($this->clientsDefaults, 'message_defaults', []), |
176
|
3 |
|
Arr::get($this->clientsConfig[$name], 'message_defaults', []) |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the HTTP client for the given client. |
182
|
|
|
* |
183
|
|
|
* @param string $name |
184
|
|
|
* @return \GuzzleHttp\Client|null |
185
|
|
|
*/ |
186
|
3 |
|
protected function getHttpClientForClient($name) |
187
|
|
|
{ |
188
|
3 |
|
if ($creator = $this->httpClientCreator) { |
189
|
1 |
|
return $creator($name); |
190
|
|
|
} |
191
|
2 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Register a custom HTTP client creator Closure. |
195
|
|
|
* |
196
|
|
|
* @param \Closure $creator |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
1 |
|
public function customHttpClient(Closure $creator) |
200
|
|
|
{ |
201
|
1 |
|
$this->httpClientCreator = $creator; |
202
|
|
|
|
203
|
1 |
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Dynamically call the default client instance. |
208
|
|
|
* |
209
|
|
|
* @param string $method |
210
|
|
|
* @param array $parameters |
211
|
|
|
* @return mixed |
212
|
|
|
*/ |
213
|
1 |
|
public function __call($method, $parameters) |
214
|
|
|
{ |
215
|
1 |
|
return call_user_func_array([$this->client(), $method], $parameters); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|