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