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 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() { |
|
125 | |||
126 | /** |
||
127 | * Set the client request. |
||
128 | * |
||
129 | * @param null|HttpClient $httpClient |
||
130 | * The client request. |
||
131 | * @param 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()) { |
|
149 | |||
150 | /** |
||
151 | * Set the HTTP plugins. |
||
152 | * |
||
153 | * @param Plugin[] $plugins |
||
154 | * An array of HTTP plugin. |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function setPlugins(array $plugins = array()) { |
||
161 | |||
162 | /** |
||
163 | * Get the Http client. |
||
164 | * |
||
165 | * @return Client |
||
166 | */ |
||
167 | 16 | public function getHttpClient() { |
|
170 | |||
171 | /** |
||
172 | * Set the Random.org API Key. |
||
173 | * |
||
174 | * @param string $key |
||
175 | * The API Key. |
||
176 | * |
||
177 | * @return self |
||
178 | */ |
||
179 | 16 | public function setApiKey($key) { |
|
184 | |||
185 | /** |
||
186 | * Get the Random.org API Key. |
||
187 | * |
||
188 | * @return string |
||
189 | * The API Key. |
||
190 | */ |
||
191 | 13 | public function getApiKey() { |
|
194 | |||
195 | /** |
||
196 | * Set the API version. |
||
197 | * |
||
198 | * @param int |
||
199 | * The API version. |
||
200 | * |
||
201 | * @return self |
||
202 | */ |
||
203 | 16 | public function setApiVersion($version) { |
|
209 | |||
210 | /** |
||
211 | * Get the API version. |
||
212 | * |
||
213 | * @return int |
||
214 | */ |
||
215 | 16 | public function getApiVersion() { |
|
218 | |||
219 | /** |
||
220 | * Set the method plugin. |
||
221 | * |
||
222 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin |
||
223 | * |
||
224 | * @return False|self |
||
225 | * Return itself, or FALSE otherwise. |
||
226 | */ |
||
227 | 13 | public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) { |
|
232 | |||
233 | /** |
||
234 | * Get the method plugin. |
||
235 | * |
||
236 | * @return \drupol\Yaroc\Plugin\MethodPluginInterface |
||
237 | */ |
||
238 | 12 | private function getMethodPlugin() { |
|
241 | |||
242 | /** |
||
243 | * Set the response. |
||
244 | * |
||
245 | * @param \Psr\Http\Message\ResponseInterface|NULL $response |
||
246 | * |
||
247 | * @return self |
||
248 | */ |
||
249 | 13 | private function setResponse(ResponseInterface $response = NULL) { |
|
254 | |||
255 | /** |
||
256 | * Get the response. |
||
257 | * |
||
258 | * @return bool|\Psr\Http\Message\ResponseInterface |
||
259 | */ |
||
260 | 14 | public function getResponse() { |
|
263 | |||
264 | /** |
||
265 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin |
||
266 | * |
||
267 | * @return ResponseInterface|\Exception |
||
268 | */ |
||
269 | 13 | private function request(MethodPluginInterface $methodPlugin) { |
|
272 | |||
273 | /** |
||
274 | * Call Random.org API. |
||
275 | * |
||
276 | * @param string $method |
||
277 | * The method to call. |
||
278 | * @param array $params |
||
279 | * The associative array of params as defined in the Random.org API. |
||
280 | * |
||
281 | * @return self |
||
282 | * Returns itself. |
||
283 | */ |
||
284 | 16 | public function call($method, array $params = array()) { |
|
301 | |||
302 | /** |
||
303 | * Get the result array from the response. |
||
304 | * |
||
305 | * @return array|bool |
||
306 | * The result array, FALSE otherwise. |
||
307 | */ |
||
308 | 14 | public function getResult() { |
|
315 | |||
316 | } |
||
317 |