Completed
Push — master ( c89762...e8d187 )
by Pol
03:02 queued 15s
created

RandomOrgAPI::getHttpClient()   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 0
crap 1
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 2
  public function setEndpoint($uri) {
94 2
    $this->endpoint = $uri;
95 2
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
96 2
  }
97
98
  /**
99
   * Get the Random.org endpoint.
100
   *
101
   * @return string
102
   */
103 17
  public function getEndpoint() {
104 17
    return sprintf($this->endpoint, $this->getApiVersion());
105
  }
106
107
  /**
108
   * Set the Method plugin manager.
109
   *
110
   * @param MethodPluginManager $methodPluginManager
111
   */
112 16
  public function setMethodPluginManager(MethodPluginManager $methodPluginManager) {
113 16
    $this->methodPluginManager = $methodPluginManager;
114 16
  }
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
   * Get the logger.
127
   *
128
   * @return \Psr\Log\LoggerInterface
129
   */
130 15
  public function getLogger() {
131 15
    return $this->logger;
132
  }
133
134
  /**
135
   * Set the logger.
136
   *
137
   * @param null|\Psr\Log\LoggerInterface $logger
138
   */
139 15
  public function setLogger(LoggerInterface $logger = NULL) {
140 15
    $this->logger = $logger ?: new Logger();
141 15
  }
142
143
  /**
144
   * Set the client request.
145
   *
146
   * @param null|HttpClient $httpClient
147
   *   The client request.
148
   */
149 16
  public function setHttpClient(HttpClient $httpClient = NULL) {
150 16
    $this->httpClient = new Client($httpClient, NULL, $this->getLogger());
151 16
  }
152
153
  /**
154
   * Get the Http client.
155
   *
156
   * @return Client
157
   */
158 16
  public function getHttpClient() {
159 16
    return $this->httpClient;
160
  }
161
162
  /**
163
   * Set the Random.org API Key.
164
   *
165
   * @param string $key
166
   *   The API Key.
167
   */
168 16
  public function setApiKey($key) {
169 16
    $this->apiKey = $key;
170 16
  }
171
172
  /**
173
   * Get the Random.org API Key.
174
   *
175
   * @return string
176
   *   The API Key.
177
   */
178 13
  public function getApiKey() {
179 13
    return $this->apiKey;
180
  }
181
182
  /**
183
   * Set the API version.
184
   *
185
   * @param int
186
   *   The API version.
187
   */
188 16
  public function setApiVersion($version) {
189 16
    $this->apiVersion = $version;
190 16
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
191 16
  }
192
193
  /**
194
   * Get the API version.
195
   *
196
   * @return int
197
   */
198 16
  public function getApiVersion() {
199 16
    return $this->apiVersion;
200
  }
201
202
203
  /**
204
   * Set the method plugin.
205
   *
206
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin
207
   */
208 13
  public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) {
209 13
    $this->methodPlugin = $methodPlugin;
210 13
  }
211
212
  /**
213
   * Get the method plugin.
214
   *
215
   * @return \drupol\Yaroc\Plugin\MethodPluginInterface
216
   */
217 12
  private function getMethodPlugin() {
218 12
    return $this->methodPlugin;
219
  }
220
221
  /**
222
   * Set the response.
223
   *
224
   * @param \Psr\Http\Message\ResponseInterface|NULL $response
225
   */
226 13
  private function setResponse(ResponseInterface $response = NULL) {
227 13
    $this->response = $response;
228 13
  }
229
230
  /**
231
   * Get the response.
232
   *
233
   * @return bool|\Psr\Http\Message\ResponseInterface
234
   */
235 14
  public function getResponse() {
236 14
    return $this->response;
237
  }
238
239
  /**
240
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin
241
   *
242
   * @return ResponseInterface|\Exception
243
   */
244 13
  private function request(MethodPluginInterface $methodPlugin) {
245 13
    return $this->httpClient->request($methodPlugin);
246
  }
247
248
  /**
249
   * Call Random.org API.
250
   *
251
   * @param string $method
252
   *   The method to call.
253
   * @param array $parameters
254
   *   The associative array of parameters as defined in the Random.org API.
255
   *
256
   * @return self
257
   *   Returns itself.
258
   */
259 16
  public function call($method, array $parameters = array()) {
260 16
    if ($methodPlugin = $this->getMethodPluginManager()->getPlugin($method)) {
261 14
      $this->setMethodPlugin($methodPlugin);
0 ignored issues
show
Bug introduced by
It seems like $methodPlugin defined by $this->getMethodPluginMa...r()->getPlugin($method) on line 260 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...
262 14
      $this->getMethodPlugin()->setApiVersion($this->getApiVersion());
263 14
      $parameters += ['apiKey' => $this->getApiKey()];
264 14
      $this->getMethodPlugin()->setParameters($parameters);
265
266 14
      $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...
267
268 13
      return $this;
269
    }
270
271 2
    $this->setResponse(NULL);
272 2
    $this->setMethodPlugin(NULL);
273
274 2
    return $this;
275
  }
276
277
  /**
278
   * Get the result array from the response.
279
   *
280
   * @return array|bool
281
   *   The result array, FALSE otherwise.
282
   */
283 14
  public function getResult() {
284 14
    if ($this->getResponse() && $this->getMethodPlugin()) {
285 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...
286
    }
287
288 1
    return FALSE;
289
  }
290
291
292
}
293