1 | <?php |
||
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 logger. |
||
43 | * |
||
44 | * @var \Psr\Log\LoggerInterface |
||
45 | */ |
||
46 | protected $logger; |
||
47 | |||
48 | /** |
||
49 | * The Method plugin manager. |
||
50 | * |
||
51 | * @var MethodPluginManager |
||
52 | */ |
||
53 | protected $methodPluginManager; |
||
54 | |||
55 | /** |
||
56 | * The Random.org API version. |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $apiVersion = 1; |
||
61 | |||
62 | /** |
||
63 | * The response if any. |
||
64 | * |
||
65 | * @var bool|ResponseInterface |
||
66 | */ |
||
67 | protected $response = FALSE; |
||
68 | |||
69 | /** |
||
70 | * @var MethodPluginInterface |
||
71 | */ |
||
72 | protected $methodPlugin = FALSE; |
||
73 | |||
74 | /** |
||
75 | * RandomOrgAPI constructor. |
||
76 | * |
||
77 | * @param null|\Http\Client\HttpClient $httpClient |
||
78 | * @param null|\Psr\Log\LoggerInterface $logger |
||
79 | */ |
||
80 | 16 | public function __construct(HttpClient $httpClient = NULL, LoggerInterface $logger = NULL) { |
|
87 | |||
88 | /** |
||
89 | * Set the Random.org endpoint template. |
||
90 | * |
||
91 | * @param string $uri |
||
92 | * The URI. |
||
93 | * |
||
94 | * @return self |
||
95 | */ |
||
96 | 2 | public function setEndpoint($uri) { |
|
102 | |||
103 | /** |
||
104 | * Get the Random.org endpoint. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 17 | public function getEndpoint() { |
|
111 | |||
112 | /** |
||
113 | * Set the Method plugin manager. |
||
114 | * |
||
115 | * @param MethodPluginManager $methodPluginManager |
||
116 | * The method plugin manager. |
||
117 | * |
||
118 | * @return self |
||
119 | */ |
||
120 | 16 | public function setMethodPluginManager(MethodPluginManager $methodPluginManager) { |
|
125 | |||
126 | /** |
||
127 | * Return the Method plugin manager. |
||
128 | * |
||
129 | * @return \drupol\Yaroc\Plugin\MethodPluginManager |
||
130 | */ |
||
131 | 13 | public function getMethodPluginManager() { |
|
134 | |||
135 | /** |
||
136 | * Get the logger. |
||
137 | * |
||
138 | * @return \Psr\Log\LoggerInterface |
||
139 | */ |
||
140 | 15 | public function getLogger() { |
|
143 | |||
144 | /** |
||
145 | * Set the logger. |
||
146 | * |
||
147 | * @param null|\Psr\Log\LoggerInterface $logger |
||
148 | * The logger. |
||
149 | * |
||
150 | * @return self |
||
151 | */ |
||
152 | 15 | public function setLogger(LoggerInterface $logger = NULL) { |
|
157 | |||
158 | /** |
||
159 | * Set the client request. |
||
160 | * |
||
161 | * @param null|HttpClient $httpClient |
||
162 | * The client request. |
||
163 | * |
||
164 | * @return self |
||
165 | */ |
||
166 | 16 | public function setHttpClient(HttpClient $httpClient = NULL) { |
|
171 | |||
172 | /** |
||
173 | * Get the Http client. |
||
174 | * |
||
175 | * @return Client |
||
176 | */ |
||
177 | 16 | public function getHttpClient() { |
|
180 | |||
181 | /** |
||
182 | * Set the Random.org API Key. |
||
183 | * |
||
184 | * @param string $key |
||
185 | * The API Key. |
||
186 | * |
||
187 | * @return self |
||
188 | */ |
||
189 | 16 | public function setApiKey($key) { |
|
194 | |||
195 | /** |
||
196 | * Get the Random.org API Key. |
||
197 | * |
||
198 | * @return string |
||
199 | * The API Key. |
||
200 | */ |
||
201 | 13 | public function getApiKey() { |
|
204 | |||
205 | /** |
||
206 | * Set the API version. |
||
207 | * |
||
208 | * @param int |
||
209 | * The API version. |
||
210 | * |
||
211 | * @return self |
||
212 | */ |
||
213 | 16 | public function setApiVersion($version) { |
|
219 | |||
220 | /** |
||
221 | * Get the API version. |
||
222 | * |
||
223 | * @return int |
||
224 | */ |
||
225 | 16 | public function getApiVersion() { |
|
228 | |||
229 | /** |
||
230 | * Set the method plugin. |
||
231 | * |
||
232 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin |
||
233 | * |
||
234 | * @return False|self |
||
235 | * Return itself, or FALSE otherwise. |
||
236 | */ |
||
237 | 13 | public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) { |
|
242 | |||
243 | /** |
||
244 | * Get the method plugin. |
||
245 | * |
||
246 | * @return \drupol\Yaroc\Plugin\MethodPluginInterface |
||
247 | */ |
||
248 | 12 | private function getMethodPlugin() { |
|
251 | |||
252 | /** |
||
253 | * Set the response. |
||
254 | * |
||
255 | * @param \Psr\Http\Message\ResponseInterface|NULL $response |
||
256 | * |
||
257 | * @return self |
||
258 | */ |
||
259 | 13 | private function setResponse(ResponseInterface $response = NULL) { |
|
264 | |||
265 | /** |
||
266 | * Get the response. |
||
267 | * |
||
268 | * @return bool|\Psr\Http\Message\ResponseInterface |
||
269 | */ |
||
270 | 14 | public function getResponse() { |
|
273 | |||
274 | /** |
||
275 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin |
||
276 | * |
||
277 | * @return ResponseInterface|\Exception |
||
278 | */ |
||
279 | 13 | private function request(MethodPluginInterface $methodPlugin) { |
|
282 | |||
283 | /** |
||
284 | * Call Random.org API. |
||
285 | * |
||
286 | * @param string $method |
||
287 | * The method to call. |
||
288 | * @param array $params |
||
289 | * The associative array of params as defined in the Random.org API. |
||
290 | * |
||
291 | * @return self |
||
292 | * Returns itself. |
||
293 | */ |
||
294 | 16 | public function call($method, array $params = array()) { |
|
311 | |||
312 | /** |
||
313 | * Get the result array from the response. |
||
314 | * |
||
315 | * @return array|bool |
||
316 | * The result array, FALSE otherwise. |
||
317 | */ |
||
318 | 14 | public function getResult() { |
|
325 | |||
326 | } |
||
327 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.