|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace drupol\Yaroc; |
|
4
|
|
|
|
|
5
|
|
|
use drupol\Yaroc\Plugin\ProviderInterface; |
|
6
|
|
|
use Http\Client\Common\Plugin\HeaderDefaultsPlugin; |
|
7
|
|
|
use Http\Client\Common\Plugin\RetryPlugin; |
|
8
|
|
|
use Http\Client\Common\PluginClient; |
|
9
|
|
|
use Http\Client\HttpClient; |
|
10
|
|
|
use Http\Discovery\HttpClientDiscovery; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Symfony\Component\Dotenv\Dotenv; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class RandomOrgAPI. |
|
16
|
|
|
*/ |
|
17
|
|
|
class RandomOrgAPI implements RandomOrgAPIInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The default Random.org endpoint. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string; |
|
23
|
|
|
*/ |
|
24
|
|
|
private $endpoint = 'https://api.random.org/json-rpc/1/invoke'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The configuration. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $configuration; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The HTTP client. |
|
35
|
|
|
* |
|
36
|
|
|
* @var \Http\Client\HttpClient |
|
37
|
|
|
*/ |
|
38
|
|
|
private $httpClient; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* RandomOrgAPI constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \Http\Client\HttpClient $httpClient |
|
|
|
|
|
|
44
|
|
|
* The HTTP client. |
|
45
|
|
|
* @param array $configuration |
|
46
|
|
|
* The configuration array. |
|
47
|
|
|
* |
|
48
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
49
|
|
|
*/ |
|
50
|
10 |
|
public function __construct(HttpClient $httpClient = null, array $configuration = []) |
|
51
|
|
|
{ |
|
52
|
10 |
|
if (null === $httpClient) { |
|
53
|
|
|
$defaultPlugins = [ |
|
54
|
10 |
|
new HeaderDefaultsPlugin([ |
|
55
|
10 |
|
'Content-Type' => 'application/json', |
|
56
|
|
|
'User-Agent' => 'YAROC (http://github.com/drupol/yaroc)', |
|
57
|
|
|
]), |
|
58
|
10 |
|
new RetryPlugin([ |
|
59
|
10 |
|
'retries' => 5, |
|
60
|
|
|
]), |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
10 |
|
$httpClient = new PluginClient(HttpClientDiscovery::find(), $defaultPlugins); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
10 |
|
$this->httpClient = $httpClient; |
|
67
|
|
|
|
|
68
|
10 |
|
$dotenv = new Dotenv(); |
|
69
|
|
|
|
|
70
|
10 |
|
$files = array_filter( |
|
71
|
|
|
[ |
|
72
|
10 |
|
__DIR__ . '/../.env.dist', |
|
73
|
|
|
__DIR__ . '/../.env' |
|
74
|
|
|
], |
|
75
|
10 |
|
'file_exists' |
|
76
|
|
|
); |
|
77
|
10 |
|
$dotenv->load(...$files); |
|
78
|
|
|
|
|
79
|
10 |
|
if ($apikey = getenv('RANDOM_ORG_APIKEY')) { |
|
80
|
10 |
|
$configuration += ['apiKey' => $apikey]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
10 |
|
$this->configuration = $configuration; |
|
84
|
10 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function withApiKey(string $apikey) :RandomOrgAPIInterface |
|
90
|
|
|
{ |
|
91
|
3 |
|
$clone = clone $this; |
|
92
|
|
|
|
|
93
|
3 |
|
$configuration = $clone->getConfiguration(); |
|
94
|
3 |
|
$configuration['apiKey'] = $apikey; |
|
95
|
3 |
|
$clone->configuration = $configuration; |
|
96
|
|
|
|
|
97
|
3 |
|
return $clone; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function withEndPoint(string $endpoint) :RandomOrgAPIInterface |
|
104
|
|
|
{ |
|
105
|
2 |
|
$clone = clone $this; |
|
106
|
2 |
|
$clone->endpoint = $endpoint; |
|
107
|
|
|
|
|
108
|
2 |
|
return $clone; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function withHttpClient(HttpClient $client) :RandomOrgAPIInterface |
|
115
|
|
|
{ |
|
116
|
1 |
|
$clone = clone $this; |
|
117
|
1 |
|
$clone->httpClient = $client; |
|
118
|
|
|
|
|
119
|
1 |
|
return $clone; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
6 |
|
public function getEndPoint() :string |
|
126
|
|
|
{ |
|
127
|
6 |
|
return $this->endpoint; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
6 |
|
public function getApiKey() :string |
|
134
|
|
|
{ |
|
135
|
6 |
|
$configuration = $this->getConfiguration(); |
|
136
|
|
|
|
|
137
|
6 |
|
return isset($configuration['apiKey']) ? $configuration['apiKey'] : ''; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
5 |
|
public function call(ProviderInterface $methodPlugin) :ResponseInterface |
|
144
|
|
|
{ |
|
145
|
5 |
|
$parameters = $methodPlugin->getParameters() + |
|
146
|
5 |
|
['apiKey' => $this->getApiKey()]; |
|
147
|
|
|
|
|
148
|
5 |
|
return $this->validateResponse( |
|
149
|
|
|
$methodPlugin |
|
150
|
5 |
|
->withEndPoint($this->getEndPoint()) |
|
151
|
5 |
|
->withHttpClient($this->getHttpClient()) |
|
152
|
5 |
|
->withParameters($parameters) |
|
153
|
5 |
|
->request() |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
3 |
|
public function get(ProviderInterface $methodPlugin) :array |
|
161
|
|
|
{ |
|
162
|
3 |
|
return json_decode( |
|
163
|
|
|
(string) $this |
|
|
|
|
|
|
164
|
3 |
|
->call($methodPlugin) |
|
165
|
3 |
|
->getBody() |
|
166
|
3 |
|
->getContents(), |
|
167
|
3 |
|
true |
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* {@inheritdoc} |
|
173
|
|
|
*/ |
|
174
|
2 |
|
public function getData(ProviderInterface $methodPlugin) |
|
175
|
|
|
{ |
|
176
|
2 |
|
$data = $this->get($methodPlugin); |
|
177
|
|
|
|
|
178
|
2 |
|
if (!isset($data['result'])) { |
|
179
|
|
|
return false; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
2 |
|
if (isset($data['result']['random']['data'])) { |
|
183
|
2 |
|
return $data['result']['random']['data']; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return false; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* {@inheritdoc} |
|
191
|
|
|
*/ |
|
192
|
7 |
|
public function getConfiguration() :array |
|
193
|
|
|
{ |
|
194
|
7 |
|
return $this->configuration; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* {@inheritdoc} |
|
199
|
|
|
*/ |
|
200
|
6 |
|
public function getHttpClient() :HttpClient |
|
201
|
|
|
{ |
|
202
|
6 |
|
return $this->httpClient; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Validate the response. |
|
207
|
|
|
* |
|
208
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
209
|
|
|
* |
|
210
|
|
|
* @return \Exception|ResponseInterface |
|
211
|
|
|
*/ |
|
212
|
5 |
|
private function validateResponse(ResponseInterface $response) :ResponseInterface |
|
213
|
|
|
{ |
|
214
|
5 |
|
if (200 === $response->getStatusCode()) { |
|
215
|
5 |
|
$body = json_decode((string) $response->getBody()->getContents(), true); |
|
216
|
|
|
|
|
217
|
5 |
|
if (isset($body['error']['code'])) { |
|
218
|
1 |
|
switch ($body['error']['code']) { |
|
219
|
1 |
View Code Duplication |
case -32600: |
|
|
|
|
|
|
220
|
|
|
throw new \InvalidArgumentException( |
|
221
|
|
|
'Invalid Request: ' . $body['error']['message'], |
|
222
|
|
|
$body['error']['code'] |
|
223
|
|
|
); |
|
224
|
1 |
|
case -32601: |
|
225
|
1 |
|
throw new \BadFunctionCallException( |
|
226
|
1 |
|
'Procedure not found: ' . $body['error']['message'], |
|
227
|
1 |
|
$body['error']['code'] |
|
228
|
|
|
); |
|
229
|
1 |
View Code Duplication |
case -32602: |
|
|
|
|
|
|
230
|
1 |
|
throw new \InvalidArgumentException( |
|
231
|
1 |
|
'Invalid arguments: ' . $body['error']['message'], |
|
232
|
1 |
|
$body['error']['code'] |
|
233
|
|
|
); |
|
234
|
1 |
View Code Duplication |
case -32603: |
|
|
|
|
|
|
235
|
|
|
throw new \RuntimeException( |
|
236
|
|
|
'Internal Error: ' . $body['error']['message'], |
|
237
|
|
|
$body['error']['code'] |
|
238
|
|
|
); |
|
239
|
|
View Code Duplication |
default: |
|
|
|
|
|
|
240
|
1 |
|
throw new \RuntimeException( |
|
241
|
1 |
|
'Invalid request/response: ' . $body['error']['message'], |
|
242
|
1 |
|
$body['error']['code'] |
|
243
|
|
|
); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
4 |
|
$response->getBody()->rewind(); |
|
249
|
|
|
|
|
250
|
4 |
|
return $response; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.