Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
41.67% |
15 / 36 |
|
60.00% |
6 / 10 |
CRAP | |
0.00% |
0 / 1 |
| TaskLogActionTrait | |
41.67% |
15 / 36 |
|
60.00% |
6 / 10 |
74.37 | |
0.00% |
0 / 1 |
| getServiceManager | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| returnJson | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getTaskLogEntity | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getUserId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTaskLogReturnData | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| returnTaskJson | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| getTaskId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTaskStatus | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTaskReport | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addExtraReturnData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPlainReport | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getReportAsAssociativeArray | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoTaskQueue\model; |
| 23 | |
| 24 | use common_report_Report as Report; |
| 25 | use oat\oatbox\service\ServiceManager; |
| 26 | use oat\taoTaskQueue\model\Entity\TaskLogEntity; |
| 27 | use oat\taoTaskQueue\model\Task\TaskInterface; |
| 28 | |
| 29 | /** |
| 30 | * Helper trait for actions/controllers to operate with task log data for a given task. |
| 31 | * |
| 32 | * @deprecated Use \oat\tao\model\taskQueue\TaskLogActionTrait |
| 33 | * |
| 34 | * @author Gyula Szucs <gyula@taotesting.com> |
| 35 | */ |
| 36 | trait TaskLogActionTrait |
| 37 | { |
| 38 | /** |
| 39 | * @return ServiceManager |
| 40 | */ |
| 41 | abstract protected function getServiceManager(); |
| 42 | |
| 43 | /** |
| 44 | * @param array $data |
| 45 | * @param int $httpStatus |
| 46 | * @return mixed |
| 47 | */ |
| 48 | abstract protected function returnJson($data, $httpStatus = 200); |
| 49 | |
| 50 | /** |
| 51 | * @param string $taskId |
| 52 | * @param string $userId |
| 53 | * @return TaskLogEntity |
| 54 | */ |
| 55 | protected function getTaskLogEntity($taskId, $userId = null) |
| 56 | { |
| 57 | /** @var TaskLogInterface $taskLog */ |
| 58 | $taskLog = $this->getServiceManager()->get(TaskLogInterface::SERVICE_ID); |
| 59 | |
| 60 | if (is_null($userId)) { |
| 61 | $userId = $this->getUserId(); |
| 62 | } |
| 63 | |
| 64 | return $taskLog->getByIdAndUser((string) $taskId, (string) $userId); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get default user id. |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | protected function getUserId() |
| 73 | { |
| 74 | return \common_session_SessionManager::getSession()->getUserUri(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param string $taskId |
| 79 | * @param string|null $forcedTaskType |
| 80 | * @param string|null $userId |
| 81 | * @return array |
| 82 | * @throws \common_exception_BadRequest |
| 83 | */ |
| 84 | protected function getTaskLogReturnData($taskId, $forcedTaskType = null, $userId = null) |
| 85 | { |
| 86 | $taskLogEntity = $this->getTaskLogEntity($taskId, $userId); |
| 87 | |
| 88 | if (!is_null($forcedTaskType) && $taskLogEntity->getTaskName() !== $forcedTaskType) { |
| 89 | throw new \common_exception_BadRequest("Wrong task type"); |
| 90 | } |
| 91 | |
| 92 | $result['id'] = $this->getTaskId($taskLogEntity); |
| 93 | $result['status'] = $this->getTaskStatus($taskLogEntity); |
| 94 | $result['report'] = $taskLogEntity->getReport() ? $this->getTaskReport($taskLogEntity) : []; |
| 95 | |
| 96 | return array_merge($result, (array) $this->addExtraReturnData($taskLogEntity)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns task data in a specific data structure required by the front-end component. |
| 101 | * |
| 102 | * @param TaskInterface $task |
| 103 | * @param array $extraData |
| 104 | * @return mixed |
| 105 | */ |
| 106 | protected function returnTaskJson(TaskInterface $task, array $extraData = []) |
| 107 | { |
| 108 | return $this->returnJson([ |
| 109 | 'success' => true, |
| 110 | 'data' => [ |
| 111 | 'extra' => $extraData, |
| 112 | 'task' => $this->getTaskLogReturnData($task->getId()) |
| 113 | ] |
| 114 | ]); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return task identifier |
| 119 | * |
| 120 | * @param TaskLogEntity $taskLogEntity |
| 121 | * @return string |
| 122 | */ |
| 123 | protected function getTaskId(TaskLogEntity $taskLogEntity) |
| 124 | { |
| 125 | return $taskLogEntity->getId(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param TaskLogEntity $taskLogEntity |
| 130 | * @return string |
| 131 | */ |
| 132 | protected function getTaskStatus(TaskLogEntity $taskLogEntity) |
| 133 | { |
| 134 | return $taskLogEntity->getStatus()->getLabel(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * As default, it returns the reports as an associative array. |
| 139 | * |
| 140 | * @param TaskLogEntity $taskLogEntity |
| 141 | * @return array |
| 142 | */ |
| 143 | protected function getTaskReport(TaskLogEntity $taskLogEntity) |
| 144 | { |
| 145 | return $this->getReportAsAssociativeArray($taskLogEntity->getReport()); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @return array |
| 150 | */ |
| 151 | protected function addExtraReturnData(TaskLogEntity $taskLogEntity) |
| 152 | { |
| 153 | return []; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @param Report $report |
| 158 | * @return Report[] |
| 159 | */ |
| 160 | protected function getPlainReport(Report $report) |
| 161 | { |
| 162 | $reports[] = $report; |
| 163 | |
| 164 | if ($report->hasChildren()) { |
| 165 | foreach ($report as $r) { |
| 166 | $reports = array_merge($reports, $this->getPlainReport($r)); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return $reports; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @param Report $report |
| 175 | * @return array |
| 176 | */ |
| 177 | protected function getReportAsAssociativeArray(Report $report) |
| 178 | { |
| 179 | $reports = []; |
| 180 | $plainReports = $this->getPlainReport($report); |
| 181 | |
| 182 | foreach ($plainReports as $r) { |
| 183 | $reports[] = [ |
| 184 | 'type' => $r->getType(), |
| 185 | 'message' => $r->getMessage(), |
| 186 | ]; |
| 187 | } |
| 188 | |
| 189 | return $reports; |
| 190 | } |
| 191 | } |