Completed
Push — master ( e975be...c9387b )
by Pol
05:02
created

RandomOrgAPI::setApiVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace drupol\Yaroc;
4
5
use drupol\Yaroc\Http\Client;
6
use drupol\Yaroc\Plugin\MethodPluginInterface;
7
use drupol\Yaroc\Plugin\MethodPluginManager;
8
use Http\Client\Common\Plugin;
9
use Http\Client\HttpClient;
10
use Http\Message\UriFactory;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * Class RandomOrgAPIClient
15
 *
16
 * @package drupol\Yaroc
17
 */
18
class RandomOrgAPI {
0 ignored issues
show
Complexity introduced by
The class RandomOrgAPI has a coupling between objects value of 14. Consider to reduce the number of dependencies under 13.
Loading history...
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 API Key.
81
   *
82
   * @param string $key
83
   *   The API Key.
84
   *
85
   * @return self
86
   */
87 16
  public function setApiKey($key) {
88 16
    $this->apiKey = $key;
89
90 16
    return $this;
91
  }
92
93
  /**
94
   * Get the Random.org API Key.
95
   *
96
   * @return string
97
   *   The API Key.
98
   */
99 13
  public function getApiKey() {
100 13
    return $this->apiKey;
101
  }
102
103
  /**
104
   * Set the API version.
105
   *
106
   * @param int
107
   *   The API version.
108
   *
109
   * @return self
110
   */
111 16
  public function setApiVersion($version) {
112 16
    $this->apiVersion = $version;
113 16
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
114
115 16
    return $this;
116
  }
117
118
  /**
119
   * Get the API version.
120
   *
121
   * @return int
122
   */
123 16
  public function getApiVersion() {
124 16
    return $this->apiVersion;
125
  }
126
127
  /**
128
   * Set the Random.org endpoint template.
129
   *
130
   * @param string $uri
131
   *   The URI.
132
   *
133
   * @return self
134
   */
135 2
  public function setEndpoint($uri) {
136 2
    $this->endpoint = $uri;
137 2
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
138
139 2
    return $this;
140
  }
141
142
  /**
143
   * Get the Random.org endpoint.
144
   *
145
   * @return string
146
   */
147 17
  public function getEndpoint() {
148 17
    return sprintf($this->endpoint, $this->getApiVersion());
149
  }
150
151
  /**
152
   * Set the client request.
153
   *
154
   * @param null|HttpClient $httpClient
155
   *   The client request.
156
   * @param null|UriFactory $uriFactory
157
   *   The URI Factory.
158
   * @param Plugin[] $plugins
159
   *   The HTTP plugins.
160
   *
161
   * @return self
162
   */
163 16
  public function setHttpClient(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, array $plugins = array()) {
164
    $defaultPlugins = [
165 16
      new Plugin\HeaderDefaultsPlugin([
166 16
        'Content-Type' => 'application/json',
167
        'User-Agent' => 'YAROC (http://github.com/drupol/yaroc)',
168
      ]),
169 16
      new Plugin\RetryPlugin([
170 16
        'retries' => 5,
171
      ]),
172
    ];
173
174 16
    $plugins = array_merge(array_values($defaultPlugins), array_values($plugins));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $plugins. This often makes code more readable.
Loading history...
175 16
    $this->httpClient = new Client($httpClient, $uriFactory, $plugins);
176 16
    $this->httpClient->setEndpoint($this->getEndpoint());
177
178 16
    return $this;
179
  }
180
181
  /**
182
   * Get the Http client.
183
   *
184
   * @return Client
185
   */
186 16
  public function getHttpClient() {
187 16
    return $this->httpClient;
188
  }
189
190
  /**
191
   * Set the method plugin.
192
   *
193
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin
194
   *
195
   * @return False|self
196
   *   Return itself, or FALSE otherwise.
197
   */
198 13
  public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) {
199 13
    $this->methodPlugin = $methodPlugin;
200
201 13
    return $methodPlugin ? $this : FALSE;
202
  }
203
204
  /**
205
   * Get the method plugin.
206
   *
207
   * @return \drupol\Yaroc\Plugin\MethodPluginInterface
208
   */
209 12
  private function getMethodPlugin() {
210 12
    return $this->methodPlugin;
211
  }
212
213
  /**
214
   * Set the Method plugin manager.
215
   *
216
   * @param MethodPluginManager $methodPluginManager
217
   *   The method plugin manager.
218
   *
219
   * @return self
220
   */
221 16
  public function setMethodPluginManager(MethodPluginManager $methodPluginManager) {
222 16
    $this->methodPluginManager = $methodPluginManager;
223
224 16
    return $this;
225
  }
226
227
  /**
228
   * Return the Method plugin manager.
229
   *
230
   * @return \drupol\Yaroc\Plugin\MethodPluginManager
231
   */
232 13
  public function getMethodPluginManager() {
233 13
    return $this->methodPluginManager;
234
  }
235
236
  /**
237
   * Set the response.
238
   *
239
   * @param \Psr\Http\Message\ResponseInterface|NULL $response
240
   *
241
   * @return self
242
   */
243 13
  private function setResponse(ResponseInterface $response = NULL) {
244 13
    $this->response = $response;
245
246 13
    return $this;
247
  }
248
249
  /**
250
   * Get the response.
251
   *
252
   * @return bool|\Psr\Http\Message\ResponseInterface
253
   */
254 14
  public function getResponse() {
255 14
    return $this->response;
256
  }
257
258
  /**
259
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin
260
   *
261
   * @return ResponseInterface|\Exception
262
   */
263 13
  private function request(MethodPluginInterface $methodPlugin) {
264 13
    return $this->httpClient->request($methodPlugin);
265
  }
266
267
  /**
268
   * Call Random.org API.
269
   *
270
   * @param string $method
271
   *   The method to call.
272
   * @param array $params
273
   *   The associative array of params as defined in the Random.org API.
274
   *
275
   * @return self
276
   *   Returns itself.
277
   */
278 16
  public function call($method, array $params = array()) {
279 16
    $this->setResponse(NULL);
280 16
    $this->setMethodPlugin(NULL);
281
282 16
    $params += ['apiKey' => $this->getApiKey()];
283
284 16
    if ($plugin = $this->getMethodPluginManager()->getPlugin($method, $params)) {
285 14
      $this->setMethodPlugin($plugin)->setResponse(
0 ignored issues
show
Bug introduced by
It seems like $plugin defined by $this->getMethodPluginMa...lugin($method, $params) on line 284 can also be of type boolean; however, drupol\Yaroc\RandomOrgAPI::setMethodPlugin() does only seem to accept null|object<drupol\Yaroc...\MethodPluginInterface>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
286 14
        $this->request(
0 ignored issues
show
Bug introduced by
It seems like $this->request($this->ge...this->getApiVersion())) targeting drupol\Yaroc\RandomOrgAPI::request() can also be of type object<Exception>; however, drupol\Yaroc\RandomOrgAPI::setResponse() does only seem to accept null|object<Psr\Http\Message\ResponseInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
287 14
          $this->getMethodPlugin()->setApiVersion($this->getApiVersion()
288
          )
289
        )
290
      );
291
    }
292
293 15
    return $this;
294
  }
295
296
  /**
297
   * Get the result array from the response.
298
   *
299
   * @param null|string $key
300
   *   The key you want to get.
301
   *
302
   * @return array|bool
303
   *   The result array, FALSE otherwise.
304
   */
305 12
  public function get($key = NULL) {
306 12
    if ($this->getResponse() && $this->getMethodPlugin()) {
307 12
      if ($result = $this->getMethodPlugin()->get($this->getResponse(), $key)) {
0 ignored issues
show
Bug introduced by
It seems like $this->getResponse() targeting drupol\Yaroc\RandomOrgAPI::getResponse() can also be of type boolean; however, drupol\Yaroc\Plugin\MethodPluginInterface::get() does only seem to accept object<Psr\Http\Message\ResponseInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
308 12
        return $result;
309
      }
310
    }
311
312 12
    return FALSE;
313
  }
314
315
  /**
316
   * Get the result array from the response.
317
   *
318
   * @param null|string $key
319
   *   The key you want to get.
320
   *
321
   * @return array|bool
322
   *   The result array, FALSE otherwise.
323
   */
324 14
  public function getFromResult($key = NULL) {
325 14
    if ($result = $this->get('result')) {
326 13
      if (!is_null($key) && isset($result[$key])) {
327
        return $result[$key];
328
      }
329 13
      if (is_null($key)) {
330 13
        return $result;
331
      }
332
    }
333
334 13
    return FALSE;
335
  }
336
337
  /**
338
   * Get the data from the result array.
339
   *
340
   * @return array|bool
341
   *   The data array, FALSE otherwise.
342
   */
343 12
  public function getData() {
344 12
    if ($result = $this->get('result')) {
345 12
      if (isset($result['random']) && isset($result['random']['data'])) {
346 12
        return $result['random']['data'];
347
      }
348
    }
349
350
    return FALSE;
351
  }
352
353
}
354