Files
myep/src/Command/CleanupXMLDumpsCommand.php
T

60 lines
1.8 KiB
PHP

<?php
namespace App\Command;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\StorageAttributes;
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:cleanup:xml-dumps',
description: 'Removes outdated XML dumps of BPN requests/responses'
)]
class CleanupXMLDumpsCommand extends Command
{
public function __construct(
private readonly FilesystemOperator $xmlDump,
private readonly LoggerInterface $logger,
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
// we look for files older than 1 week
$maxDate = (new \DateTimeImmutable())->modify('-3 day')->getTimestamp();
try {
$files = $this
->xmlDump
->listContents('.')
->filter(fn (StorageAttributes $attributes) => $attributes->isFile() && $attributes->lastModified() < $maxDate)
->map(fn (StorageAttributes $attributes) => $attributes->path())
->toArray();
} catch (FilesystemException $e) {
$io->error($e->getMessage());
return Command::FAILURE;
}
foreach ($files as $file) {
$this->xmlDump->delete($file);
}
$message = 'Removed '.count($files).' outdated XML dumps';
$this->logger->info($message);
$io->success($message);
return Command::SUCCESS;
}
}