|
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
|
|
|
abstract class AbstractClient |
|
14
|
|
|
{ |
|
15
|
|
|
private $guzzleClient; |
|
16
|
|
|
private $accessToken; |
|
17
|
|
|
private $organizationUuid = 'self'; |
|
18
|
|
|
|
|
19
|
15 |
|
public function __construct(ClientInterface $guzzleClient, AccessToken $accessToken) |
|
20
|
|
|
{ |
|
21
|
15 |
|
$this->guzzleClient = $guzzleClient; |
|
22
|
15 |
|
$this->accessToken = $accessToken; |
|
23
|
15 |
|
$this->validateAccessToken(); |
|
24
|
13 |
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
public function setOrganizationUuid(int $organizationUuid): void |
|
27
|
|
|
{ |
|
28
|
2 |
|
$this->organizationUuid = $organizationUuid; |
|
|
|
|
|
|
29
|
2 |
|
} |
|
30
|
|
|
|
|
31
|
9 |
|
protected function getOrganizationUuid(): string |
|
32
|
|
|
{ |
|
33
|
9 |
|
return $this->organizationUuid; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
6 |
|
protected function get(string $url, ?array $queryParameters = null): ResponseInterface |
|
38
|
|
|
{ |
|
39
|
6 |
|
$options = array_merge(['headers' => $this->getAuthorizationHeader()], ['query' => $queryParameters]); |
|
40
|
|
|
|
|
41
|
6 |
|
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
|
11 |
|
protected function getAuthorizationHeader(): array |
|
65
|
|
|
{ |
|
66
|
11 |
|
$this->validateAccessToken(); |
|
67
|
11 |
|
return ['Authorization' => sprintf('Bearer %s', $this->accessToken->getToken())]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
15 |
|
private function validateAccessToken(): void |
|
71
|
|
|
{ |
|
72
|
15 |
|
if ($this->accessToken->isExpired()) { |
|
73
|
2 |
|
throw new AccessTokenExpiredException( |
|
74
|
2 |
|
sprintf( |
|
75
|
2 |
|
'Access Token was valid till \'%s\' it\'s now \'%s\'', |
|
76
|
2 |
|
$this->accessToken->getExpires()->format('Y-m-d H:i:s.u'), |
|
77
|
2 |
|
(new DateTime())->format('Y-m-d H:i:s.u') |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
13 |
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
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.