1 | <?php |
||
2 | |||
3 | namespace Parroauth2\Client\EndPoint\Token; |
||
4 | |||
5 | use DateTime; |
||
6 | |||
7 | /** |
||
8 | * Response of the token endpoint |
||
9 | * |
||
10 | * @see https://tools.ietf.org/html/rfc6749#section-5.1 |
||
11 | * |
||
12 | * @psalm-immutable |
||
13 | */ |
||
14 | class TokenResponse |
||
15 | { |
||
16 | /** |
||
17 | * @var array<string, mixed> |
||
18 | */ |
||
19 | private $response; |
||
20 | |||
21 | /** |
||
22 | * @var DateTime|null |
||
23 | */ |
||
24 | private $expiresAt; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * TokenResponse constructor. |
||
29 | * |
||
30 | * @param array<string, mixed> $response |
||
31 | */ |
||
32 | 47 | public function __construct(array $response) |
|
33 | { |
||
34 | 47 | $this->response = $response; |
|
35 | |||
36 | 47 | if (isset($response['expires_in']) && $response['expires_in'] >= 0) { |
|
37 | /** @psalm-suppress ImpureMethodCall */ |
||
38 | 33 | $this->expiresAt = (new DateTime())->add(new \DateInterval('PT' . (int) $response['expires_in'] . 'S')); |
|
39 | } |
||
40 | 47 | } |
|
41 | |||
42 | /** |
||
43 | * Get the access token |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 29 | public function accessToken(): string |
|
48 | { |
||
49 | 29 | return $this->response['access_token']; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Get the access token type |
||
54 | * The value is in lower case |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | 14 | public function type(): string |
|
59 | { |
||
60 | 14 | return strtolower($this->response['token_type']); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Get the expiration date time |
||
65 | * May be null if expires_in is not provided |
||
66 | * |
||
67 | * @return DateTime|null |
||
68 | */ |
||
69 | 10 | public function expiresAt(): ?DateTime |
|
70 | { |
||
71 | 10 | return $this->expiresAt; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Check if the access token has expired |
||
76 | * If expires_in is not provided, this method will always return true |
||
77 | * |
||
78 | * Note: This method does not guarantee that the token is actually valid |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | 6 | public function expired(): bool |
|
83 | { |
||
84 | 6 | return $this->expiresAt && $this->expiresAt < new DateTime(); |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Get the issued refresh token |
||
89 | * |
||
90 | * @return string|null |
||
91 | */ |
||
92 | 12 | public function refreshToken(): ?string |
|
93 | { |
||
94 | 12 | return $this->response['refresh_token'] ?? null; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Get the list of requested (and authorized) scopes |
||
99 | * |
||
100 | * @return string[]|null |
||
101 | */ |
||
102 | 7 | public function scopes(): ?array |
|
103 | { |
||
104 | 7 | if (isset($this->response['scope'])) { |
|
105 | 7 | return explode(' ', $this->response['scope']); |
|
106 | } |
||
107 | |||
108 | 1 | return null; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get a response field |
||
113 | * |
||
114 | * @param string $key |
||
115 | * @param null $default |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
116 | * |
||
117 | * @return mixed |
||
118 | */ |
||
119 | 1 | public function get(string $key, $default = null) |
|
120 | { |
||
121 | 1 | return $this->response[$key] ?? $default; |
|
122 | } |
||
123 | } |
||
124 |