1 | <?php |
||
2 | |||
3 | namespace IproSoftwareApi\AccessToken; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use IproSoftwareApi\Contracts\AccessToken as AccessTokenInterface; |
||
7 | use IproSoftwareApi\Exceptions\IproSoftwareApiAccessTokenException; |
||
8 | use Psr\Http\Message\ResponseInterface; |
||
9 | |||
10 | class AccessToken implements AccessTokenInterface |
||
11 | { |
||
12 | protected $accessToken; |
||
13 | protected $tokenType; |
||
14 | protected $expiresIn; |
||
15 | protected $expiresAt; |
||
16 | |||
17 | /** |
||
18 | * AccessToken constructor. |
||
19 | * |
||
20 | * @param string|null $accessToken |
||
21 | * @param string|null $tokenType |
||
22 | * @param string|null $expiresIn |
||
23 | * @param string|null $expiresAt |
||
24 | */ |
||
25 | 18 | public function __construct(string $accessToken = null, string $tokenType = null, string $expiresIn = null, string $expiresAt = null) |
|
26 | { |
||
27 | 18 | $this->accessToken = $accessToken; |
|
28 | 18 | $this->tokenType = $tokenType; |
|
29 | 18 | $this->expiresIn = $expiresIn; |
|
30 | 18 | $this->expiresAt = $expiresAt; |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * Specify data which should be serialized to JSON. |
||
35 | * |
||
36 | * @link https://php.net/manual/en/jsonserializable.jsonserialize.php |
||
37 | * |
||
38 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
39 | * which is a value of any type other than a resource. |
||
40 | * |
||
41 | * @since 5.4.0 |
||
42 | */ |
||
43 | 1 | public function jsonSerialize(): mixed |
|
44 | { |
||
45 | 1 | return [ |
|
46 | 1 | 'access_token' => $this->accessToken, |
|
47 | 1 | 'token_type' => $this->tokenType, |
|
48 | 1 | 'expires_in' => $this->expiresIn, |
|
49 | 1 | 'expires_at' => $this->expiresAt, |
|
50 | 1 | ]; |
|
51 | } |
||
52 | |||
53 | 8 | public static function makeFromJson(string $json): ?AccessTokenInterface |
|
54 | { |
||
55 | 8 | $data = json_decode($json, true); |
|
56 | 8 | if (is_array($data)) { |
|
57 | 8 | $accessToken = new self( |
|
58 | 8 | $data['access_token'] ?? null, |
|
59 | 8 | $data['token_type'] ?? null, |
|
60 | 8 | $data['expires_in'] ?? null, |
|
61 | 8 | $data['expires_at'] ?? null |
|
62 | 8 | ); |
|
63 | } |
||
64 | |||
65 | 8 | return (isset($accessToken) && $accessToken->hasAccessToken()) ? $accessToken : null; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param ResponseInterface $response |
||
70 | * |
||
71 | * @throws IproSoftwareApiAccessTokenException |
||
72 | * |
||
73 | * @return AccessTokenInterface|null |
||
74 | */ |
||
75 | 5 | public static function makeFromApiResponse(ResponseInterface $response): ?AccessTokenInterface |
|
76 | { |
||
77 | 5 | $responseBody = json_decode($response->getBody(), true); |
|
78 | |||
79 | 5 | if (!isset($responseBody['access_token']) |
|
80 | 4 | || !isset($responseBody['token_type']) |
|
81 | 4 | || !isset($responseBody['expires_in']) |
|
82 | 5 | || $responseBody['expires_in'] < 60 * 5 |
|
83 | ) { |
||
84 | 1 | throw new IproSoftwareApiAccessTokenException($response, 'Get Access Token: Not Valid Response'); |
|
85 | } |
||
86 | |||
87 | 4 | $expiresAt = Carbon::now()->addSeconds((int)($responseBody['expires_in']??0)); |
|
88 | 4 | $responseBody['expires_at'] = $expiresAt->toString(); |
|
89 | |||
90 | 4 | $accessToken = call_user_func( |
|
91 | 4 | [self::class, 'makeFromJson'], |
|
92 | 4 | json_encode($responseBody) |
|
93 | 4 | ); |
|
94 | |||
95 | 4 | if (!($accessToken instanceof self)) { |
|
96 | 2 | throw new IproSoftwareApiAccessTokenException( |
|
97 | 2 | $response, |
|
98 | 2 | 'Get Access Token: Error while initialising' |
|
99 | 2 | ); |
|
100 | } |
||
101 | |||
102 | 2 | return $accessToken; |
|
103 | } |
||
104 | |||
105 | 15 | public function hasAccessToken(): bool |
|
106 | { |
||
107 | 15 | return (bool) $this->accessToken && !$this->isTokenExpired(); |
|
108 | } |
||
109 | |||
110 | 12 | public function isTokenExpired(): bool |
|
111 | { |
||
112 | 12 | return Carbon::parse($this->expiresAt) |
|
113 | 12 | ->lessThanOrEqualTo(Carbon::now()); |
|
114 | } |
||
115 | |||
116 | 4 | public function getAuthorizationHeader(): string |
|
117 | { |
||
118 | 4 | return ucfirst($this->tokenType) . ' ' . $this->accessToken; |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
119 | } |
||
120 | } |
||
121 |