prepareResult($report, $user); $attachmentId = $this->sendingService->getExportAttachmentId($report, $result, null, $user); if (!$attachmentId) { throw new Error("Could not generate an export file for report {$report->getId()}."); } $attachment = $this->entityManager->getRDBRepositoryByClass(Attachment::class)->getById($attachmentId); if (!$attachment) { throw new Error("Could not fetch the export attachment."); } $this->prepareAttachmentFields($attachment); return $attachment; } private function getSendingListMaxCount(): int { return $this->config->get('reportSendingListMaxCount', self::LIST_REPORT_MAX_SIZE); } private function prepareListSearchParams(ReportEntity $report): SearchParams { $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); } return $searchParams; } /** * @throws BadRequest * @throws Forbidden * @throws Error * @throws NotFound */ private function prepareResult(ReportEntity $report, ?User $user): ListType\Result|GridType\Result { if ($report->getType() === ReportEntity::TYPE_LIST) { $searchParams = $this->prepareListSearchParams($report); return $this->service->runList($report->getId(), $searchParams, $user); } return $this->service->runGrid($report->getId(), null, $user); } private function prepareAttachmentFields(Attachment $attachment): void { $attachment->setRole(Attachment::ROLE_EXPORT_FILE); $attachment->setParent(null); $this->entityManager->saveEntity($attachment); } }