Conditions | 5 |
Paths | 16 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
153 |