Cancelled
Pull Request — master (#11)
by Laurens
23:42
created

GuzzleIzettleClient::getAuthorizationHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi;
6
7
use DateTime;
8
use GuzzleHttp\ClientInterface;
9
use LauLamanApps\IzettleApi\Client\AccessToken;
10
use LauLamanApps\IzettleApi\Client\Exceptions\AccessTokenExpiredException;
11
use Psr\Http\Message\ResponseInterface;
12
13
class GuzzleIzettleClient implements IzettleClientInterface
14
{
15
    private $guzzleClient;
16
    private $accessToken;
17
18 10
    public function __construct(ClientInterface $guzzleClient, AccessToken $accessToken)
19
    {
20 10
        $this->guzzleClient = $guzzleClient;
21 10
        $this->accessToken = $accessToken;
22 10
        $this->validateAccessToken();
23 9
    }
24
25 3
    public function get(string $url, ?array $queryParameters = null): ResponseInterface
26
    {
27 3
        $options =  array_merge(['headers' => $this->getAuthorizationHeader()], ['query' => $queryParameters]);
28
29 3
        return $this->guzzleClient->get($url, $options);
30
    }
31
32 2
    public function post(string $url, string $jsonData): void
33
    {
34 2
        $headers = array_merge(
35 2
            $this->getAuthorizationHeader(),
36
            [
37 2
                'content-type' => 'application/json',
38
                'Accept' => 'application/json',
39
            ]
40
        );
41
42 2
        $options =  array_merge(['headers' => $headers], ['body' => $jsonData]);
43 2
        $this->guzzleClient->post($url, $options);
44 2
    }
45
46 2
    public function put(string $url, string $jsonData): void
47
    {
48 2
        $headers = array_merge(
49 2
            $this->getAuthorizationHeader(),
50
            [
51 2
                'content-type' => 'application/json',
52
                'Accept' => 'application/json',
53
            ]
54
        );
55
56 2
        $options =  array_merge(['headers' => $headers], ['body' => $jsonData]);
57
58 2
        $this->guzzleClient->put($url, $options);
59 2
    }
60
61 1
    public function delete(string $url): void
62
    {
63 1
        $this->guzzleClient->delete($url, ['headers' => $this->getAuthorizationHeader()]);
64 1
    }
65
66 1
    public function getJson(ResponseInterface $response): string
67
    {
68 1
        return $response->getBody()->getContents();
69
    }
70
71 8
    private function getAuthorizationHeader(): array
72
    {
73 8
        $this->validateAccessToken();
74
75 8
        return ['Authorization' => sprintf('Bearer %s', $this->accessToken->getToken())];
76
    }
77
78 10
    private function validateAccessToken(): void
79
    {
80 10
        if ($this->accessToken->isExpired()) {
81 1
            throw new AccessTokenExpiredException(
82 1
                sprintf(
83 1
                    'Access Token was valid till \'%s\' it\'s now \'%s\'',
84 1
                    $this->accessToken->getExpires()->format('Y-m-d H:i:s.u'),
85 1
                    (new DateTime())->format('Y-m-d H:i:s.u')
86
                )
87
            );
88
        }
89 9
    }
90
}
91