1 | <?php |
||
32 | final class ExportAnalyticsHandler implements MessageHandlerInterface |
||
33 | { |
||
34 | /** @var RepositoryManagerInterface */ |
||
35 | private $elasticaRepositoryManager; |
||
36 | |||
37 | /** @var string */ |
||
38 | private $cacheDir; |
||
39 | |||
40 | /** @var ReportMailer */ |
||
41 | private $mailer; |
||
42 | |||
43 | /** @var ReportFileUploader */ |
||
44 | private $reportFileUploader; |
||
45 | |||
46 | /** @var CsvFileWriter */ |
||
47 | private $csvFileWriter; |
||
48 | |||
49 | /** @var RepositoryInterface */ |
||
50 | private $analyticsReportRepository; |
||
51 | |||
52 | /** @var CachedTenantContextInterface */ |
||
53 | private $cachedTenantContext; |
||
54 | |||
55 | /** @var TenantRepositoryInterface */ |
||
56 | private $tenantRepository; |
||
57 | |||
58 | public function __construct( |
||
77 | |||
78 | public function __invoke(ExportAnalytics $exportAnalytics) |
||
79 | { |
||
80 | $fileName = $exportAnalytics->getFileName(); |
||
81 | |||
82 | /** @var AnalyticsReportInterface $analyticsReport */ |
||
83 | $analyticsReport = $this->analyticsReportRepository->findOneBy(['assetId' => $fileName]); |
||
84 | |||
85 | if (null === $analyticsReport) { |
||
86 | throw new AnalyticsReportNotFoundException("Analytics report $fileName not found."); |
||
87 | } |
||
88 | |||
89 | try { |
||
90 | $tenantCode = $exportAnalytics->getTenantCode(); |
||
91 | $criteria = Criteria::fromQueryParameters( |
||
92 | $exportAnalytics->getTerm(), |
||
93 | [ |
||
94 | 'sort' => ['articleStatistics.pageViewsNumber' => 'desc'], |
||
95 | 'publishedBefore' => $exportAnalytics->getEnd(), |
||
96 | 'publishedAfter' => $exportAnalytics->getStart(), |
||
97 | 'tenantCode' => $tenantCode, |
||
98 | 'routes' => $exportAnalytics->getRouteIds(), |
||
99 | 'authors' => $exportAnalytics->getAuthors(), |
||
100 | ] |
||
101 | ); |
||
102 | |||
103 | $tenant = $this->tenantRepository->findOneBy(['code' => $tenantCode]); |
||
104 | if (null === $tenant) { |
||
105 | throw new RuntimeException("Tenant with code $tenantCode not found"); |
||
106 | } |
||
107 | |||
108 | $this->cachedTenantContext->setTenant($tenant); |
||
109 | |||
110 | $articleRepository = $this->elasticaRepositoryManager->getRepository(Article::class); |
||
111 | |||
112 | $articles = $articleRepository->findByCriteria($criteria); |
||
113 | $total = $articles->getTotalHits(); |
||
114 | $articles = $articles->getResults(0, 0 !== $total ? $total : 1); |
||
115 | $data = $this->objectsToArray($articles->toArray()); |
||
116 | $path = $this->cacheDir.'/'.$fileName; |
||
117 | |||
118 | $this->csvFileWriter->write($path, $data); |
||
119 | |||
120 | $url = $this->reportFileUploader->upload($analyticsReport, $path); |
||
121 | |||
122 | $this->mailer->sendReportReadyEmailNotification($exportAnalytics->getUserEmail(), $url); |
||
123 | $analyticsReport->setStatus(AnalyticsReportInterface::STATUS_COMPLETED); |
||
124 | } catch (Throwable $e) { |
||
125 | $analyticsReport->setStatus(AnalyticsReportInterface::STATUS_ERRORED); |
||
126 | } |
||
127 | |||
128 | $analyticsReport->setUpdatedAt(DateTime::getCurrentDateTime()); |
||
129 | $this->analyticsReportRepository->flush(); |
||
130 | } |
||
131 | |||
132 | private function objectsToArray(array $rows): array |
||
152 | } |
||
153 |