1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\Yaroc; |
4
|
|
|
|
5
|
|
|
use drupol\Yaroc\Http\Client; |
6
|
|
|
use drupol\Yaroc\Plugin\MethodPluginInterface; |
7
|
|
|
use drupol\Yaroc\Plugin\MethodPluginManager; |
8
|
|
|
use Http\Client\Common\Plugin; |
9
|
|
|
use Http\Client\HttpClient; |
10
|
|
|
use Http\Message\UriFactory; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class RandomOrgAPIClient |
15
|
|
|
* |
16
|
|
|
* @package drupol\Yaroc |
17
|
|
|
*/ |
18
|
|
|
class RandomOrgAPI { |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The default Random.org endpoint template. |
22
|
|
|
* |
23
|
|
|
* @var string; |
24
|
|
|
*/ |
25
|
|
|
protected $endpoint = 'https://api.random.org/json-rpc/%s/invoke'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The Random.org api key. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $apiKey = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The HTTP client. |
36
|
|
|
* |
37
|
|
|
* @var Client |
38
|
|
|
*/ |
39
|
|
|
protected $httpClient; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The Method plugin manager. |
43
|
|
|
* |
44
|
|
|
* @var MethodPluginManager |
45
|
|
|
*/ |
46
|
|
|
protected $methodPluginManager; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The Random.org API version. |
50
|
|
|
* |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $apiVersion = 1; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The response if any. |
57
|
|
|
* |
58
|
|
|
* @var bool|ResponseInterface |
59
|
|
|
*/ |
60
|
|
|
protected $response = FALSE; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var MethodPluginInterface |
64
|
|
|
*/ |
65
|
|
|
protected $methodPlugin = FALSE; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* RandomOrgAPI constructor. |
69
|
|
|
* |
70
|
|
|
* @param null|\Http\Client\HttpClient $httpClient |
71
|
|
|
* The HTTP client. |
72
|
|
|
*/ |
73
|
16 |
|
public function __construct(HttpClient $httpClient = NULL) { |
74
|
16 |
|
$this->setHttpClient($httpClient); |
75
|
16 |
|
$this->setMethodPluginManager(new MethodPluginManager()); |
76
|
16 |
|
$this->setApiVersion($this->getApiVersion()); |
77
|
16 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the Random.org endpoint template. |
81
|
|
|
* |
82
|
|
|
* @param string $uri |
83
|
|
|
* The URI. |
84
|
|
|
* |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
2 |
|
public function setEndpoint($uri) { |
88
|
2 |
|
$this->endpoint = $uri; |
89
|
2 |
|
$this->getHttpClient()->setEndpoint($this->getEndpoint()); |
90
|
|
|
|
91
|
2 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the Random.org endpoint. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
17 |
|
public function getEndpoint() { |
100
|
17 |
|
return sprintf($this->endpoint, $this->getApiVersion()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set the Method plugin manager. |
105
|
|
|
* |
106
|
|
|
* @param MethodPluginManager $methodPluginManager |
107
|
|
|
* The method plugin manager. |
108
|
|
|
* |
109
|
|
|
* @return self |
110
|
|
|
*/ |
111
|
16 |
|
public function setMethodPluginManager(MethodPluginManager $methodPluginManager) { |
112
|
16 |
|
$this->methodPluginManager = $methodPluginManager; |
113
|
|
|
|
114
|
16 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Return the Method plugin manager. |
119
|
|
|
* |
120
|
|
|
* @return \drupol\Yaroc\Plugin\MethodPluginManager |
121
|
|
|
*/ |
122
|
13 |
|
public function getMethodPluginManager() { |
123
|
13 |
|
return $this->methodPluginManager; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set the client request. |
128
|
|
|
* |
129
|
|
|
* @param null|HttpClient $httpClient |
130
|
|
|
* The client request. |
131
|
|
|
* @param null|UriFactory $uriFactory |
132
|
|
|
* The URI Factory. |
133
|
|
|
* @param Plugin[] $plugins |
134
|
|
|
* The HTTP plugins. |
135
|
|
|
* |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
16 |
|
public function setHttpClient(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, array $plugins = array()) { |
139
|
|
|
$defaultPlugins = [ |
140
|
16 |
|
new Plugin\HeaderDefaultsPlugin(['Content-Type' => 'application/json']) |
141
|
16 |
|
]; |
142
|
|
|
|
143
|
16 |
|
$plugins = array_merge(array_values($defaultPlugins), array_values($plugins)); |
|
|
|
|
144
|
16 |
|
$this->httpClient = new Client($httpClient, $uriFactory, $plugins); |
145
|
16 |
|
$this->httpClient->setEndpoint($this->getEndpoint()); |
146
|
|
|
|
147
|
16 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the Http client. |
152
|
|
|
* |
153
|
|
|
* @return Client |
154
|
|
|
*/ |
155
|
16 |
|
public function getHttpClient() { |
156
|
16 |
|
return $this->httpClient; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set the Random.org API Key. |
161
|
|
|
* |
162
|
|
|
* @param string $key |
163
|
|
|
* The API Key. |
164
|
|
|
* |
165
|
|
|
* @return self |
166
|
|
|
*/ |
167
|
16 |
|
public function setApiKey($key) { |
168
|
16 |
|
$this->apiKey = $key; |
169
|
|
|
|
170
|
16 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the Random.org API Key. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
* The API Key. |
178
|
|
|
*/ |
179
|
13 |
|
public function getApiKey() { |
180
|
13 |
|
return $this->apiKey; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the API version. |
185
|
|
|
* |
186
|
|
|
* @param int |
187
|
|
|
* The API version. |
188
|
|
|
* |
189
|
|
|
* @return self |
190
|
|
|
*/ |
191
|
16 |
|
public function setApiVersion($version) { |
192
|
16 |
|
$this->apiVersion = $version; |
193
|
16 |
|
$this->getHttpClient()->setEndpoint($this->getEndpoint()); |
194
|
|
|
|
195
|
16 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get the API version. |
200
|
|
|
* |
201
|
|
|
* @return int |
202
|
|
|
*/ |
203
|
16 |
|
public function getApiVersion() { |
204
|
16 |
|
return $this->apiVersion; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Set the method plugin. |
209
|
|
|
* |
210
|
|
|
* @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin |
211
|
|
|
* |
212
|
|
|
* @return False|self |
213
|
|
|
* Return itself, or FALSE otherwise. |
214
|
|
|
*/ |
215
|
13 |
|
public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) { |
216
|
13 |
|
$this->methodPlugin = $methodPlugin; |
217
|
|
|
|
218
|
13 |
|
return $methodPlugin ? $this : FALSE; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the method plugin. |
223
|
|
|
* |
224
|
|
|
* @return \drupol\Yaroc\Plugin\MethodPluginInterface |
225
|
|
|
*/ |
226
|
12 |
|
private function getMethodPlugin() { |
227
|
12 |
|
return $this->methodPlugin; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Set the response. |
232
|
|
|
* |
233
|
|
|
* @param \Psr\Http\Message\ResponseInterface|NULL $response |
234
|
|
|
* |
235
|
|
|
* @return self |
236
|
|
|
*/ |
237
|
13 |
|
private function setResponse(ResponseInterface $response = NULL) { |
238
|
13 |
|
$this->response = $response; |
239
|
|
|
|
240
|
13 |
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Get the response. |
245
|
|
|
* |
246
|
|
|
* @return bool|\Psr\Http\Message\ResponseInterface |
247
|
|
|
*/ |
248
|
14 |
|
public function getResponse() { |
249
|
14 |
|
return $this->response; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin |
254
|
|
|
* |
255
|
|
|
* @return ResponseInterface|\Exception |
256
|
|
|
*/ |
257
|
13 |
|
private function request(MethodPluginInterface $methodPlugin) { |
258
|
13 |
|
return $this->httpClient->request($methodPlugin); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Call Random.org API. |
263
|
|
|
* |
264
|
|
|
* @param string $method |
265
|
|
|
* The method to call. |
266
|
|
|
* @param array $params |
267
|
|
|
* The associative array of params as defined in the Random.org API. |
268
|
|
|
* |
269
|
|
|
* @return self |
270
|
|
|
* Returns itself. |
271
|
|
|
*/ |
272
|
16 |
|
public function call($method, array $params = array()) { |
273
|
16 |
|
$this->setResponse(NULL); |
274
|
16 |
|
$this->setMethodPlugin(NULL); |
275
|
|
|
|
276
|
16 |
|
$params += ['apiKey' => $this->getApiKey()]; |
277
|
|
|
|
278
|
16 |
|
if ($plugin = $this->getMethodPluginManager()->getPlugin($method, $params)) { |
279
|
14 |
|
$this->setMethodPlugin($plugin)->setResponse( |
|
|
|
|
280
|
14 |
|
$this->request( |
|
|
|
|
281
|
14 |
|
$this->getMethodPlugin()->setApiVersion($this->getApiVersion() |
282
|
14 |
|
) |
283
|
14 |
|
) |
284
|
13 |
|
); |
285
|
13 |
|
} |
286
|
|
|
|
287
|
15 |
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get the result array from the response. |
292
|
|
|
* |
293
|
|
|
* @return array|bool |
294
|
|
|
* The result array, FALSE otherwise. |
295
|
|
|
*/ |
296
|
14 |
|
public function getResult() { |
297
|
14 |
|
if ($this->getResponse() && $this->getMethodPlugin()) { |
298
|
13 |
|
return $this->getMethodPlugin()->getResult($this->getResponse()); |
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
1 |
|
return FALSE; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
} |
305
|
|
|
|