Completed
Push — master ( 470997...794ec6 )
by Andrew
02:51 queued 11s
created

Instance::makeRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 8
cts 10
cp 0.8
rs 9.4285
cc 2
eloc 9
nc 2
nop 4
crap 2.032
1
<?php
2
3
namespace NodeRED;
4
5
use GuzzleHttp\Client;
6
7
class Instance
8
{
9
    private $endpoint;
10
    private $client;
11
12 3
    public function __construct($endpoint, Client $client = null)
13
    {
14 3
        if (null === $client) {
15
            $client = new Client();
16
        }
17
18 3
        $this->endpoint = $endpoint;
19 3
        $this->client = $client;
20 3
    }
21
22 2
    public function get($path, $token = null)
23
    {
24 2
        return $this->makeRequest('GET', $path, [], $token);
25
    }
26
27 1
    public function formPost($path, $data, $token = null)
28
    {
29 1
        return $this->makeRequest('POST', $path, ['form_params' => $data], $token);
30
    }
31
32
    public function jsonPost($path, $data, $token = null)
33
    {
34
        return $this->makeRequest('POST', $path, ['json' => $data], $token);
35
    }
36
37 3
    private function makeRequest($method, $path, $extraOptions = [], $token = null)
38
    {
39
        $options = [
40
            'headers' => [
41 3
                'Node-RED-API-Version' => 'v2',
42 3
            ],
43 3
            'http_errors' => false,
44 3
        ];
45
46 3
        if (null !== $token) {
47
            $options['headers']['Authorization'] = $token->getAuthorizationString();
48
        }
49
50 3
        $response = $this->client->request($method, $this->endpoint . $path, array_merge($options, $extraOptions));
51
52 3
        return $this->processResponse($response);
0 ignored issues
show
Documentation introduced by
$response is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<NodeRED\ResponseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
    }
54
55
    private function processResponse(ResponseInterface $response)
56
    {
57
        $statusCode = $response->getStatusCode();
58
59
        if ($statusCode < 200 || $statusCode >= 300) {
60
            throw ApiException::fromResponse($response);
61
        }
62
63
        $json = json_decode($response->getBody(), true);
64
65
        if (NULL === $json) {
66
            throw new ApiException('Could not get JSON from response', 0, $response);
67
        }
68
69
        return $json;
70
    }
71
}
72