forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessToken.php
More file actions
62 lines (49 loc) · 1.73 KB
/
AccessToken.php
File metadata and controls
62 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Tempest\Auth\OAuth\DataObjects;
use League\OAuth2\Client\Token\AccessToken as LeagueAccessToken;
use League\OAuth2\Client\Token\AccessTokenInterface;
use Tempest\Mapper\MapFrom;
use function Tempest\map;
final readonly class AccessToken
{
public function __construct(
#[MapFrom('access_token')]
public string $accessToken,
#[MapFrom('token_type')]
public string $tokenType,
public string $scope,
#[MapFrom('expires_in')]
public ?int $expiresIn = null,
#[MapFrom('refresh_token')]
public ?string $refreshToken = null,
#[MapFrom('additional_informations')]
public ?array $additionalInformations = null,
) {}
public static function from(array $data): self
{
return map($data)->to(self::class);
}
public static function fromLeagueAccessToken(AccessTokenInterface $accessToken): self
{
return new self(
accessToken: $accessToken->getToken(),
tokenType: $accessToken->getValues()['token_type'] ?? 'Bearer',
scope: $accessToken->getValues()['scope'] ?? '',
expiresIn: $accessToken->getExpires()
? ($accessToken->getExpires() - time())
: null,
refreshToken: $accessToken->getRefreshToken(),
additionalInformations: $accessToken->getValues() ?: null,
);
}
public function toLeagueAccessToken(): LeagueAccessToken
{
return new LeagueAccessToken([
'access_token' => $this->accessToken,
'refresh_token' => $this->refreshToken,
'expires_in' => $this->expiresIn,
...$this->additionalInformations,
]);
}
}