![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mautic.corals.io/app/bundles/ReportBundle/Model/ |
<?php namespace Mautic\ReportBundle\Model; use Mautic\ReportBundle\Scheduler\Model\FileHandler; class ReportCleanup { public const KEEP_FILE_DAYS = 7; public function __construct(private FileHandler $fileHandler) { } public function cleanup(int $reportId): void { if ($this->shouldBeDeleted($this->fileHandler->getPathToCompressedCsvFileForReportId($reportId))) { $this->fileHandler->deleteCompressedCsvFileForReportId($reportId); } } /** * Deletes files older than KEEP_FILE_DAYS. */ public function cleanupAll(): void { $reportDirectory = $this->fileHandler->getCompressedCsvFileForReportDir(); if (!file_exists($reportDirectory)) { return; } $files = array_diff(scandir($reportDirectory), ['.', '..']); foreach ($files as $file) { $filePath = $reportDirectory.'/'.$file; if (is_dir($filePath)) { continue; } if ($this->shouldBeDeleted($filePath)) { $this->fileHandler->delete($filePath); } } } private function shouldBeDeleted(string $filePath): bool { if (!file_exists($filePath)) { return false; } $created = new \DateTime(date('Y-m-d', filemtime($filePath))); $now = new \DateTime(); $days = $created->diff($now)->days; if ($days > self::KEEP_FILE_DAYS) { return true; } return false; } }