Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| PruneToken | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| handle | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Albet\SanctumRefresh\Commands; |
| 4 | |
| 5 | use Albet\SanctumRefresh\Models\RefreshToken; |
| 6 | use Illuminate\Console\Command; |
| 7 | |
| 8 | class PruneToken extends Command |
| 9 | { |
| 10 | public $signature = 'prune:token'; |
| 11 | |
| 12 | public $description = 'Prune expired token but instead from token expiration, use expiration from refresh.'; |
| 13 | |
| 14 | public function handle(): int |
| 15 | { |
| 16 | // Find refresh token that associated with Access Token. |
| 17 | // Figure out their expires_at and deletes them. |
| 18 | $tokens = RefreshToken::with('accessToken') |
| 19 | ->whereHas('accessToken', fn ($q) => $q->where('expires_at', '<', now())) |
| 20 | ->where('expires_at', '<', now()) |
| 21 | ->get(); |
| 22 | |
| 23 | // iterates through the token |
| 24 | foreach ($tokens as $token) { |
| 25 | // check if relationship match |
| 26 | if ($token->accessToken !== null) { |
| 27 | $token->accessToken->delete(); |
| 28 | $token->delete(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | $this->info('Token cleared.'); |
| 33 | |
| 34 | return self::SUCCESS; |
| 35 | } |
| 36 | } |