File size: 3,176 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
<?php

namespace Mautic\ReportBundle\Model;

use Mautic\ReportBundle\Adapter\ReportDataAdapter;
use Mautic\ReportBundle\Entity\Scheduler;
use Mautic\ReportBundle\Event\ReportScheduleSendEvent;
use Mautic\ReportBundle\Exception\FileIOException;
use Mautic\ReportBundle\ReportEvents;
use Mautic\ReportBundle\Scheduler\Enum\SchedulerEnum;
use Mautic\ReportBundle\Scheduler\Option\ExportOption;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class ReportExporter
{
    public function __construct(
        private ScheduleModel $schedulerModel,
        private ReportDataAdapter $reportDataAdapter,
        private ReportExportOptions $reportExportOptions,
        private ReportFileWriter $reportFileWriter,
        private EventDispatcherInterface $eventDispatcher
    ) {
    }

    /**
     * @throws FileIOException
     */
    public function processExport(ExportOption $exportOption): void
    {
        $schedulers = $this->schedulerModel->getScheduledReportsForExport($exportOption);
        foreach ($schedulers as $scheduler) {
            $this->processReport($scheduler);
        }
    }

    /**
     * @throws FileIOException
     */
    private function processReport(Scheduler $scheduler): void
    {
        $report = $scheduler->getReport();

        $dateTo = clone $scheduler->getScheduleDate();
        $dateTo->setTime(0, 0, 0);

        $dateFrom = clone $dateTo;
        switch ($report->getScheduleUnit()) {
            case SchedulerEnum::UNIT_NOW:
                $dateFrom->sub(new \DateInterval('P10Y'));
                $this->schedulerModel->turnOffScheduler($report);
                break;
            case SchedulerEnum::UNIT_DAILY:
                $dateFrom->sub(new \DateInterval('P1D'));
                break;
            case SchedulerEnum::UNIT_WEEKLY:
                $dateFrom->sub(new \DateInterval('P7D'));
                break;
            case SchedulerEnum::UNIT_MONTHLY:
                $dateFrom->sub(new \DateInterval('P1M'));
                break;
        }

        $this->reportExportOptions->setDateFrom($dateFrom);
        $this->reportExportOptions->setDateTo($dateTo->sub(new \DateInterval('PT1S')));

        // just published reports, but schedule continue
        if ($report->isPublished()) {
            $this->reportExportOptions->beginExport();
            while (true) {
                $data = $this->reportDataAdapter->getReportData($report, $this->reportExportOptions);

                $this->reportFileWriter->writeReportData($scheduler, $data, $this->reportExportOptions);

                $totalResults = $data->getTotalResults();
                unset($data);

                if ($this->reportExportOptions->getNumberOfProcessedResults() >= $totalResults) {
                    break;
                }

                $this->reportExportOptions->nextBatch();
            }

            $file  = $this->reportFileWriter->getFilePath($scheduler);
            $event = new ReportScheduleSendEvent($scheduler, $file);
            $this->eventDispatcher->dispatch($event, ReportEvents::REPORT_SCHEDULE_SEND);
        }

        $this->schedulerModel->reportWasScheduled($report);
    }
}