Completed
Push — master ( 01e022...1bca2a )
by Pol
19:58 queued 17:22
created

RandomOrgAPI::setLogger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
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
   *   The URI.
93
   *
94
   * @return self
95
   */
96 2
  public function setEndpoint($uri) {
97 2
    $this->endpoint = $uri;
98 2
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
99
100 2
    return $this;
101
  }
102
103
  /**
104
   * Get the Random.org endpoint.
105
   *
106
   * @return string
107
   */
108 17
  public function getEndpoint() {
109 17
    return sprintf($this->endpoint, $this->getApiVersion());
110
  }
111
112
  /**
113
   * Set the Method plugin manager.
114
   *
115
   * @param MethodPluginManager $methodPluginManager
116
   *   The method plugin manager.
117
   *
118
   * @return self
119
   */
120 16
  public function setMethodPluginManager(MethodPluginManager $methodPluginManager) {
121 16
    $this->methodPluginManager = $methodPluginManager;
122
123 16
    return $this;
124
  }
125
126
  /**
127
   * Return the Method plugin manager.
128
   *
129
   * @return \drupol\Yaroc\Plugin\MethodPluginManager
130
   */
131 13
  public function getMethodPluginManager() {
132 13
    return $this->methodPluginManager;
133
  }
134
135
  /**
136
   * Get the logger.
137
   *
138
   * @return \Psr\Log\LoggerInterface
139
   */
140 15
  public function getLogger() {
141 15
    return $this->logger;
142
  }
143
144
  /**
145
   * Set the logger.
146
   *
147
   * @param null|\Psr\Log\LoggerInterface $logger
148
   *   The logger.
149
   *
150
   * @return self
151
   */
152 15
  public function setLogger(LoggerInterface $logger = NULL) {
153 15
    $this->logger = $logger ?: new Logger();
154
155 15
    return $this;
156
  }
157
158
  /**
159
   * Set the client request.
160
   *
161
   * @param null|HttpClient $httpClient
162
   *   The client request.
163
   *
164
   * @return self
165
   */
166 16
  public function setHttpClient(HttpClient $httpClient = NULL) {
167 16
    $this->httpClient = new Client($httpClient, NULL, $this->getLogger());
168
169 16
    return $this;
170
  }
171
172
  /**
173
   * Get the Http client.
174
   *
175
   * @return Client
176
   */
177 16
  public function getHttpClient() {
178 16
    return $this->httpClient;
179
  }
180
181
  /**
182
   * Set the Random.org API Key.
183
   *
184
   * @param string $key
185
   *   The API Key.
186
   *
187
   * @return self
188
   */
189 16
  public function setApiKey($key) {
190 16
    $this->apiKey = $key;
191
192 16
    return $this;
193
  }
194
195
  /**
196
   * Get the Random.org API Key.
197
   *
198
   * @return string
199
   *   The API Key.
200
   */
201 13
  public function getApiKey() {
202 13
    return $this->apiKey;
203
  }
204
205
  /**
206
   * Set the API version.
207
   *
208
   * @param int
209
   *   The API version.
210
   *
211
   * @return self
212
   */
213 16
  public function setApiVersion($version) {
214 16
    $this->apiVersion = $version;
215 16
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
216
217 16
    return $this;
218
  }
219
220
  /**
221
   * Get the API version.
222
   *
223
   * @return int
224
   */
225 16
  public function getApiVersion() {
226 16
    return $this->apiVersion;
227
  }
228
229
  /**
230
   * Set the method plugin.
231
   *
232
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin
233
   *
234
   * @return False|self
235
   *   Return itself, or FALSE otherwise.
236
   */
237 13
  public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) {
238 13
    $this->methodPlugin = $methodPlugin;
239
240 13
    return $methodPlugin ? $this : FALSE;
241
  }
242
243
  /**
244
   * Get the method plugin.
245
   *
246
   * @return \drupol\Yaroc\Plugin\MethodPluginInterface
247
   */
248 12
  private function getMethodPlugin() {
249 12
    return $this->methodPlugin;
250
  }
251
252
  /**
253
   * Set the response.
254
   *
255
   * @param \Psr\Http\Message\ResponseInterface|NULL $response
256
   *
257
   * @return self
258
   */
259 13
  private function setResponse(ResponseInterface $response = NULL) {
260 13
    $this->response = $response;
261
262 13
    return $this;
263
  }
264
265
  /**
266
   * Get the response.
267
   *
268
   * @return bool|\Psr\Http\Message\ResponseInterface
269
   */
270 14
  public function getResponse() {
271 14
    return $this->response;
272
  }
273
274
  /**
275
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin
276
   *
277
   * @return ResponseInterface|\Exception
278
   */
279 13
  private function request(MethodPluginInterface $methodPlugin) {
280 13
    return $this->httpClient->request($methodPlugin);
281
  }
282
283
  /**
284
   * Call Random.org API.
285
   *
286
   * @param string $method
287
   *   The method to call.
288
   * @param array $params
289
   *   The associative array of params as defined in the Random.org API.
290
   *
291
   * @return self
292
   *   Returns itself.
293
   */
294 16
  public function call($method, array $params = array()) {
295 16
    $this->setResponse(NULL);
296 16
    $this->setMethodPlugin(NULL);
297
298 16
    $params += ['apiKey' => $this->getApiKey()];
299
300 16
    if ($plugin = $this->getMethodPluginManager()->getPlugin($method, $params)) {
301 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 300 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...
302 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...
303 14
          $this->getMethodPlugin()->setApiVersion($this->getApiVersion()
304
          )
305
        )
306
      );
307
    }
308
309 15
    return $this;
310
  }
311
312
  /**
313
   * Get the result array from the response.
314
   *
315
   * @return array|bool
316
   *   The result array, FALSE otherwise.
317
   */
318 14
  public function getResult() {
319 14
    if ($this->getResponse() && $this->getMethodPlugin()) {
320 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...
321
    }
322
323 1
    return FALSE;
324
  }
325
326
}
327