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