Add functionality to delete old versions of documents

This commit introduces a feature that automatically deletes old versions of StoredObjects in the Chill application. A cron job, "RemoveOldVersionCronJob", has been implemented to delete versions older than 90 days. A message handler, "RemoveOldVersionMessageHandler", has been added to handle deletion requests. Furthermore, unit tests for the new functionality have been provided.
This commit is contained in:
2024-07-15 15:54:26 +02:00
parent 67d24cb951
commit c38f7c1179
10 changed files with 476 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\DocStoreBundle\Service\StoredObjectCleaner;
use Chill\DocStoreBundle\Exception\StoredObjectManagerException;
use Chill\DocStoreBundle\Repository\StoredObjectVersionRepository;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
final readonly class RemoveOldVersionMessageHandler implements MessageHandlerInterface
{
private const LOG_PREFIX = '[RemoveOldVersionMessageHandler] ';
public function __construct(
private StoredObjectVersionRepository $storedObjectVersionRepository,
private LoggerInterface $logger,
private EntityManagerInterface $entityManager,
private StoredObjectManagerInterface $storedObjectManager,
) {}
/**
* @throws StoredObjectManagerException
*/
public function __invoke(RemoveOldVersionMessage $message): void
{
$this->logger->info(self::LOG_PREFIX.'Received one message', ['storedObjectVersionId' => $message->storedObjectVersionId]);
$storedObjectVersion = $this->storedObjectVersionRepository->find($message->storedObjectVersionId);
if (null === $storedObjectVersion) {
$this->logger->error(self::LOG_PREFIX.'StoredObjectVersion not found in database', ['storedObjectVersionId' => $message->storedObjectVersionId]);
throw new \RuntimeException('StoredObjectVersion not found with id '.$message->storedObjectVersionId);
}
$this->storedObjectManager->delete($storedObjectVersion);
$this->entityManager->remove($storedObjectVersion);
$this->entityManager->flush();
// clear the entity manager for future usage
$this->entityManager->clear();
}
}