Completed
Push — master ( 37e60c...4e2f07 )
by Pol
05:19
created

RandomOrgAPI::getApiVersion()   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\Plugin\MethodPluginInterface;
7
use drupol\Yaroc\Plugin\MethodPluginManager;
8
use Http\Client\HttpClient;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Class RandomOrgAPIClient
13
 *
14
 * @package drupol\Yaroc
15
 */
16
class RandomOrgAPI {
17
18
  /**
19
   * The default Random.org endpoint template.
20
   *
21
   * @var string;
22
   */
23
  protected $endpoint = 'https://api.random.org/json-rpc/%s/invoke';
24
25
  /**
26
   * The Random.org api key.
27
   *
28
   * @var string
29
   */
30
  protected $apiKey = '';
31
32
  /**
33
   * The HTTP client.
34
   *
35
   * @var Client
36
   */
37
  protected $httpClient;
38
39
  /**
40
   * The Method plugin manager.
41
   *
42
   * @var MethodPluginManager
43
   */
44
  protected $methodPluginManager;
45
46
  /**
47
   * The Random.org API version.
48
   *
49
   * @var int
50
   */
51
  protected $apiVersion = 1;
52
53
  /**
54
   * The response if any.
55
   *
56
   * @var bool|ResponseInterface
57
   */
58
  protected $response = FALSE;
59
60
  /**
61
   * @var MethodPluginInterface
62
   */
63
  protected $methodPlugin = FALSE;
64
65
  /**
66
   * RandomOrgAPI constructor.
67
   *
68
   * @param null|\Http\Client\HttpClient $httpClient
69
   *   The HTTP client.
70
   */
71 16
  public function __construct(HttpClient $httpClient = NULL) {
72 16
    $this->setHttpClient($httpClient);
73 16
    $this->setMethodPluginManager(new MethodPluginManager());
74 16
    $this->setApiVersion($this->getApiVersion());
75 16
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
76 16
  }
77
78
  /**
79
   * Set the Random.org endpoint template.
80
   *
81
   * @param string $uri
82
   *   The URI.
83
   *
84
   * @return self
85
   */
86 2
  public function setEndpoint($uri) {
87 2
    $this->endpoint = $uri;
88 2
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
89
90 2
    return $this;
91
  }
92
93
  /**
94
   * Get the Random.org endpoint.
95
   *
96
   * @return string
97
   */
98 17
  public function getEndpoint() {
99 17
    return sprintf($this->endpoint, $this->getApiVersion());
100
  }
101
102
  /**
103
   * Set the Method plugin manager.
104
   *
105
   * @param MethodPluginManager $methodPluginManager
106
   *   The method plugin manager.
107
   *
108
   * @return self
109
   */
110 16
  public function setMethodPluginManager(MethodPluginManager $methodPluginManager) {
111 16
    $this->methodPluginManager = $methodPluginManager;
112
113 16
    return $this;
114
  }
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
   * Set the client request.
127
   *
128
   * @param null|HttpClient $httpClient
129
   *   The client request.
130
   *
131
   * @return self
132
   */
133 16
  public function setHttpClient(HttpClient $httpClient = NULL) {
134 16
    $this->httpClient = new Client($httpClient, NULL, NULL);
0 ignored issues
show
Unused Code introduced by
The call to Client::__construct() has too many arguments starting with NULL.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
135
136 16
    return $this;
137
  }
138
139
  /**
140
   * Get the Http client.
141
   *
142
   * @return Client
143
   */
144 16
  public function getHttpClient() {
145 16
    return $this->httpClient;
146
  }
147
148
  /**
149
   * Set the Random.org API Key.
150
   *
151
   * @param string $key
152
   *   The API Key.
153
   *
154
   * @return self
155
   */
156 16
  public function setApiKey($key) {
157 16
    $this->apiKey = $key;
158
159 16
    return $this;
160
  }
161
162
  /**
163
   * Get the Random.org API Key.
164
   *
165
   * @return string
166
   *   The API Key.
167
   */
168 13
  public function getApiKey() {
169 13
    return $this->apiKey;
170
  }
171
172
  /**
173
   * Set the API version.
174
   *
175
   * @param int
176
   *   The API version.
177
   *
178
   * @return self
179
   */
180 16
  public function setApiVersion($version) {
181 16
    $this->apiVersion = $version;
182 16
    $this->getHttpClient()->setEndpoint($this->getEndpoint());
183
184 16
    return $this;
185
  }
186
187
  /**
188
   * Get the API version.
189
   *
190
   * @return int
191
   */
192 16
  public function getApiVersion() {
193 16
    return $this->apiVersion;
194
  }
195
196
  /**
197
   * Set the method plugin.
198
   *
199
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface|NULL $methodPlugin
200
   *
201
   * @return False|self
202
   *   Return itself, or FALSE otherwise.
203
   */
204 13
  public function setMethodPlugin(MethodPluginInterface $methodPlugin = NULL) {
205 13
    $this->methodPlugin = $methodPlugin;
206
207 13
    return $methodPlugin ? $this : FALSE;
208
  }
209
210
  /**
211
   * Get the method plugin.
212
   *
213
   * @return \drupol\Yaroc\Plugin\MethodPluginInterface
214
   */
215 12
  private function getMethodPlugin() {
216 12
    return $this->methodPlugin;
217
  }
218
219
  /**
220
   * Set the response.
221
   *
222
   * @param \Psr\Http\Message\ResponseInterface|NULL $response
223
   *
224
   * @return self
225
   */
226 13
  private function setResponse(ResponseInterface $response = NULL) {
227 13
    $this->response = $response;
228
229 13
    return $this;
230
  }
231
232
  /**
233
   * Get the response.
234
   *
235
   * @return bool|\Psr\Http\Message\ResponseInterface
236
   */
237 14
  public function getResponse() {
238 14
    return $this->response;
239
  }
240
241
  /**
242
   * @param \drupol\Yaroc\Plugin\MethodPluginInterface $methodPlugin
243
   *
244
   * @return ResponseInterface|\Exception
245
   */
246 13
  private function request(MethodPluginInterface $methodPlugin) {
247 13
    return $this->httpClient->request($methodPlugin);
248
  }
249
250
  /**
251
   * Call Random.org API.
252
   *
253
   * @param string $method
254
   *   The method to call.
255
   * @param array $params
256
   *   The associative array of params as defined in the Random.org API.
257
   *
258
   * @return self
259
   *   Returns itself.
260
   */
261 16
  public function call($method, array $params = array()) {
262 16
    $this->setResponse(NULL);
263 16
    $this->setMethodPlugin(NULL);
264
265 16
    $params += ['apiKey' => $this->getApiKey()];
266
267 16
    if ($plugin = $this->getMethodPluginManager()->getPlugin($method, $params)) {
268 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 267 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...
269 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...
270 14
          $this->getMethodPlugin()->setApiVersion($this->getApiVersion()
271
          )
272
        )
273
      );
274
    }
275
276 15
    return $this;
277
  }
278
279
  /**
280
   * Get the result array from the response.
281
   *
282
   * @return array|bool
283
   *   The result array, FALSE otherwise.
284
   */
285 14
  public function getResult() {
286 14
    if ($this->getResponse() && $this->getMethodPlugin()) {
287 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...
288
    }
289
290 1
    return FALSE;
291
  }
292
293
}
294