1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\Yaroc; |
4
|
|
|
|
5
|
|
|
use drupol\Yaroc\Plugin\MethodPluginInterface; |
6
|
|
|
use Http\Client\HttpClient; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class RandomOrgAPI. |
11
|
|
|
*/ |
12
|
|
|
class RandomOrgAPI implements RandomOrgAPIInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The default Random.org endpoint template. |
16
|
|
|
* |
17
|
|
|
* @var string; |
18
|
|
|
*/ |
19
|
|
|
private $endpoint = 'https://api.random.org/json-rpc/1/invoke'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The configuration. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $configuration; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The HTTP client. |
30
|
|
|
* |
31
|
|
|
* @var \Http\Client\HttpClient |
32
|
|
|
*/ |
33
|
|
|
private $httpClient; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* RandomOrgAPI constructor. |
37
|
|
|
* |
38
|
|
|
* @param array $configuration |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $configuration = []) |
41
|
|
|
{ |
42
|
|
|
$this->configuration = $configuration; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function withApiKey(string $apikey) |
49
|
|
|
{ |
50
|
|
|
$clone = clone $this; |
51
|
|
|
|
52
|
|
|
$configuration = $clone->getConfiguration(); |
53
|
|
|
$configuration['apiKey'] = $apikey; |
54
|
|
|
|
55
|
|
|
$clone->configuration = $configuration; |
56
|
|
|
|
57
|
|
|
return $clone; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function withEndPoint(string $endpoint) |
64
|
|
|
{ |
65
|
|
|
$clone = clone $this; |
66
|
|
|
$clone->endpoint = $endpoint; |
67
|
|
|
|
68
|
|
|
return $clone; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
16 |
|
*/ |
74
|
16 |
|
public function withHttpClient(HttpClient $client) |
75
|
16 |
|
{ |
76
|
16 |
|
$clone = clone $this; |
77
|
16 |
|
$clone->httpClient = $client; |
78
|
|
|
|
79
|
|
|
return $clone; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getEndPoint() |
86
|
|
|
{ |
87
|
16 |
|
return $this->endpoint; |
88
|
16 |
|
} |
89
|
|
|
|
90
|
16 |
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getApiKey() |
94
|
|
|
{ |
95
|
|
|
$configuration = $this->getConfiguration(); |
96
|
|
|
|
97
|
|
|
$configuration += ['apiKey' => '']; |
98
|
|
|
|
99
|
13 |
|
return $configuration['apiKey']; |
100
|
13 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function call(MethodPluginInterface $methodPlugin) |
106
|
|
|
{ |
107
|
|
|
$parameters = $methodPlugin->getParameters(); |
108
|
|
|
$parameters += ['apiKey' => $this->getApiKey()]; |
109
|
|
|
|
110
|
|
|
return $this->validateResponse($methodPlugin |
111
|
16 |
|
->withEndPoint($this->getEndPoint()) |
112
|
16 |
|
->withParameters($parameters)->call()); |
113
|
16 |
|
} |
114
|
|
|
|
115
|
16 |
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function get(MethodPluginInterface $methodPlugin) |
119
|
|
|
{ |
120
|
|
|
return json_decode( |
121
|
|
|
(string) $this |
|
|
|
|
122
|
|
|
->call($methodPlugin) |
123
|
16 |
|
->getBody() |
124
|
16 |
|
->getContents(), |
125
|
|
|
true |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function getData(MethodPluginInterface $methodPlugin) |
133
|
|
|
{ |
134
|
|
|
$data = $this->get($methodPlugin); |
135
|
2 |
|
|
136
|
2 |
|
if (!isset($data['result'])) { |
137
|
2 |
|
return false; |
138
|
|
|
} |
139
|
2 |
|
|
140
|
|
|
if (isset($data['result']['random']) && isset($data['result']['random']['data'])) { |
141
|
|
|
return $data['result']['random']['data']; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
17 |
|
/** |
148
|
17 |
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function getConfiguration() |
151
|
|
|
{ |
152
|
|
|
return $this->configuration; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function getHttpClient() |
159
|
|
|
{ |
160
|
|
|
return $this->httpClient; |
161
|
|
|
} |
162
|
|
|
|
163
|
16 |
|
/** |
164
|
|
|
* Validate the response. |
165
|
16 |
|
* |
166
|
16 |
|
* @param \Psr\Http\Message\ResponseInterface $response |
167
|
|
|
* |
168
|
|
|
* @return \Exception|ResponseInterface |
169
|
16 |
|
*/ |
170
|
16 |
|
private function validateResponse(ResponseInterface $response) |
171
|
|
|
{ |
172
|
|
|
if (200 == $response->getStatusCode()) { |
173
|
|
|
$body = json_decode((string) $response->getBody()->getContents(), true); |
174
|
16 |
|
|
175
|
16 |
|
if (isset($body['error']['code'])) { |
176
|
16 |
|
switch ($body['error']['code']) { |
177
|
|
View Code Duplication |
case -32600: |
|
|
|
|
178
|
16 |
|
throw new \InvalidArgumentException( |
179
|
|
|
'Invalid Request: ' . $body['error']['message'], |
180
|
|
|
$body['error']['code'] |
181
|
|
|
); |
182
|
|
|
case -32601: |
183
|
|
|
throw new \BadFunctionCallException( |
184
|
|
|
'Procedure not found: ' . $body['error']['message'], |
185
|
|
|
$body['error']['code'] |
186
|
16 |
|
); |
187
|
16 |
View Code Duplication |
case -32602: |
|
|
|
|
188
|
|
|
throw new \InvalidArgumentException( |
189
|
|
|
'Invalid arguments: ' . $body['error']['message'], |
190
|
|
|
$body['error']['code'] |
191
|
|
|
); |
192
|
|
View Code Duplication |
case -32603: |
|
|
|
|
193
|
|
|
throw new \RuntimeException( |
194
|
|
|
'Internal Error: ' . $body['error']['message'], |
195
|
|
|
$body['error']['code'] |
196
|
|
|
); |
197
|
|
View Code Duplication |
default: |
|
|
|
|
198
|
13 |
|
throw new \RuntimeException( |
199
|
13 |
|
'Invalid request/response: ' . $body['error']['message'], |
200
|
|
|
$body['error']['code'] |
201
|
13 |
|
); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$response->getBody()->rewind(); |
207
|
|
|
|
208
|
|
|
return $response; |
209
|
12 |
|
} |
210
|
|
|
} |
211
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: