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

GuzzleIzettleClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 78
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A get() 0 6 1
A post() 0 13 1
A put() 0 14 1
A delete() 0 4 1
A getJson() 0 4 1
A getAuthorizationHeader() 0 6 1
A validateAccessToken() 0 12 2
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