dev-think-one /
laravel-workcast
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LaravelWorkcast; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use Illuminate\Support\Arr; |
||
| 7 | |||
| 8 | class AccessToken |
||
| 9 | { |
||
| 10 | protected string $token; |
||
| 11 | |||
| 12 | protected \DateTimeInterface $expiresAt; |
||
| 13 | |||
| 14 | protected int $expiresIn = 0; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * AccessToken constructor. |
||
| 18 | * |
||
| 19 | * @param string $token |
||
| 20 | * @param int $expiresIn |
||
| 21 | * @param \DateTimeInterface|null $expiresAt |
||
| 22 | */ |
||
| 23 | 19 | public function __construct(string $token, int $expiresIn, ?\DateTimeInterface $expiresAt = null) |
|
| 24 | { |
||
| 25 | 19 | $this->token = $token; |
|
| 26 | 19 | $this->expiresIn = $expiresIn; |
|
| 27 | |||
| 28 | 19 | if ($expiresAt) { |
|
| 29 | 1 | $this->expiresAt = $expiresAt; |
|
| 30 | } else { |
||
| 31 | 19 | $this->expiresAt = Carbon::now()->addSeconds($this->expiresIn); |
|
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Check is access token valid |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | 17 | public function valid(): bool |
|
| 40 | { |
||
| 41 | 17 | return $this->token && Carbon::now()->addMinute()->lessThan($this->expiresAt); |
|
| 42 | } |
||
| 43 | |||
| 44 | 2 | public function getExpiresAt(): \DateTimeInterface |
|
| 45 | { |
||
| 46 | 2 | if ($this->valid()) { |
|
| 47 | 2 | return $this->expiresAt; |
|
| 48 | } |
||
| 49 | |||
| 50 | 1 | return Carbon::now(); |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | 7 | public function getToken(): string |
|
| 57 | { |
||
| 58 | 7 | return $this->token; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get Authorization header value |
||
| 63 | * |
||
| 64 | * @return string|null |
||
| 65 | */ |
||
| 66 | 3 | public function getAuthorizationHeader(): ?string |
|
| 67 | { |
||
| 68 | 3 | if ($this->valid()) { |
|
| 69 | 2 | return "Bearer {$this->token}"; |
|
| 70 | } |
||
| 71 | |||
| 72 | 2 | return null; |
|
| 73 | } |
||
| 74 | |||
| 75 | 14 | public function __serialize(): array |
|
| 76 | { |
||
| 77 | 14 | return [ |
|
| 78 | 14 | 'token' => $this->token, |
|
| 79 | 14 | 'expiresIn' => $this->expiresIn, |
|
| 80 | 14 | 'expiresAt' => $this->expiresAt->format('Y-m-d H:i:s'), |
|
| 81 | 14 | ]; |
|
| 82 | } |
||
| 83 | |||
| 84 | 13 | public function __unserialize(array $data): void |
|
| 85 | { |
||
| 86 | 13 | $this->token = $data['token'] ?? ''; |
|
| 87 | 13 | $this->expiresIn = $data['expiresIn'] ?? 0; |
|
| 88 | |||
| 89 | try { |
||
| 90 | 13 | $this->expiresAt = Carbon::createFromFormat('Y-m-d H:i:s', $data['expiresAt']); |
|
| 91 | 1 | } catch (\Exception $e) { |
|
| 92 | 1 | $this->expiresAt = Carbon::now(); |
|
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | 1 | public function getRawData(array|string $only = [ |
|
| 97 | 'expiresIn', |
||
| 98 | 'expiresAt', |
||
| 99 | ]): array |
||
| 100 | { |
||
| 101 | 1 | return collect([ |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 102 | 1 | 'token' => $this->token, |
|
| 103 | 1 | 'expiresIn' => $this->expiresIn, |
|
| 104 | 1 | 'expiresAt' => $this->expiresAt->format('Y-m-d H:i:s'), |
|
| 105 | 1 | ]) |
|
| 106 | 1 | ->only(Arr::wrap($only)) |
|
| 107 | 1 | ->toArray(); |
|
| 108 | } |
||
| 109 | } |
||
| 110 |