config = $config; $this->service = $service; $this->entityManager = $entityManager; $this->sendingService = $sendingService; $this->emailBuilder = $emailBuilder; $this->log = $log; } /** * @throws BadRequest * @throws Error * @throws Forbidden * @throws NotFound */ public function run(Data $data): void { $data = $data->getRaw(); $reportId = $data->reportId; $userId = $data->userId; /** @var ?ReportEntity $report */ $report = $this->entityManager->getEntityById(ReportEntity::ENTITY_TYPE, $reportId); if (!$report) { throw new RuntimeException("Report Sending: No report $reportId."); } /** @var ?User $user */ $user = $this->entityManager->getEntityById(User::ENTITY_TYPE, $userId); if (!$user) { throw new RuntimeException("Report Sending: No user $userId."); } if ($report->getType() === ReportEntity::TYPE_LIST) { $searchParams = SearchParams::create() ->withMaxSize($this->getSendingListMaxCount()); $orderByList = $report->getOrderByList(); if ($orderByList) { $arr = explode(':', $orderByList); /** * @var 'ASC'|'DESC' $orderDirection * @noinspection PhpRedundantVariableDocTypeInspection */ $orderDirection = strtoupper($arr[0]); $searchParams = $searchParams ->withOrderBy($arr[1]) ->withOrder($orderDirection); } $result = $this->service->runList($reportId, $searchParams, $user); } else { $result = $this->service->runGrid($reportId, null, $user); } $reportResult = $result; if ($result instanceof ListResult) { $reportResult = []; foreach ($result->getCollection() as $e) { $reportResult[] = get_object_vars($e->getValueMap()); } if ( count($reportResult) === 0 && $report->get('emailSendingDoNotSendEmptyReport') ) { $this->log->debug('Report Sending: Report ' . $report->get('name') . ' is empty and was not sent.'); return; } } if ($reportResult instanceof ListResult) { throw new LogicException(); } $attachmentId = $this->sendingService->getExportAttachmentId($report, $result, null, $user); $this->emailBuilder->buildEmailData($data, $reportResult, $report); $this->emailBuilder->sendEmail( $data->userId, $data->emailSubject, $data->emailBody, $attachmentId ); } private function getSendingListMaxCount(): int { return $this->config->get('reportSendingListMaxCount', self::LIST_REPORT_MAX_SIZE); } }