Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
TokenConfig | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getToken | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getDefaultToken | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getDefaultRefreshToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Albet\SanctumRefresh\Services\Factories; |
4 | |
5 | use Carbon\Carbon; |
6 | |
7 | readonly class TokenConfig |
8 | { |
9 | public Carbon $tokenExpireAt; |
10 | |
11 | public Carbon $refreshTokenExpireAt; |
12 | |
13 | public function __construct(public array $abilities = ['*'], ?Carbon $tokenExpireAt = null, ?Carbon $refreshTokenExpireAt = null) |
14 | { |
15 | $this->tokenExpireAt = $this->getDefaultToken($tokenExpireAt); |
16 | $this->refreshTokenExpireAt = $this->getDefaultRefreshToken($refreshTokenExpireAt); |
17 | } |
18 | |
19 | private function getToken(?Carbon $token, string $configString): ?Carbon |
20 | { |
21 | if (! $token) { |
22 | $config = config($configString); |
23 | if (! $config) { |
24 | return null; |
25 | } |
26 | |
27 | return now()->addMinutes($config); |
28 | } |
29 | |
30 | return $token; |
31 | } |
32 | |
33 | /** |
34 | * @throws \Exception |
35 | */ |
36 | private function getDefaultToken(?Carbon $token): Carbon |
37 | { |
38 | $result = self::getToken($token, 'sanctum-refresh.expiration.access_token'); |
39 | |
40 | if (! $result) { |
41 | throw new \Exception('sanctum-refresh.expiration.access_token is not set'); |
42 | } |
43 | |
44 | return $result; |
45 | } |
46 | |
47 | private function getDefaultRefreshToken(?Carbon $token): Carbon |
48 | { |
49 | return self::getToken($token, 'sanctum-refresh.expiration.refresh_token'); |
50 | } |
51 | } |