php-api/src/Api/Console/Commands/CleanupExpiredGracePeriods.php
Snider 57fa0d6727 refactor(api): relocate API module to Core\Api namespace
Move API module from src/Mod/Api/ to src/Api/ and update namespace
from Core\Mod\Api\ to Core\Api\ as part of monorepo separation.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 16:14:36 +00:00

67 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Mod\Api\Console\Commands;
use Illuminate\Console\Command;
use Mod\Api\Services\ApiKeyService;
/**
* Clean up API keys with expired grace periods.
*
* When an API key is rotated, the old key enters a grace period where
* both keys are valid. This command revokes keys whose grace period
* has ended.
*/
class CleanupExpiredGracePeriods extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'api:cleanup-grace-periods
{--dry-run : Show what would be revoked without actually revoking}';
/**
* The console command description.
*/
protected $description = 'Revoke API keys with expired grace periods after rotation';
/**
* Execute the console command.
*/
public function handle(ApiKeyService $service): int
{
$dryRun = $this->option('dry-run');
if ($dryRun) {
$this->warn('DRY RUN MODE - No keys will be revoked');
$this->newLine();
// Count keys that would be cleaned up
$count = \Mod\Api\Models\ApiKey::gracePeriodExpired()
->whereNull('deleted_at')
->count();
if ($count === 0) {
$this->info('No API keys with expired grace periods found.');
} else {
$this->info("Would revoke {$count} API key(s) with expired grace periods.");
}
return Command::SUCCESS;
}
$this->info('Cleaning up API keys with expired grace periods...');
$count = $service->cleanupExpiredGracePeriods();
if ($count === 0) {
$this->info('No API keys with expired grace periods found.');
} else {
$this->info("Revoked {$count} API key(s) with expired grace periods.");
}
return Command::SUCCESS;
}
}