*/ trait TaskLogActionTrait { /** * @return ServiceManager */ abstract protected function getServiceManager(); /** * @param array $data * @param int $httpStatus * @return mixed */ abstract protected function returnJson($data, $httpStatus = 200); /** * @param string $taskId * @param string $userId * @return TaskLogEntity */ protected function getTaskLogEntity($taskId, $userId = null) { /** @var TaskLogInterface $taskLog */ $taskLog = $this->getServiceManager()->get(TaskLogInterface::SERVICE_ID); if (is_null($userId)) { $userId = $this->getUserId(); } return $taskLog->getByIdAndUser((string) $taskId, (string) $userId); } /** * Get default user id. * * @return string */ protected function getUserId() { return \common_session_SessionManager::getSession()->getUserUri(); } /** * @param string $taskId * @param string|null $forcedTaskType * @param string|null $userId * @return array * @throws \common_exception_BadRequest */ protected function getTaskLogReturnData($taskId, $forcedTaskType = null, $userId = null) { $taskLogEntity = $this->getTaskLogEntity($taskId, $userId); if (!is_null($forcedTaskType) && $taskLogEntity->getTaskName() !== $forcedTaskType) { throw new \common_exception_BadRequest("Wrong task type"); } $result['id'] = $this->getTaskId($taskLogEntity); $result['status'] = $this->getTaskStatus($taskLogEntity); $result['report'] = $taskLogEntity->getReport() ? $this->getTaskReport($taskLogEntity) : []; return array_merge($result, (array) $this->addExtraReturnData($taskLogEntity)); } /** * Returns task data in a specific data structure required by the front-end component. * * @param TaskInterface $task * @param array $extraData * @return mixed */ protected function returnTaskJson(TaskInterface $task, array $extraData = []) { return $this->returnJson([ 'success' => true, 'data' => [ 'extra' => $extraData, 'task' => $this->getTaskLogReturnData($task->getId()) ] ]); } /** * Return task identifier * * @param TaskLogEntity $taskLogEntity * @return string */ protected function getTaskId(TaskLogEntity $taskLogEntity) { return $taskLogEntity->getId(); } /** * @param TaskLogEntity $taskLogEntity * @return string */ protected function getTaskStatus(TaskLogEntity $taskLogEntity) { return $taskLogEntity->getStatus()->getLabel(); } /** * As default, it returns the reports as an associative array. * * @param TaskLogEntity $taskLogEntity * @return array */ protected function getTaskReport(TaskLogEntity $taskLogEntity) { return $this->getReportAsAssociativeArray($taskLogEntity->getReport()); } /** * @return array */ protected function addExtraReturnData(TaskLogEntity $taskLogEntity) { return []; } /** * @param Report $report * @return Report[] */ protected function getPlainReport(Report $report) { $reports[] = $report; if ($report->hasChildren()) { foreach ($report as $r) { $reports = array_merge($reports, $this->getPlainReport($r)); } } return $reports; } /** * @param Report $report * @return array */ protected function getReportAsAssociativeArray(Report $report) { $reports = []; $plainReports = $this->getPlainReport($report); foreach ($plainReports as $r) { $reports[] = [ 'type' => $r->getType(), 'message' => $r->getMessage(), ]; } return $reports; } }