1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CommerceGuys\Guzzle\Oauth2; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class AccessToken |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
protected $token; |
11
|
|
|
|
12
|
|
|
/** @var \DateTime|null */ |
13
|
|
|
protected $expires; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $type; |
17
|
|
|
|
18
|
|
|
/** @var AccessToken|null */ |
19
|
|
|
protected $refreshToken; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
protected $data; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $token |
26
|
|
|
* @param string $type The token type (from OAuth2 key 'token_type'). |
27
|
|
|
* @param array $data Other token data. |
28
|
|
|
*/ |
29
|
12 |
|
public function __construct($token, $type, array $data = []) |
30
|
|
|
{ |
31
|
12 |
|
$this->token = $token; |
32
|
12 |
|
$this->type = $type; |
33
|
12 |
|
$this->data = $data; |
34
|
12 |
|
if (isset($data['expires'])) { |
35
|
3 |
|
$this->expires = new \DateTime(); |
36
|
3 |
|
$this->expires->setTimestamp($data['expires']); |
37
|
12 |
|
} elseif (isset($data['expires_in'])) { |
38
|
5 |
|
$this->expires = new \DateTime(); |
39
|
5 |
|
$this->expires->add(new \DateInterval(sprintf('PT%sS', $data['expires_in']))); |
40
|
5 |
|
} |
41
|
12 |
|
if (isset($data['refresh_token'])) { |
42
|
9 |
|
$this->refreshToken = new self($data['refresh_token'], 'refresh_token'); |
43
|
9 |
|
} |
44
|
12 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
7 |
|
public function isExpired() |
50
|
|
|
{ |
51
|
7 |
|
return $this->expires !== null && $this->expires->getTimestamp() < time(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return \DateTime|null |
56
|
|
|
*/ |
57
|
3 |
|
public function getExpires() |
58
|
|
|
{ |
59
|
3 |
|
return $this->expires; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
1 |
|
public function getData() |
66
|
|
|
{ |
67
|
1 |
|
return $this->data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
1 |
|
public function getScope() |
74
|
|
|
{ |
75
|
1 |
|
return isset($this->data['scope']) ? $this->data['scope'] : ''; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
11 |
|
public function getToken() |
82
|
|
|
{ |
83
|
11 |
|
return $this->token; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return AccessToken|null |
88
|
|
|
*/ |
89
|
7 |
|
public function getRefreshToken() |
90
|
|
|
{ |
91
|
7 |
|
return $this->refreshToken; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
3 |
|
public function getType() |
98
|
|
|
{ |
99
|
3 |
|
return $this->type; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param AccessToken $refreshToken |
104
|
|
|
* |
105
|
|
|
* @return self |
106
|
|
|
*/ |
107
|
2 |
|
public function setRefreshToken(AccessToken $refreshToken) |
108
|
|
|
{ |
109
|
2 |
|
if ($refreshToken->getType() != 'refresh_token') { |
110
|
|
|
throw new InvalidArgumentException('Expected AccessToken to be "refresh_token" type, got "%s"', $refreshToken->getType()); |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
$this->refreshToken = $refreshToken; |
114
|
|
|
|
115
|
2 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|