Laravel AT Protocol Client (alpha & unstable)
1<?php
2
3namespace SocialDept\AtpClient\Session;
4
5use SocialDept\AtpClient\Data\Credentials;
6use SocialDept\AtpClient\Data\DPoPKey;
7use SocialDept\AtpClient\Enums\AuthType;
8use SocialDept\AtpClient\Enums\Scope;
9
10class Session
11{
12 public function __construct(
13 protected Credentials $credentials,
14 protected DPoPKey $dpopKey,
15 protected string $pdsEndpoint,
16 ) {}
17
18 public function did(): string
19 {
20 return $this->credentials->did;
21 }
22
23 public function handle(): ?string
24 {
25 return $this->credentials->handle;
26 }
27
28 public function accessToken(): string
29 {
30 return $this->credentials->accessToken;
31 }
32
33 public function refreshToken(): string
34 {
35 return $this->credentials->refreshToken;
36 }
37
38 public function dpopKey(): DPoPKey
39 {
40 return $this->dpopKey;
41 }
42
43 public function pdsEndpoint(): string
44 {
45 return $this->pdsEndpoint;
46 }
47
48 public function isExpired(): bool
49 {
50 return $this->credentials->isExpired();
51 }
52
53 public function expiresIn(): int
54 {
55 return $this->credentials->expiresIn();
56 }
57
58 public function scopes(): array
59 {
60 return $this->credentials->scope;
61 }
62
63 public function hasScope(string $scope): bool
64 {
65 return in_array($scope, $this->credentials->scope, true);
66 }
67
68 /**
69 * Check if the session has the given scope (alias for hasScope with Scope enum support).
70 */
71 public function can(string|Scope $scope): bool
72 {
73 $scopeValue = $scope instanceof Scope ? $scope->value : $scope;
74
75 return $this->hasScope($scopeValue);
76 }
77
78 /**
79 * Check if the session has any of the given scopes.
80 *
81 * @param array<string|Scope> $scopes
82 */
83 public function canAny(array $scopes): bool
84 {
85 foreach ($scopes as $scope) {
86 if ($this->can($scope)) {
87 return true;
88 }
89 }
90
91 return false;
92 }
93
94 /**
95 * Check if the session has all of the given scopes.
96 *
97 * @param array<string|Scope> $scopes
98 */
99 public function canAll(array $scopes): bool
100 {
101 foreach ($scopes as $scope) {
102 if (! $this->can($scope)) {
103 return false;
104 }
105 }
106
107 return true;
108 }
109
110 /**
111 * Check if the session does NOT have the given scope.
112 */
113 public function cannot(string|Scope $scope): bool
114 {
115 return ! $this->can($scope);
116 }
117
118 public function authType(): AuthType
119 {
120 return $this->credentials->authType;
121 }
122
123 public function isLegacy(): bool
124 {
125 return $this->credentials->authType === AuthType::Legacy;
126 }
127
128 public function withCredentials(Credentials $credentials): self
129 {
130 return new self($credentials, $this->dpopKey, $this->pdsEndpoint);
131 }
132}