Completed
Push — master ( 73ec26...13e542 )
by Pol
02:41
created

RandomOrgAPI::getResult()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 0
crap 3.1406
1
<?php
2
3
namespace drupol\Yaroc;
4
5
use drupol\Yaroc\Http\Client;
6
use drupol\Yaroc\Log\Logger;
7
use drupol\Yaroc\Plugin\MethodPluginInterface;
8
use drupol\Yaroc\Plugin\MethodPluginManager;
9
use Http\Client\HttpClient;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Log\LoggerInterface;
12
13
/**
14
 * Class RandomOrgAPIClient
15
 *
16
 * @package drupol\Yaroc
17
 */
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 logger.
43
   *
44
   * @var \Psr\Log\LoggerInterface
45
   */
46
  protected $logger;
47
48
  /**
49
   * The Method plugin manager.
50
   *
51
   * @var MethodPluginManager
52
   */
53
  protected $methodPluginManager;
54
55
  /**
56
   * The Random.org API version.
57
   *
58
   * @var int
59
   */
60
  protected $apiVersion = 1;
61
62
  /**
63
   * The response if any.
64
   *
65
   * @var bool|ResponseInterface
66
   */
67
  protected $response = FALSE;
68
69
  /**
70
   * @var MethodPluginInterface
71
   */
72
  protected $methodPlugin = FALSE;
73
74
  /**
75
   * RandomOrgAPI constructor.
76
   *
77
   * @param null|\Http\Client\HttpClient $httpClient
78
   * @param null|\Psr\Log\LoggerInterface $logger
79
   */
80 16
  public function __construct(HttpClient $httpClient = NULL, LoggerInterface $logger = NULL) {
81 16
    $this->setLogger($logger);
82 16
    $this->setHttpClient($httpClient);
83 16
    $this->setMethodPluginManager(new MethodPluginManager());
84 16
    $this->setApiVersion($this->getApiVersion());
85 16
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
86 16
  }
87
88
  /**
89
   * Set the Random.org endpoint template.
90
   *
91
   * @param string $uri
92
   */
93 1
  public function setEndpoint($uri) {
94 1
    $this->endpoint = $uri;
95 1
  }
96
97
  /**
98
   * Get the Random.org endpoint.
99
   *
100
   * @return string
101
   */
102 16
  public function getEndpoint() {
103 16
    return sprintf($this->endpoint, $this->getApiVersion());
104
  }
105
106
  /**
107
   * Set the Method plugin manager.
108
   *
109
   * @param MethodPluginManager $methodPluginManager
110
   */
111 16
  public function setMethodPluginManager(MethodPluginManager $methodPluginManager) {
112 16
    $this->methodPluginManager = $methodPluginManager;
113 16
  }
114
115
  /**
116
   * Return the Method plugin manager.
117
   *
118
   * @return \drupol\Yaroc\Plugin\MethodPluginManager
119
   */
120 13
  public function getMethodPluginManager() {
121 13
    return $this->methodPluginManager;
122
  }
123
124
  /**
125
   * Get the logger.
126
   *
127
   * @return \Psr\Log\LoggerInterface
128
   */
129 15
  public function getLogger() {
130 15
    return $this->logger;
131
  }
132
133
  /**
134
   * Set the logger.
135
   *
136
   * @param null|\Psr\Log\LoggerInterface $logger
137
   */
138 15
  public function setLogger(LoggerInterface $logger = NULL) {
139 15
    $this->logger = $logger ?: new Logger();
140 15
  }
141
142
  /**
143
   * Set the client request.
144
   *
145
   * @param null|HttpClient $httpClient
146
   *   The client request.
147
   */
148 16
  public function setHttpClient(HttpClient $httpClient = NULL) {
149 16
    $this->httpClient = new Client($httpClient, NULL, $this->getLogger());
150 16
  }
151
152
  /**
153
   * Get the Http client.
154
   *
155
   * @return Client
156
   */
157 16
  public function getHttpClient() {
158 16
    return $this->httpClient;
159
  }
160
161
  /**
162
   * Set the Random.org API Key.
163
   *
164
   * @param string $key
165
   *   The API Key.
166
   */
167 16
  public function setApiKey($key) {
168 16
    $this->apiKey = $key;
169 16
  }
170
171
  /**
172
   * Get the Random.org API Key.
173
   *
174
   * @return string
175
   *   The API Key.
176
   */
177 13
  public function getApiKey() {
178 13
    return $this->apiKey;
179
  }
180
181
  /**
182
   * Set the API version.
183
   *
184
   * @param int
185
   *   The API version.
186
   */
187 16
  public function setApiVersion($version) {
188 16
    $this->apiVersion = $version;
189 16
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
190 16
  }
191
192
  /**
193
   * Get the API version.
194
   *
195
   * @return int
196
   */
197 16
  public function getApiVersion() {
198 16
    return $this->apiVersion;
199
  }
200
201
202
  /**
203
   * Set the method plugin.
204
   *
205
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin
206
   */
207 13
  public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) {
208 13
    $this->methodPlugin = $methodPlugin;
209 13
  }
210
211
  /**
212
   * Get the method plugin.
213
   *
214
   * @return \drupol\Yaroc\Plugin\MethodPluginInterface
215
   */
216 12
  private function getMethodPlugin() {
217 12
    return $this->methodPlugin;
218
  }
219
220
  /**
221
   * Set the response.
222
   *
223
   * @param \Psr\Http\Message\ResponseInterface|NULL $response
224
   */
225 13
  private function setResponse(ResponseInterface $response = NULL) {
226 13
    $this->response = $response;
227 13
  }
228
229
  /**
230
   * Get the response.
231
   *
232
   * @return bool|\Psr\Http\Message\ResponseInterface
233
   */
234 13
  public function getResponse() {
235 13
    return $this->response;
236
  }
237
238
  /**
239
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin
240
   *
241
   * @return ResponseInterface|\Exception
242
   */
243 12
  private function request(MethodPluginInterface $methodPlugin) {
244 12
    return $this->httpClient->request($methodPlugin);
245
  }
246
247
  /**
248
   * Call Random.org API.
249
   *
250
   * @param string $method
251
   *   The method to call.
252
   * @param array $parameters
253
   *   The associative array of parameters as defined in the Random.org API.
254
   *
255
   * @return self
256
   *   Returns itself.
257
   */
258 15
  public function call($method, array $parameters = array()) {
259 15
    if ($methodPlugin = $this->getMethodPluginManager()->getPlugin($method)) {
260 13
      $this->setMethodPlugin($methodPlugin);
0 ignored issues
show
Bug introduced by
It seems like $methodPlugin defined by $this->getMethodPluginMa...r()->getPlugin($method) on line 259 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...
261 13
      $this->getMethodPlugin()->setApiVersion($this->getApiVersion());
262 13
      $parameters += ['apiKey' => $this->getApiKey()];
263 13
      $this->getMethodPlugin()->setParameters($parameters);
264
265 13
      $this->setResponse($this->request($this->getMethodPlugin()));
0 ignored issues
show
Bug introduced by
It seems like $this->request($this->getMethodPlugin()) 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...
266
267 12
      return $this;
268
    }
269
270 2
    $this->setResponse(NULL);
271 2
    $this->setMethodPlugin(NULL);
272
273 2
    return $this;
274
  }
275
276
  /**
277
   * Get the result array from the response.
278
   *
279
   * @return array|bool
280
   *   The result array, FALSE otherwise.
281
   */
282 13
  public function getResult() {
283 13
    if ($this->getResponse() && $this->getMethodPlugin()) {
284 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...
285
    }
286
287
    return FALSE;
288
  }
289
290
291
}
292