Completed
Push — master ( 4e2f07...9d647b )
by Pol
02:55
created

RandomOrgAPI::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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 13. 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 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() {
123 13
    return $this->methodPluginManager;
124
  }
125
126
  /**
127
   * Set the client request.
128
   *
129
   * @param null|HttpClient $httpClient
130
   *   The client request.
131
   * @param UriFactory $uriFactory
0 ignored issues
show
Documentation introduced by
Should the type for parameter $uriFactory not be null|UriFactory?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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));
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...
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
   * 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()) {
159
    return $this;
160
  }
161
162
  /**
163
   * Get the Http client.
164
   *
165
   * @return Client
166
   */
167 16
  public function getHttpClient() {
168 16
    return $this->httpClient;
169
  }
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) {
180 16
    $this->apiKey = $key;
181
182 16
    return $this;
183
  }
184
185
  /**
186
   * Get the Random.org API Key.
187
   *
188
   * @return string
189
   *   The API Key.
190
   */
191 13
  public function getApiKey() {
192 13
    return $this->apiKey;
193
  }
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) {
204 16
    $this->apiVersion = $version;
205 16
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
206
207 16
    return $this;
208
  }
209
210
  /**
211
   * Get the API version.
212
   *
213
   * @return int
214
   */
215 16
  public function getApiVersion() {
216 16
    return $this->apiVersion;
217
  }
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) {
228 13
    $this->methodPlugin = $methodPlugin;
229
230 13
    return $methodPlugin ? $this : FALSE;
231
  }
232
233
  /**
234
   * Get the method plugin.
235
   *
236
   * @return \drupol\Yaroc\Plugin\MethodPluginInterface
237
   */
238 12
  private function getMethodPlugin() {
239 12
    return $this->methodPlugin;
240
  }
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) {
250 13
    $this->response = $response;
251
252 13
    return $this;
253
  }
254
255
  /**
256
   * Get the response.
257
   *
258
   * @return bool|\Psr\Http\Message\ResponseInterface
259
   */
260 14
  public function getResponse() {
261 14
    return $this->response;
262
  }
263
264
  /**
265
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin
266
   *
267
   * @return ResponseInterface|\Exception
268
   */
269 13
  private function request(MethodPluginInterface $methodPlugin) {
270 13
    return $this->httpClient->request($methodPlugin);
271
  }
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()) {
285 16
    $this->setResponse(NULL);
286 16
    $this->setMethodPlugin(NULL);
287
288 16
    $params += ['apiKey' => $this->getApiKey()];
289
290 16
    if ($plugin = $this->getMethodPluginManager()->getPlugin($method, $params)) {
291 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 290 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...
292 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...
293 14
          $this->getMethodPlugin()->setApiVersion($this->getApiVersion()
294 14
          )
295 14
        )
296 13
      );
297 13
    }
298
299 15
    return $this;
300
  }
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() {
309 14
    if ($this->getResponse() && $this->getMethodPlugin()) {
310 13
      return $this->getMethodPlugin()->getResult($this->getResponse());
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\Meth...nInterface::getResult() 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...
311
    }
312
313 1
    return FALSE;
314
  }
315
316
}
317