File size: 3,504 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

declare(strict_types=1);

namespace Mautic\ReportBundle\Scheduler\Model;

use Mautic\CoreBundle\Exception\FileInvalidException;
use Mautic\CoreBundle\Exception\FilePathException;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\FilePathResolver;
use Mautic\CoreBundle\Helper\FileProperties;
use Mautic\ReportBundle\Entity\Report;
use Mautic\ReportBundle\Exception\FileTooBigException;

class FileHandler
{
    private const REPORTS_DIR = 'csv_reports';

    public function __construct(
        private FilePathResolver $filePathResolver,
        private FileProperties $fileProperties,
        private CoreParametersHelper $coreParametersHelper
    ) {
    }

    /**
     * @throws FileInvalidException
     * @throws FileTooBigException
     */
    public function fileCanBeAttached(string $filePath): void
    {
        $fileSize    = $this->fileProperties->getFileSize($filePath);
        $maxFileSize = (int) $this->coreParametersHelper->get('report_export_max_filesize_in_bytes');

        if ($fileSize > $maxFileSize) {
            throw new FileTooBigException("File {$filePath} has {$fileSize} bytes which is more than the limit of {$maxFileSize} bytes.");
        }
    }

    /**
     * Zips the file and returns the path where the zip file was created.
     *
     * @throws FilePathException
     */
    public function zipIt(string $originalFilePath): string
    {
        $zipFilePath = str_replace('.csv', '.zip', $originalFilePath);
        $zipArchive  = new \ZipArchive();

        if (true === $zipArchive->open($zipFilePath, \ZipArchive::OVERWRITE | \ZipArchive::CREATE)) {
            $zipArchive->addFile($originalFilePath, 'report.csv');
            $zipArchive->close();

            return $zipFilePath;
        }

        throw new FilePathException("Could not create zip archive at {$zipFilePath}. {$zipArchive->getStatusString()}");
    }

    public function getPathToCompressedCsvFileForReport(Report $report): string
    {
        return $this->getPathToCompressedCsvFileForReportId($report->getId());
    }

    public function getPathToCompressedCsvFileForReportId(int $reportId): string
    {
        return $this->getCompressedCsvFileForReportDir()."/report_{$reportId}.zip";
    }

    /**
     * @codeCoverageIgnore as it calls PHP function only.
     */
    public function compressedCsvFileForReportExists(Report $report): bool
    {
        $filePath = $this->getPathToCompressedCsvFileForReport($report);

        return file_exists($filePath);
    }

    public function moveZipToPermanentLocation(Report $report, string $originalPath): void
    {
        $compressedCsvPath = $this->getPathToCompressedCsvFileForReport($report);

        $this->filePathResolver->delete($compressedCsvPath);
        $this->filePathResolver->createDirectory(dirname($compressedCsvPath));
        $this->filePathResolver->move($originalPath, $compressedCsvPath);
    }

    public function delete(string $filePath): void
    {
        $this->filePathResolver->delete($filePath);
    }

    public function deleteCompressedCsvFileForReportId(int $reportId): void
    {
        $filePath = $this->getPathToCompressedCsvFileForReportId($reportId);
        if (file_exists($filePath)) {
            $this->delete($filePath);
        }
    }

    public function getCompressedCsvFileForReportDir(): string
    {
        $reportDir = $this->coreParametersHelper->get('report_temp_dir');

        return $reportDir.'/'.self::REPORTS_DIR;
    }
}