Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
TaskLogActionTrait | |
0.00% |
0 / 37 |
|
0.00% |
0 / 10 |
306 | |
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 | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getUserId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTaskLogReturnData | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
returnTaskJson | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getTaskId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTaskStatus | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTaskReport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addExtraReturnData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
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\tao\model\taskQueue; |
23 | |
24 | use common_report_Report as Report; |
25 | use oat\oatbox\service\ServiceManager; |
26 | use oat\tao\model\taskQueue\Task\TaskInterface; |
27 | use oat\tao\model\taskQueue\TaskLog\Entity\EntityInterface; |
28 | |
29 | /** |
30 | * Helper trait for actions/controllers to operate with task log data for a given task. |
31 | * |
32 | * @author Gyula Szucs <gyula@taotesting.com> |
33 | */ |
34 | trait TaskLogActionTrait |
35 | { |
36 | /** |
37 | * @return ServiceManager |
38 | */ |
39 | abstract protected function getServiceManager(); |
40 | |
41 | /** |
42 | * @param array $data |
43 | * @param int $httpStatus |
44 | * @return mixed |
45 | */ |
46 | abstract protected function returnJson($data, $httpStatus = 200); |
47 | |
48 | /** |
49 | * @param string $taskId |
50 | * @param string $userId |
51 | * @return EntityInterface |
52 | */ |
53 | protected function getTaskLogEntity($taskId, $userId = null) |
54 | { |
55 | /** @var TaskLogInterface $taskLog */ |
56 | $taskLog = $this->getServiceManager()->get(TaskLogInterface::SERVICE_ID); |
57 | |
58 | if (is_null($userId)) { |
59 | $userId = $this->getUserId(); |
60 | } |
61 | |
62 | return $taskLog->getByIdAndUser((string) $taskId, (string) $userId); |
63 | } |
64 | |
65 | /** |
66 | * Get default user id. |
67 | * |
68 | * @return string |
69 | */ |
70 | protected function getUserId() |
71 | { |
72 | return \common_session_SessionManager::getSession()->getUserUri(); |
73 | } |
74 | |
75 | /** |
76 | * @param string $taskId |
77 | * @param string|null $forcedTaskType |
78 | * @param string|null $userId |
79 | * @return array |
80 | * @throws \common_exception_BadRequest |
81 | */ |
82 | protected function getTaskLogReturnData($taskId, $forcedTaskType = null, $userId = null) |
83 | { |
84 | $taskLogEntity = $this->getTaskLogEntity($taskId, $userId); |
85 | |
86 | if (!is_null($forcedTaskType) && $taskLogEntity->getTaskName() !== $forcedTaskType) { |
87 | throw new \common_exception_BadRequest("Wrong task type"); |
88 | } |
89 | |
90 | $result['id'] = $this->getTaskId($taskLogEntity); |
91 | $result['status'] = $this->getTaskStatus($taskLogEntity); |
92 | $result['report'] = $taskLogEntity->getReport() ? $this->getTaskReport($taskLogEntity) : []; |
93 | |
94 | return array_merge($result, (array) $this->addExtraReturnData($taskLogEntity)); |
95 | } |
96 | |
97 | /** |
98 | * Returns task data in a specific data structure required by the front-end component. |
99 | * |
100 | * @param TaskInterface $task |
101 | * @param array $extraData |
102 | * @return mixed |
103 | */ |
104 | protected function returnTaskJson(TaskInterface $task, array $extraData = []) |
105 | { |
106 | return $this->returnJson([ |
107 | 'success' => true, |
108 | 'data' => [ |
109 | 'extra' => $extraData, |
110 | 'task' => $this->getTaskLogReturnData($task->getId()) |
111 | ] |
112 | ]); |
113 | } |
114 | |
115 | /** |
116 | * Return task identifier |
117 | * |
118 | * @param EntityInterface $taskLogEntity |
119 | * @return string |
120 | */ |
121 | protected function getTaskId($taskLogEntity) |
122 | { |
123 | return $taskLogEntity->getId(); |
124 | } |
125 | |
126 | /** |
127 | * @param EntityInterface $taskLogEntity |
128 | * @return string |
129 | */ |
130 | protected function getTaskStatus($taskLogEntity) |
131 | { |
132 | return $taskLogEntity->getStatus()->getLabel(); |
133 | } |
134 | |
135 | /** |
136 | * As default, it returns the reports as an associative array. |
137 | * |
138 | * @param EntityInterface $taskLogEntity |
139 | * @return array |
140 | */ |
141 | protected function getTaskReport($taskLogEntity) |
142 | { |
143 | return $this->getReportAsAssociativeArray($taskLogEntity->getReport()); |
144 | } |
145 | |
146 | /** |
147 | * @return array |
148 | */ |
149 | protected function addExtraReturnData(EntityInterface $taskLogEntity) |
150 | { |
151 | return []; |
152 | } |
153 | |
154 | /** |
155 | * @param Report $report |
156 | * @return Report[] |
157 | */ |
158 | protected function getPlainReport(Report $report) |
159 | { |
160 | $reports[] = $report; |
161 | |
162 | if ($report->hasChildren()) { |
163 | foreach ($report as $r) { |
164 | $reports = array_merge($reports, $this->getPlainReport($r)); |
165 | } |
166 | } |
167 | |
168 | return $reports; |
169 | } |
170 | |
171 | /** |
172 | * @param Report $report |
173 | * @return array |
174 | */ |
175 | protected function getReportAsAssociativeArray(Report $report) |
176 | { |
177 | $reports = []; |
178 | $plainReports = $this->getPlainReport($report); |
179 | |
180 | foreach ($plainReports as $r) { |
181 | $reports[] = [ |
182 | 'type' => $r->getType(), |
183 | 'message' => $r->getMessage(), |
184 | ]; |
185 | } |
186 | |
187 | return $reports; |
188 | } |
189 | } |