|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LauLamanApps\iZettleApi\Client; |
|
6
|
|
|
|
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
9
|
|
|
|
|
10
|
|
|
final class AccessTokenFactory |
|
11
|
|
|
{ |
|
12
|
|
|
const API_ACCESS_TOKEN_REQUEST_URL = 'https://oauth.izettle.net/token'; |
|
13
|
|
|
const API_ACCESS_TOKEN_PASSWORD_GRANT = 'password'; |
|
14
|
|
|
const API_ACCESS_TOKEN_REFRESH_TOKEN_URL = 'https://oauth.izettle.net/token'; |
|
15
|
|
|
const API_ACCESS_TOKEN_REFRESH_TOKEN_GRANT = 'refresh_token'; |
|
16
|
|
|
|
|
17
|
|
|
private $guzzleClient; |
|
18
|
|
|
private $clientId; |
|
19
|
|
|
private $clientSecret; |
|
20
|
|
|
|
|
21
|
2 |
|
public function __construct(GuzzleClient $guzzleClient, string $clientId, string $clientSecret) |
|
22
|
|
|
{ |
|
23
|
2 |
|
$this->guzzleClient = $guzzleClient; |
|
24
|
2 |
|
$this->clientId = $clientId; |
|
25
|
2 |
|
$this->clientSecret = $clientSecret; |
|
26
|
2 |
|
} |
|
27
|
|
|
|
|
28
|
1 |
View Code Duplication |
public function getFromUserLogin(string $username, string $password): AccessToken |
|
29
|
|
|
{ |
|
30
|
|
|
$options = [ |
|
31
|
|
|
'form_params' => [ |
|
32
|
1 |
|
'grant_type' => self::API_ACCESS_TOKEN_PASSWORD_GRANT, |
|
33
|
1 |
|
'client_id' => $this->clientId, |
|
34
|
1 |
|
'client_secret' => $this->clientSecret, |
|
35
|
1 |
|
'username' => $username, |
|
36
|
1 |
|
'password' => $password |
|
37
|
|
|
], |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
1 |
|
return $this->getAccessTokenFromData($this->makeRequest(self::API_ACCESS_TOKEN_REQUEST_URL, $options)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
View Code Duplication |
public function refresh(AccessToken $accessToken): AccessToken |
|
44
|
|
|
{ |
|
45
|
|
|
$options = [ |
|
46
|
|
|
'form_params' => [ |
|
47
|
1 |
|
'grant_type' => self::API_ACCESS_TOKEN_REFRESH_TOKEN_GRANT, |
|
48
|
1 |
|
'client_id' => $this->clientId, |
|
49
|
1 |
|
'client_secret' => $this->clientSecret, |
|
50
|
1 |
|
'refresh_token' => $accessToken->getRefreshToken() |
|
51
|
|
|
], |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
1 |
|
return $this->getAccessTokenFromData($this->makeRequest(self::API_ACCESS_TOKEN_REFRESH_TOKEN_URL, $options)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
private function makeRequest($url, $options): array |
|
58
|
|
|
{ |
|
59
|
2 |
|
$headers = ['headers' => ['Content-Type' => 'application/x-www-form-urlencoded']]; |
|
60
|
2 |
|
$options = array_merge($headers, $options); |
|
61
|
|
|
|
|
62
|
2 |
|
$response = $this->guzzleClient->request('POST', $url, $options); |
|
63
|
2 |
|
$data = json_decode($response->getBody()->getContents(), true); |
|
64
|
|
|
|
|
65
|
2 |
|
return $data; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
private function getAccessTokenFromData(array $data): AccessToken |
|
69
|
|
|
{ |
|
70
|
2 |
|
return new AccessToken( |
|
71
|
2 |
|
$data['access_token'], |
|
72
|
2 |
|
new DateTime(sprintf('+%d second', $data['expires_in'])), |
|
73
|
2 |
|
$data['refresh_token'] |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|