feat: cli command to anonymize local database

This commit is contained in:
Björn Fromme
2026-06-17 10:32:02 +02:00
parent 66d0f511df
commit 80569e0977
7 changed files with 786 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\Service\DatabaseAnonymizer;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:db:anonymize',
description: 'Anonymize personal data in the local database',
)]
final class DbAnonymizeCommand extends Command
{
public function __construct(
private readonly DatabaseAnonymizer $databaseAnonymizer,
private readonly LoggerInterface $logger,
private readonly string $environment,
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
if ('prod' === $this->environment) {
$io->error('The database anonymizer is disabled in the prod environment.');
return Command::FAILURE;
}
try {
$report = $this->databaseAnonymizer->anonymizeAll();
} catch (\Throwable $exception) {
$this->logger->error('Database anonymization failed', [
'environment' => $this->environment,
'error' => $exception->getMessage(),
]);
$io->error($exception->getMessage());
return Command::FAILURE;
}
$io->success(sprintf(
'Anonymized %d users, %d newsletter consents, %d newsletter opt-in requests, and %d booking drafts.',
$report['users'],
$report['newsletterConsents'],
$report['newsletterOptInRequests'],
$report['bookingEditDrafts'],
));
return Command::SUCCESS;
}
}