src/Core/Framework/DataAbstractionLayer/Version/Cleanup/CleanupVersionTaskHandler.php line 43

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Version\Cleanup;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;
  8. use Symfony\Component\Messenger\Attribute\AsMessageHandler;
  9. /**
  10.  * @internal
  11.  */
  12. #[AsMessageHandler(handlesCleanupVersionTask::class)]
  13. #[Package('core')]
  14. final class CleanupVersionTaskHandler extends ScheduledTaskHandler
  15. {
  16.     /**
  17.      * @internal
  18.      */
  19.     public function __construct(
  20.         EntityRepository $repository,
  21.         private readonly Connection $connection,
  22.         private readonly int $days
  23.     ) {
  24.         parent::__construct($repository);
  25.     }
  26.     public function run(): void
  27.     {
  28.         $time = new \DateTime();
  29.         $time->modify(sprintf('-%s day'$this->days));
  30.         do {
  31.             $result $this->connection->executeStatement(
  32.                 'DELETE FROM version WHERE created_at <= :timestamp LIMIT 1000',
  33.                 ['timestamp' => $time->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
  34.             );
  35.         } while ($result 0);
  36.         do {
  37.             $result $this->connection->executeStatement(
  38.                 'DELETE FROM version_commit WHERE created_at <= :timestamp LIMIT 1000',
  39.                 ['timestamp' => $time->format(Defaults::STORAGE_DATE_TIME_FORMAT)]
  40.             );
  41.         } while ($result 0);
  42.     }
  43. }