Test Failed
Pull Request — master (#7)
by Laurens
02:01
created

AbstractClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 69
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setOrganizationUuid() 0 4 1
A getOrganizationUuid() 0 4 1
A get() 0 6 1
A post() 0 14 1
A delete() 0 4 1
A getAuthorizationHeader() 0 4 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\Client as GuzzleClient;
9
use LauLamanApps\IzettleApi\Client\AccessToken;
10
use LauLamanApps\IzettleApi\Client\Exceptions\AccessTokenExpiredException;
11
use Psr\Http\Message\ResponseInterface;
12
13
abstract class AbstractClient
14
{
15
    private $guzzleClient;
16
    private $accessToken;
17
    private $organizationUuid = 'self';
18
19 14
    public function __construct(GuzzleClient $guzzleClient, AccessToken $accessToken)
20
    {
21 14
        $this->guzzleClient = $guzzleClient;
22 14
        $this->accessToken = $accessToken;
23 14
        $this->validateAccessToken();
24 12
    }
25
26 2
    public function setOrganizationUuid(int $organizationUuid): void
27
    {
28 2
        $this->organizationUuid = $organizationUuid;
0 ignored issues
show
Documentation Bug introduced by
The property $organizationUuid was declared of type string, but $organizationUuid is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
29 2
    }
30
31 9
    protected function getOrganizationUuid(): string
32
    {
33 9
        return $this->organizationUuid;
34
    }
35
36
37 5
    protected function get(string $url, ?array $queryParameters = null): ResponseInterface
38
    {
39 5
        $options =  array_merge(['headers' => $this->getAuthorizationHeader()], ['query' => $queryParameters]);
40
41 5
        return $this->guzzleClient->get($url, $options);
42
    }
43
44 3
    protected function post(string $url, string $data): void
45
    {
46 3
        $headers = array_merge(
47 3
            $this->getAuthorizationHeader(),
48
            [
49 3
                'content-type' => 'application/json',
50
                'Accept' => 'application/json',
51
            ]
52
        );
53
54 3
        $options =  array_merge(['headers' => $headers], ['body' => $data]);
55
56 3
        $this->guzzleClient->post($url, $options);
57 3
    }
58
59 2
    protected function delete(string $url): void
60
    {
61 2
        $this->guzzleClient->delete($url, ['headers' => $this->getAuthorizationHeader()]);
62 2
    }
63
64 10
    protected function getAuthorizationHeader(): array
65
    {
66 10
        return ['Authorization' => sprintf('Bearer %s', $this->accessToken->getToken())];
67
    }
68
69 14
    private function validateAccessToken(): void
70
    {
71 14
        if ($this->accessToken->isExpired()) {
72 2
            throw new AccessTokenExpiredException(
73 2
                sprintf(
74 2
                    'Access Token was valid till \'%s\' its now \'%s\'',
75 2
                    $this->accessToken->getExpires()->format('Y-m-d H:i:s.u'),
76 2
                    (new DateTime())->format('Y-m-d H:i:s.u')
77
                )
78
            );
79
        }
80 12
    }
81
}
82