1 | <?php |
||
16 | class RandomOrgAPI { |
||
17 | |||
18 | /** |
||
19 | * The default Random.org endpoint template. |
||
20 | * |
||
21 | * @var string; |
||
22 | */ |
||
23 | protected $endpoint = 'https://api.random.org/json-rpc/%s/invoke'; |
||
24 | |||
25 | /** |
||
26 | * The Random.org api key. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $apiKey = ''; |
||
31 | |||
32 | /** |
||
33 | * The HTTP client. |
||
34 | * |
||
35 | * @var Client |
||
36 | */ |
||
37 | protected $httpClient; |
||
38 | |||
39 | /** |
||
40 | * The Method plugin manager. |
||
41 | * |
||
42 | * @var MethodPluginManager |
||
43 | */ |
||
44 | protected $methodPluginManager; |
||
45 | |||
46 | /** |
||
47 | * The Random.org API version. |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $apiVersion = 1; |
||
52 | |||
53 | /** |
||
54 | * The response if any. |
||
55 | * |
||
56 | * @var bool|ResponseInterface |
||
57 | */ |
||
58 | protected $response = FALSE; |
||
59 | |||
60 | /** |
||
61 | * @var MethodPluginInterface |
||
62 | */ |
||
63 | protected $methodPlugin = FALSE; |
||
64 | |||
65 | /** |
||
66 | * RandomOrgAPI constructor. |
||
67 | * |
||
68 | * @param null|\Http\Client\HttpClient $httpClient |
||
69 | * The HTTP client. |
||
70 | */ |
||
71 | 16 | public function __construct(HttpClient $httpClient = NULL) { |
|
72 | 16 | $this->setHttpClient($httpClient); |
|
73 | 16 | $this->setMethodPluginManager(new MethodPluginManager()); |
|
74 | 16 | $this->setApiVersion($this->getApiVersion()); |
|
75 | 16 | $this->getHttpClient()->setEndpoint($this->getEndpoint()); |
|
76 | 16 | } |
|
77 | |||
78 | /** |
||
79 | * Set the Random.org endpoint template. |
||
80 | * |
||
81 | * @param string $uri |
||
82 | * The URI. |
||
83 | * |
||
84 | * @return self |
||
85 | */ |
||
86 | 2 | public function setEndpoint($uri) { |
|
87 | 2 | $this->endpoint = $uri; |
|
88 | 2 | $this->getHttpClient()->setEndpoint($this->getEndpoint()); |
|
89 | |||
90 | 2 | return $this; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get the Random.org endpoint. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 17 | public function getEndpoint() { |
|
99 | 17 | return sprintf($this->endpoint, $this->getApiVersion()); |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Set the Method plugin manager. |
||
104 | * |
||
105 | * @param MethodPluginManager $methodPluginManager |
||
106 | * The method plugin manager. |
||
107 | * |
||
108 | * @return self |
||
109 | */ |
||
110 | 16 | public function setMethodPluginManager(MethodPluginManager $methodPluginManager) { |
|
111 | 16 | $this->methodPluginManager = $methodPluginManager; |
|
112 | |||
113 | 16 | return $this; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Return the Method plugin manager. |
||
118 | * |
||
119 | * @return \drupol\Yaroc\Plugin\MethodPluginManager |
||
120 | */ |
||
121 | 13 | public function getMethodPluginManager() { |
|
122 | 13 | return $this->methodPluginManager; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Set the client request. |
||
127 | * |
||
128 | * @param null|HttpClient $httpClient |
||
129 | * The client request. |
||
130 | * |
||
131 | * @return self |
||
132 | */ |
||
133 | 16 | public function setHttpClient(HttpClient $httpClient = NULL) { |
|
134 | 16 | $this->httpClient = new Client($httpClient, NULL, NULL); |
|
|
|||
135 | |||
136 | 16 | return $this; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Get the Http client. |
||
141 | * |
||
142 | * @return Client |
||
143 | */ |
||
144 | 16 | public function getHttpClient() { |
|
145 | 16 | return $this->httpClient; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Set the Random.org API Key. |
||
150 | * |
||
151 | * @param string $key |
||
152 | * The API Key. |
||
153 | * |
||
154 | * @return self |
||
155 | */ |
||
156 | 16 | public function setApiKey($key) { |
|
157 | 16 | $this->apiKey = $key; |
|
158 | |||
159 | 16 | return $this; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * Get the Random.org API Key. |
||
164 | * |
||
165 | * @return string |
||
166 | * The API Key. |
||
167 | */ |
||
168 | 13 | public function getApiKey() { |
|
169 | 13 | return $this->apiKey; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Set the API version. |
||
174 | * |
||
175 | * @param int |
||
176 | * The API version. |
||
177 | * |
||
178 | * @return self |
||
179 | */ |
||
180 | 16 | public function setApiVersion($version) { |
|
181 | 16 | $this->apiVersion = $version; |
|
182 | 16 | $this->getHttpClient()->setEndpoint($this->getEndpoint()); |
|
183 | |||
184 | 16 | return $this; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * Get the API version. |
||
189 | * |
||
190 | * @return int |
||
191 | */ |
||
192 | 16 | public function getApiVersion() { |
|
193 | 16 | return $this->apiVersion; |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Set the method plugin. |
||
198 | * |
||
199 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin |
||
200 | * |
||
201 | * @return False|self |
||
202 | * Return itself, or FALSE otherwise. |
||
203 | */ |
||
204 | 13 | public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) { |
|
205 | 13 | $this->methodPlugin = $methodPlugin; |
|
206 | |||
207 | 13 | return $methodPlugin ? $this : FALSE; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Get the method plugin. |
||
212 | * |
||
213 | * @return \drupol\Yaroc\Plugin\MethodPluginInterface |
||
214 | */ |
||
215 | 12 | private function getMethodPlugin() { |
|
218 | |||
219 | /** |
||
220 | * Set the response. |
||
221 | * |
||
222 | * @param \Psr\Http\Message\ResponseInterface|NULL $response |
||
223 | * |
||
224 | * @return self |
||
225 | */ |
||
226 | 13 | private function setResponse(ResponseInterface $response = NULL) { |
|
227 | 13 | $this->response = $response; |
|
228 | |||
229 | 13 | return $this; |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * Get the response. |
||
234 | * |
||
235 | * @return bool|\Psr\Http\Message\ResponseInterface |
||
236 | */ |
||
237 | 14 | public function getResponse() { |
|
240 | |||
241 | /** |
||
242 | * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin |
||
243 | * |
||
244 | * @return ResponseInterface|\Exception |
||
245 | */ |
||
246 | 13 | private function request(MethodPluginInterface $methodPlugin) { |
|
249 | |||
250 | /** |
||
251 | * Call Random.org API. |
||
252 | * |
||
253 | * @param string $method |
||
254 | * The method to call. |
||
255 | * @param array $params |
||
256 | * The associative array of params as defined in the Random.org API. |
||
257 | * |
||
258 | * @return self |
||
259 | * Returns itself. |
||
260 | */ |
||
261 | 16 | public function call($method, array $params = array()) { |
|
278 | |||
279 | /** |
||
280 | * Get the result array from the response. |
||
281 | * |
||
282 | * @return array|bool |
||
283 | * The result array, FALSE otherwise. |
||
284 | */ |
||
285 | 14 | public function getResult() { |
|
292 | |||
293 | } |
||
294 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.