src/Core/Framework/Log/ScheduledTask/LogCleanupTaskHandler.php line 56

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Log\ScheduledTask;
  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 Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\Messenger\Attribute\AsMessageHandler;
  10. /**
  11.  * @internal
  12.  */
  13. #[AsMessageHandler(handlesLogCleanupTask::class)]
  14. #[Package('core')]
  15. final class LogCleanupTaskHandler extends ScheduledTaskHandler
  16. {
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(
  21.         EntityRepository $scheduledTaskRepository,
  22.         private readonly SystemConfigService $systemConfigService,
  23.         private readonly Connection $connection
  24.     ) {
  25.         parent::__construct($scheduledTaskRepository);
  26.     }
  27.     public function run(): void
  28.     {
  29.         $entryLifetimeSeconds $this->systemConfigService->getInt('core.logging.entryLifetimeSeconds');
  30.         $maxEntries $this->systemConfigService->getInt('core.logging.entryLimit');
  31.         if ($entryLifetimeSeconds !== -1) {
  32.             $deleteBefore = (new \DateTime(sprintf('- %s seconds'$entryLifetimeSeconds)))
  33.                 ->format(Defaults::STORAGE_DATE_TIME_FORMAT);
  34.             $this->connection->executeStatement(
  35.                 'DELETE FROM `log_entry` WHERE `created_at` < :before',
  36.                 ['before' => $deleteBefore]
  37.             );
  38.         }
  39.         if ($maxEntries !== -1) {
  40.             $sql 'DELETE ld FROM `log_entry` ld LEFT JOIN (
  41.                         SELECT id
  42.                         FROM `log_entry`
  43.                         ORDER BY `created_at`
  44.                         DESC LIMIT :maxEntries
  45.                     ) ls ON ld.ID = ls.ID
  46.                     WHERE ls.ID IS NULL;';
  47.             $statement $this->connection->prepare($sql);
  48.             $statement->bindValue('maxEntries'$maxEntries\PDO::PARAM_INT);
  49.             $statement->executeStatement();
  50.         }
  51.     }
  52. }