Passed
Push — master ( da6516...18010c )
by Gabriel
02:39
created

RequestHandler::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Waredesk;
4
5
use GuzzleHttp\Exception\ClientException;
6
use GuzzleHttp\HandlerStack;
7
use GuzzleHttp\Psr7\Request as GuzzleRequest;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Psr7\Request;
10
use Psr\Http\Message\ResponseInterface;
11
use Waredesk\Exceptions\UnknownException;
12
13
class RequestHandler
14
{
15
    private $mockHandler;
16
    private $accessToken;
17
    private $apiUrl;
18
    private $client;
19
20 7
    public function __construct(string $accessToken = null, string $apiUrl = null)
21
    {
22 7
        $this->accessToken = $accessToken;
23 7
        $this->apiUrl = $apiUrl;
24 7
        $this->client = new Client(['base_uri' => $this->apiUrl, 'handler' => $this->mockHandler]);
25 7
    }
26
27
    public function getAccessToken(): string
28
    {
29
        return $this->accessToken;
30
    }
31
32 7
    public function setAccessToken(string $accessToken = null)
33
    {
34 7
        $this->accessToken = $accessToken;
35 7
    }
36
37
    public function getApiUrl(): string
38
    {
39
        return $this->apiUrl;
40
    }
41
42 7
    public function setApiUrl(string $apiUrl)
43
    {
44 7
        $this->apiUrl = $apiUrl;
45 7
        $this->client = new Client(['base_uri' => $this->apiUrl, 'handler' => $this->mockHandler]);
46 7
    }
47
48 7
    public function setMockHandler(HandlerStack $mockHandler = null)
49
    {
50 7
        $this->mockHandler = $mockHandler;
51 7
        $this->client = new Client(['base_uri' => $this->apiUrl, 'handler' => $this->mockHandler]);
52 7
    }
53
54 1
    private function handleBadResponse(ResponseInterface $response = null)
55
    {
56 1
        if ($response) {
57 1
            $body = (string)$response->getBody();
58 1
            $json = \GuzzleHttp\json_decode($body, true);
59 1
            (new ErrorHandler())($json);
60
            return;
61
        }
62
        throw new UnknownException();
63
    }
64
65 1
    private function handleException(ClientException $exception)
66
    {
67 1
        $this->handleBadResponse($exception->getResponse());
68
    }
69
70 4
    private function enhanceHeaders(array $headers = []): array
71
    {
72 4
        $headers['Content-Type'] = 'application/json';
73 4
        if ($this->accessToken !== null) {
74 3
            $headers['Authorization'] = 'Bearer '.$this->accessToken;
75
        }
76 4
        return $headers;
77
    }
78
79 4
    private function encodeParams($params = null): ?string
80
    {
81 4
        $body = null;
82 4
        if ($params) {
83 3
            $body = \GuzzleHttp\json_encode($params);
84
        }
85 4
        return $body;
86
    }
87
88 4
    private function request(Request $request): array
89
    {
90
        try {
91 4
            $response = $this->client->send($request);
92 3
            if ($response->getStatusCode() >= 200 && $response->getStatusCode() <= 399) {
93 3
                return \GuzzleHttp\json_decode((string)$response->getBody(), true);
94
            }
95
            $this->handleBadResponse($response);
96 1
        } catch (ClientException $e) {
97 1
            $this->handleException($e);
98
        }
99
        throw new UnknownException();
100
    }
101
102 1
    public function get(string $endpoint, array $headers = [], array $params = null): array
103
    {
104 1
        return $this->request(
105 1
            new GuzzleRequest(
106 1
                'GET',
107
                $endpoint,
108 1
                $this->enhanceHeaders($headers),
109 1
                $this->encodeParams($params)
110
            )
111
        );
112
    }
113
114 3
    public function post(string $endpoint, $params = null, array $headers = []): array
115
    {
116 3
        return $this->request(
117 3
            new GuzzleRequest(
118 3
                'POST',
119
                $endpoint,
120 3
                $this->enhanceHeaders($headers),
121 3
                $this->encodeParams($params)
122
            )
123
        );
124
    }
125
}
126