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