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 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() { |
|
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) { |
|
197 | |||
198 | /** |
||
199 | * Get the API version. |
||
200 | * |
||
201 | * @return int |
||
202 | */ |
||
203 | 16 | public function getApiVersion() { |
|
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) { |
|
220 | |||
221 | /** |
||
222 | * Get the method plugin. |
||
223 | * |
||
224 | * @return \drupol\Yaroc\Plugin\MethodPluginInterface |
||
225 | */ |
||
226 | 12 | private function getMethodPlugin() { |
|
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) { |
|
242 | |||
243 | /** |
||
244 | * Get the response. |
||
245 | * |
||
246 | * @return bool|\Psr\Http\Message\ResponseInterface |
||
247 | */ |
||
248 | 14 | public function getResponse() { |
|
251 | |||
252 | /** |
||
253 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin |
||
254 | * |
||
255 | * @return ResponseInterface|\Exception |
||
256 | */ |
||
257 | 13 | private function request(MethodPluginInterface $methodPlugin) { |
|
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()) { |
|
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() { |
|
303 | |||
304 | } |
||
305 |