feat: XML dump cleanup command
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
#[AsCommand(
|
||||||
|
name: 'app:cleanup:xml-dumps',
|
||||||
|
description: 'Removes XML dumps of BPN requests/responses older than a week')
|
||||||
|
]
|
||||||
|
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
|
||||||
|
{
|
||||||
|
// we look for files older than 1 week
|
||||||
|
$maxDate = (new \DateTimeImmutable())->modify('-1 week')->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) {
|
||||||
|
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
||||||
|
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
$this->xmlDump->delete($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
$message = 'Removed '.count($files).' outdated XML dumps';
|
||||||
|
$this->logger->info($message);
|
||||||
|
$output->writeln('<info>'.$message.'</info>');
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user