| Conditions | 4 |
| Paths | 16 |
| Total Lines | 51 |
| 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 | '', |
||
| 93 | [ |
||
| 94 | 'sort' => ['articleStatistics.pageViewsNumber' => 'desc'], |
||
| 95 | 'publishedBefore' => $exportAnalytics->getEnd(), |
||
| 96 | 'publishedAfter' => $exportAnalytics->getStart(), |
||
| 97 | 'tenantCode' => $tenantCode, |
||
| 98 | ] |
||
| 99 | ); |
||
| 100 | |||
| 101 | $tenant = $this->tenantRepository->findOneBy(['code' => $tenantCode]); |
||
| 102 | if (null === $tenant) { |
||
| 103 | throw new RuntimeException("Tenant with code $tenantCode not found"); |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->cachedTenantContext->setTenant($tenant); |
||
| 107 | |||
| 108 | $articleRepository = $this->elasticaRepositoryManager->getRepository(Article::class); |
||
| 109 | |||
| 110 | $articles = $articleRepository->findByCriteria($criteria); |
||
| 111 | $total = $articles->getTotalHits(); |
||
| 112 | $articles = $articles->getResults(0, $total); |
||
| 113 | $data = $this->objectsToArray($articles->toArray()); |
||
| 114 | $path = $this->cacheDir.'/'.$fileName; |
||
| 115 | |||
| 116 | $this->csvFileWriter->write($path, $data); |
||
| 117 | |||
| 118 | $url = $this->reportFileUploader->upload($analyticsReport, $path); |
||
| 119 | |||
| 120 | $this->mailer->sendReportReadyEmailNotification($exportAnalytics->getUserEmail(), $url); |
||
| 121 | $analyticsReport->setStatus(AnalyticsReportInterface::STATUS_COMPLETED); |
||
| 122 | } catch (Throwable $e) { |
||
| 123 | $analyticsReport->setStatus(AnalyticsReportInterface::STATUS_ERRORED); |
||
| 124 | } |
||
| 125 | |||
| 126 | $analyticsReport->setUpdatedAt(new DateTime()); |
||
| 127 | $this->analyticsReportRepository->flush(); |
||
| 128 | } |
||
| 129 | |||
| 151 |