Passed
Push — master ( dc4f1f...6b2c4c )
by Gabriel
02:50
created

RequestHandler::setApiUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Waredesk;
4
5
use GuzzleHttp\Psr7\Request as GuzzleRequest;
6
use GuzzleHttp\Client;
7
use Psr\Http\Message\ResponseInterface;
8
9
class RequestHandler
10
{
11
    private $accessToken;
12
    private $apiUrl;
13
    private $client;
14
15
    public function __construct(string $accessToken = null, string $apiUrl = null)
16
    {
17
        $this->accessToken = $accessToken;
18
        $this->apiUrl = $apiUrl;
19
        $this->client = new Client(['base_uri' => $this->apiUrl . '/']);
20
    }
21
22
    public function getAccessToken(): string
23
    {
24
        return $this->accessToken;
25
    }
26
27
    public function setAccessToken(string $accessToken)
28
    {
29
        $this->accessToken = $accessToken;
30
    }
31
32
    public function getApiUrl(): string
33
    {
34
        return $this->apiUrl;
35
    }
36
37
    public function setApiUrl(string $apiUrl)
38
    {
39
        $this->apiUrl = $apiUrl;
40
        $this->client = new Client(['base_uri' => $apiUrl . '/']);
41
    }
42
43
    private function handleException(ResponseInterface $response)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        die('beeeurkk');
46
    }
47
48
    public function get(string $endpoint, array $headers = [], array $params = null)
0 ignored issues
show
Unused Code introduced by
The parameter $endpoint is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $headers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $request = new GuzzleRequest('GET', 'http://httpbin.org/get');
0 ignored issues
show
Unused Code introduced by
$request is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
    }
52
53
    public function post(string $endpoint, array $params = null, array $headers = []): array
54
    {
55
        $headers['Content-Type'] = 'application/json';
56
        $body = null;
57
        if ($params) $body = \GuzzleHttp\json_encode($params);
58
        $request = new GuzzleRequest('POST', $endpoint, $headers, $body);
59
        $response = $this->client->send($request);
60
61
        if ($response->getStatusCode() >= 200 && $response->getStatusCode() <= 399) {
62
            return \GuzzleHttp\json_decode((string)$response->getBody(), true);
63
        } else {
64
            $this->handleException($response);
65
        }
66
    }
67
}
68