1 | <?php |
||
10 | final class DatabaseAccessTokenRepository implements AccessTokenRepositoryInterface |
||
11 | { |
||
12 | /** |
||
13 | * The connection. |
||
14 | * |
||
15 | * @var ConnectionInterface |
||
16 | */ |
||
17 | private $connection; |
||
18 | |||
19 | /** |
||
20 | * The temporary access token table. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | private $table; |
||
25 | |||
26 | /** |
||
27 | * The number of seconds a token should last. |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | private $expires; |
||
32 | |||
33 | /** |
||
34 | * DatabaseAccessTokenRepository constructor. |
||
35 | * |
||
36 | * @param ConnectionInterface $connection The database connection interface. |
||
37 | * @param string $table The name of the database table. |
||
38 | * @param int $expires The default expire time in minutes. |
||
39 | */ |
||
40 | 11 | public function __construct(ConnectionInterface $connection, $table, $expires) |
|
46 | |||
47 | /** |
||
48 | * Retrieve an access token from the storage. |
||
49 | * |
||
50 | * @param int $authenticatableId The unique identifier of the authenticatable who has the access. |
||
51 | * @param string $token The encrypted token of the authenticatable. |
||
52 | * |
||
53 | * @return stdClass|array|bool |
||
54 | */ |
||
55 | 2 | public function retrieve($authenticatableId, $token) |
|
61 | |||
62 | /** |
||
63 | * Retrieve the first valid resource by the given attributes. |
||
64 | * |
||
65 | * @param array $queryParams The key - value pairs to match. |
||
66 | * @param array $attributes The attributes to be returned from the storage. |
||
67 | * |
||
68 | * @return stdClass|array|null |
||
69 | */ |
||
70 | 3 | public function retrieveByAttributes(array $queryParams, array $attributes = ['*']) |
|
71 | { |
||
72 | 3 | $query = $this->getTable(); |
|
73 | |||
74 | 3 | foreach ($queryParams as $column => $value) { |
|
75 | 3 | $query = $query->where($column, $value); |
|
76 | 3 | } |
|
77 | |||
78 | 3 | $token = $query->first($attributes); |
|
79 | |||
80 | 3 | return $this->filterExpiredAccessToken($token); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Store a new access token in the storage. |
||
85 | * |
||
86 | * @param int $authenticatableId The unique identifier of the authenticatable who has the access. |
||
87 | * @param string $token The encrypted token of the authenticatable. |
||
88 | * @param string|null $expiresAt The expiration date of the access token. |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | 2 | public function store($authenticatableId, $token, $expiresAt = null) |
|
101 | |||
102 | /** |
||
103 | * Update the expire date of the given access token in the storage. |
||
104 | * |
||
105 | * @param int $authenticatableId The unique identifier of the authenticatable. |
||
106 | * @param string $token The encrypted token to be updated. |
||
107 | * @param string $expiresAt The new expiration date of the access token. |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | 1 | public function update($authenticatableId, $token, $expiresAt) |
|
117 | |||
118 | /** |
||
119 | * Delete a resource from the storage. |
||
120 | * |
||
121 | * @param string $token The encrypted token of the authenticatable. |
||
122 | * |
||
123 | * @return int |
||
124 | */ |
||
125 | 1 | public function delete($token) |
|
129 | |||
130 | /** |
||
131 | * Delete expired access tokens from the storage. |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | 1 | public function deleteExpired() |
|
139 | |||
140 | /** |
||
141 | * Get a new find query. |
||
142 | * |
||
143 | * @param int $authenticatableId |
||
144 | * @param string $token |
||
145 | * |
||
146 | * @return \Illuminate\Database\Query\Builder |
||
147 | */ |
||
148 | 3 | private function find($authenticatableId, $token) |
|
152 | |||
153 | /** |
||
154 | * Get an access token payload. |
||
155 | * |
||
156 | * @param int $authenticatableId |
||
157 | * @param string $token |
||
158 | * @param string|null $expiresAt |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | 2 | private function getAccessTokenPayload($authenticatableId, $token, $expiresAt) |
|
163 | { |
||
164 | 2 | $expiresAt = $expiresAt ? $expiresAt : $this->makeExpiresAt(); |
|
165 | |||
166 | $payload = [ |
||
167 | 2 | 'authenticatable_id' => $authenticatableId, |
|
168 | 2 | 'token' => $token, |
|
169 | 2 | 'created_at' => (string) $this->getNow(), |
|
170 | 2 | 'expires_at' => (string) $expiresAt, |
|
171 | 2 | ]; |
|
172 | |||
173 | 2 | return $payload; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Filter the given database response and only return if it is not expired. |
||
178 | * |
||
179 | * @param stdClass|array|null $accessToken |
||
180 | * |
||
181 | * @return stdClass|array|null |
||
182 | */ |
||
183 | 5 | private function filterExpiredAccessToken($accessToken) |
|
189 | |||
190 | /** |
||
191 | * Determine if the token has expired. |
||
192 | * |
||
193 | * @param stdClass $accessToken |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | 4 | private function accessTokenExpired(stdClass $accessToken) |
|
203 | |||
204 | /** |
||
205 | * Make the expires at property from configuration. |
||
206 | * |
||
207 | * @return Carbon |
||
208 | */ |
||
209 | 1 | private function makeExpiresAt() |
|
213 | |||
214 | /** |
||
215 | * Get the current time. |
||
216 | * |
||
217 | * @return Carbon |
||
218 | */ |
||
219 | 7 | private function getNow() |
|
223 | |||
224 | /** |
||
225 | * Get the time after given minutes. |
||
226 | * |
||
227 | * @param int $minutesLater |
||
228 | * |
||
229 | * @return Carbon |
||
230 | */ |
||
231 | 1 | private function getFuture($minutesLater) |
|
235 | |||
236 | /** |
||
237 | * Begin a new database query against the table. |
||
238 | * |
||
239 | * @return \Illuminate\Database\Query\Builder |
||
240 | */ |
||
241 | 10 | private function getTable() |
|
245 | |||
246 | /** |
||
247 | * Get the database connection instance. |
||
248 | * |
||
249 | * @return ConnectionInterface |
||
250 | */ |
||
251 | 10 | private function getConnection() |
|
255 | } |
||
256 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.