Files
esun3d/app/code/Esun/LingxingErp/Cron/CleanSyncLogs.php
T
2026-09-15 10:35:56 +08:00

37 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\LingxingErp\Cron;
use Esun\LingxingErp\Model\Config;
use Magento\Framework\App\ResourceConnection;
class CleanSyncLogs
{
private const COMPACT_STATUSES = ['success', 'ignored'];
public function __construct(
private readonly Config $config,
private readonly ResourceConnection $resource
) {
}
public function execute(): int
{
$connection = $this->resource->getConnection();
$table = $this->resource->getTableName('esun_lingxing_sync_log');
$successCutoff = gmdate('Y-m-d H:i:s', time() - ($this->config->getSuccessLogRetentionDays() * 86400));
$errorCutoff = gmdate('Y-m-d H:i:s', time() - ($this->config->getErrorLogRetentionDays() * 86400));
$deleted = $connection->delete($table, [
'status IN (?)' => self::COMPACT_STATUSES,
'created_at < ?' => $successCutoff,
]);
$deleted += $connection->delete($table, [
'status NOT IN (?)' => self::COMPACT_STATUSES,
'created_at < ?' => $errorCutoff,
]);
return $deleted;
}
}