mein-sterntours/app/Console/Commands/CleanupNewsletterBlockedEmails.php
2026-01-23 17:34:40 +01:00

122 lines
3.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\NewsletterContact;
use Illuminate\Console\Command;
class CleanupNewsletterBlockedEmails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'newsletter:cleanup-blocked-emails {--dry-run : Nur anzeigen, ohne zu löschen}';
/**
* The console description of the command.
*
* @var string
*/
protected $description = 'Entfernt Newsletter-Kontakte mit blockierten E-Mail-Adressen (Alias/Proxy von Buchungsplattformen)';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->info('Suche nach blockierten E-Mail-Adressen...');
$dryRun = $this->option('dry-run');
// Liste der blockierten Domains
$blockedDomains = [
'@guest.booking.com',
'@messages.homeaway.com',
'@fewo.check24.de',
'@booking.com',
'@homeaway.com',
'@check24.de',
'@partner.booking.com',
];
$stats = [
'found' => 0,
'deleted' => 0,
];
// Hole alle Newsletter-Kontakte
$contacts = NewsletterContact::all();
$this->info("Prüfe {$contacts->count()} Kontakte...");
$bar = $this->output->createProgressBar($contacts->count());
$bar->start();
foreach ($contacts as $contact) {
$emailLower = strtolower($contact->email);
$isBlockedEmail = false;
foreach ($blockedDomains as $domain) {
if (str_ends_with($emailLower, strtolower($domain))) {
$isBlockedEmail = true;
break;
}
}
if ($isBlockedEmail) {
$stats['found']++;
if ($dryRun) {
$this->newLine();
$this->warn("Würde löschen: {$contact->email} (ID: {$contact->id})");
} else {
// Log erstellen vor dem Löschen
$contact->logs()->create([
'action' => 'deleted',
'description' => 'Kontakt entfernt - blockierte E-Mail-Domain (Buchungsplattform-Alias)',
]);
$contact->delete();
$stats['deleted']++;
}
}
$bar->advance();
}
$bar->finish();
$this->newLine(2);
// Statistiken ausgeben
if ($dryRun) {
$this->info('Dry-Run abgeschlossen!');
$this->table(
['Statistik', 'Anzahl'],
[
['Gefundene blockierte E-Mails', $stats['found']],
]
);
if ($stats['found'] > 0) {
$this->newLine();
$this->info('Führe den Befehl ohne --dry-run aus, um die Kontakte zu löschen:');
$this->comment('php artisan newsletter:cleanup-blocked-emails');
}
} else {
$this->info('Bereinigung abgeschlossen!');
$this->table(
['Statistik', 'Anzahl'],
[
['Gefundene blockierte E-Mails', $stats['found']],
['Gelöschte Kontakte', $stats['deleted']],
]
);
}
return 0;
}
}