80 lines
2.2 KiB
PHP
80 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Repository\BookingEditDraftRepository;
|
|
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\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
#[AsCommand(
|
|
name: 'app:draft:cleanup',
|
|
description: 'Delete booking edit drafts for departed travels',
|
|
)]
|
|
class DraftCleanupCommand extends Command
|
|
{
|
|
public function __construct(
|
|
private readonly BookingEditDraftRepository $draftRepository,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Show what would be deleted without actually deleting');
|
|
}
|
|
|
|
/**
|
|
* @see Command::execute()
|
|
*/
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$io = new SymfonyStyle($input, $output);
|
|
$dryRun = $input->getOption('dry-run');
|
|
|
|
if ($dryRun) {
|
|
$expiredDrafts = $this->draftRepository->createQueryBuilder('d')
|
|
->where('d.travelDate < :today')
|
|
->setParameter('today', new \DateTimeImmutable('today'))
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
$count = \count($expiredDrafts);
|
|
|
|
if (0 === $count) {
|
|
$io->success('No expired drafts found.');
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
$io->note(sprintf('[DRY RUN] Would delete %d expired draft(s)', $count));
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
$deletedCount = $this->draftRepository->deleteExpiredDrafts();
|
|
|
|
if (0 === $deletedCount) {
|
|
$io->success('No expired drafts found.');
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
$this->logger->info('Deleted expired booking edit drafts', [
|
|
'count' => $deletedCount,
|
|
]);
|
|
|
|
$io->success(sprintf('Deleted %d expired draft(s).', $deletedCount));
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|