1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BlizzardApi\Tokens; |
4
|
|
|
|
5
|
|
|
use BlizzardApi\Tokens\Exceptions\Expired; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Access Token |
9
|
|
|
* |
10
|
|
|
* @author Hristo Mitev <[email protected]> |
11
|
|
|
* @author Oleg Kachinsky <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Access |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string $token The access token itself |
17
|
|
|
*/ |
18
|
|
|
protected $token; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string $tokenType The type of the token |
22
|
|
|
*/ |
23
|
|
|
protected $tokenType; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int $expiresIn The time for the token to expire in seconds |
27
|
|
|
*/ |
28
|
|
|
protected $expiresIn; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \DateTime $createdAt when was the token created |
32
|
|
|
*/ |
33
|
|
|
protected $createdAt; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \DateTime $expiresAt when is the token expiring |
37
|
|
|
*/ |
38
|
|
|
protected $expiresAt; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
* |
43
|
|
|
* @param string $accessToken Access token |
44
|
|
|
* @param string $tokenType Token type |
45
|
|
|
* @param int $expiresIn Expires in (seconds) |
46
|
|
|
*/ |
47
|
|
|
public function __construct($accessToken, $tokenType, $expiresIn) |
48
|
|
|
{ |
49
|
|
|
$this->token = $accessToken; |
50
|
|
|
$this->tokenType = $tokenType; |
51
|
|
|
$this->expiresIn = $expiresIn; |
52
|
|
|
$this->createdAt = new \DateTime(); |
53
|
|
|
$this->expiresAt = new \DateTime(); |
54
|
|
|
$this->expiresAt->add(new \DateInterval('PT'.$this->expiresIn.'S')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create token from json object |
59
|
|
|
* |
60
|
|
|
* @param \stdClass $jsonObject JSON object |
61
|
|
|
* |
62
|
|
|
* @return Access |
63
|
|
|
*/ |
64
|
|
|
public static function fromJson($jsonObject) |
65
|
|
|
{ |
66
|
|
|
return new self($jsonObject->access_token, $jsonObject->token_type, $jsonObject->expires_in); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Check if the token is expired |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function isExpired() |
75
|
|
|
{ |
76
|
|
|
return $this->expiresAt < new \DateTime(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the token string |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
* |
84
|
|
|
* @throws Expired |
85
|
|
|
*/ |
86
|
|
|
public function getToken() |
87
|
|
|
{ |
88
|
|
|
if ($this->isExpired()) { |
89
|
|
|
throw new Expired('Token has expired'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->token; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|