presseportale/app/Console/Commands/PurgeMagicLinks.php
Kevin Adametz 5b8bdf4182
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
12-05-2026 Frontend dev
2026-05-12 18:32:33 +02:00

37 lines
1.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\MagicLink;
use Illuminate\Console\Command;
/**
* Löscht verbrauchte oder abgelaufene Magic-Links, die älter als 30 Tage sind.
* Läuft täglich via Scheduler.
*/
class PurgeMagicLinks extends Command
{
protected $signature = 'magic-links:purge {--days=30 : Links älter als X Tage löschen}';
protected $description = 'Löscht verbrauchte und abgelaufene Magic-Links.';
public function handle(): int
{
$days = (int) $this->option('days');
$cutoff = now()->subDays($days);
$deleted = MagicLink::query()
->where(function ($q) use ($cutoff): void {
$q->where('expires_at', '<', $cutoff)
->orWhere(function ($q) use ($cutoff): void {
$q->whereNotNull('consumed_at')
->where('consumed_at', '<', $cutoff);
});
})
->delete();
$this->info("Magic-Links bereinigt: {$deleted} Einträge gelöscht.");
return self::SUCCESS;
}
}