Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.08% |
68 / 79 |
|
81.25% |
13 / 16 |
CRAP | |
0.00% |
0 / 1 |
| TaskLogEntity | |
86.08% |
68 / 79 |
|
81.25% |
13 / 16 |
31.27 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| createFromArray | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
6 | |||
| getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParentId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTaskName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParameters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLabel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOwner | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getReport | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCreatedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUpdatedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStatus | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isMasterStatus | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFileNameFromReport | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
5 | |||
| 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-2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoTaskQueue\model\Entity; |
| 23 | |
| 24 | use common_exception_Error; |
| 25 | use common_report_Report as Report; |
| 26 | use DateTime; |
| 27 | use oat\oatbox\reporting\Report as NewReport; |
| 28 | use oat\tao\model\taskQueue\TaskLog\Entity\TaskLogEntity as BaseTaskLogEntity; |
| 29 | use oat\taoTaskQueue\model\TaskLogBroker\TaskLogBrokerInterface; |
| 30 | use oat\taoTaskQueue\model\ValueObjects\TaskLogCategorizedStatus; |
| 31 | |
| 32 | /** |
| 33 | * @deprecated Use \oat\tao\model\taskQueue\TaskLog\Entity\TaskLogEntity |
| 34 | */ |
| 35 | class TaskLogEntity extends BaseTaskLogEntity implements TaskLogEntityInterface |
| 36 | { |
| 37 | /** @var string */ |
| 38 | private $id; |
| 39 | |
| 40 | /** @var string */ |
| 41 | private $parentId; |
| 42 | |
| 43 | /** @var string */ |
| 44 | private $taskName; |
| 45 | |
| 46 | /** @var array */ |
| 47 | private $parameters; |
| 48 | |
| 49 | /** @var string */ |
| 50 | private $label; |
| 51 | |
| 52 | /** @var TaskLogCategorizedStatus */ |
| 53 | private $status; |
| 54 | |
| 55 | /** @var bool */ |
| 56 | private $masterStatus; |
| 57 | |
| 58 | /** @var string */ |
| 59 | private $owner; |
| 60 | |
| 61 | /** @var Report */ |
| 62 | private $report; |
| 63 | |
| 64 | /** @var DateTime */ |
| 65 | private $createdAt; |
| 66 | |
| 67 | /** @var DateTime */ |
| 68 | private $updatedAt; |
| 69 | |
| 70 | /** |
| 71 | * TaskLogEntity constructor. |
| 72 | * |
| 73 | * @param string $id |
| 74 | * @param string $parentId |
| 75 | * @param string $taskName |
| 76 | * @param TaskLogCategorizedStatus $status |
| 77 | * @param boolean $masterStatus |
| 78 | * @param array $parameters |
| 79 | * @param string $label |
| 80 | * @param string $owner |
| 81 | * @param DateTime|null $createdAt |
| 82 | * @param DateTime|null $updatedAt |
| 83 | * @param Report|null $report |
| 84 | */ |
| 85 | public function __construct( |
| 86 | $id, |
| 87 | $parentId, |
| 88 | $taskName, |
| 89 | TaskLogCategorizedStatus $status, |
| 90 | array $parameters, |
| 91 | $label, |
| 92 | $owner, |
| 93 | DateTime $createdAt = null, |
| 94 | DateTime $updatedAt = null, |
| 95 | Report $report = null, |
| 96 | $masterStatus = false |
| 97 | ) { |
| 98 | $this->id = $id; |
| 99 | $this->parentId = $parentId; |
| 100 | $this->taskName = $taskName; |
| 101 | $this->status = $status; |
| 102 | $this->parameters = $parameters; |
| 103 | $this->label = $label; |
| 104 | $this->owner = $owner; |
| 105 | $this->report = $report; |
| 106 | $this->createdAt = $createdAt; |
| 107 | $this->updatedAt = $updatedAt; |
| 108 | $this->masterStatus = $masterStatus; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param array $row |
| 113 | * @param string $dateFormat |
| 114 | * @return TaskLogEntity |
| 115 | * @throws common_exception_Error |
| 116 | */ |
| 117 | public static function createFromArray(array $row, string $dateFormat = 'Y-m-d H:i:s') |
| 118 | { |
| 119 | return new self( |
| 120 | $row[TaskLogBrokerInterface::COLUMN_ID], |
| 121 | $row[TaskLogBrokerInterface::COLUMN_PARENT_ID], |
| 122 | $row[TaskLogBrokerInterface::COLUMN_TASK_NAME], |
| 123 | TaskLogCategorizedStatus::createFromString($row[TaskLogBrokerInterface::COLUMN_STATUS]), |
| 124 | isset($row[TaskLogBrokerInterface::COLUMN_PARAMETERS]) |
| 125 | ? json_decode($row[TaskLogBrokerInterface::COLUMN_PARAMETERS], true) |
| 126 | : [], |
| 127 | isset($row[TaskLogBrokerInterface::COLUMN_LABEL]) ? $row[TaskLogBrokerInterface::COLUMN_LABEL] : '', |
| 128 | isset($row[TaskLogBrokerInterface::COLUMN_OWNER]) ? $row[TaskLogBrokerInterface::COLUMN_OWNER] : '', |
| 129 | isset($row[TaskLogBrokerInterface::COLUMN_CREATED_AT]) |
| 130 | ? DateTime::createFromFormat( |
| 131 | $dateFormat, |
| 132 | $row[TaskLogBrokerInterface::COLUMN_CREATED_AT], |
| 133 | new \DateTimeZone('UTC') |
| 134 | ) |
| 135 | : null, |
| 136 | isset($row[TaskLogBrokerInterface::COLUMN_UPDATED_AT]) |
| 137 | ? DateTime::createFromFormat( |
| 138 | $dateFormat, |
| 139 | $row[TaskLogBrokerInterface::COLUMN_UPDATED_AT], |
| 140 | new \DateTimeZone('UTC') |
| 141 | ) |
| 142 | : null, |
| 143 | NewReport::jsonUnserialize($row[TaskLogBrokerInterface::COLUMN_REPORT]), |
| 144 | $row[TaskLogBrokerInterface::COLUMN_MASTER_STATUS] ?? false |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @return string |
| 150 | */ |
| 151 | public function getId() |
| 152 | { |
| 153 | return $this->id; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return string |
| 158 | */ |
| 159 | public function getParentId() |
| 160 | { |
| 161 | return $this->parentId; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @return string |
| 166 | */ |
| 167 | public function getTaskName() |
| 168 | { |
| 169 | return $this->taskName; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @return array |
| 174 | */ |
| 175 | public function getParameters() |
| 176 | { |
| 177 | return $this->parameters; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @return string |
| 182 | */ |
| 183 | public function getLabel() |
| 184 | { |
| 185 | return $this->label; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @return string |
| 190 | */ |
| 191 | public function getOwner() |
| 192 | { |
| 193 | return $this->owner; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @return Report|null |
| 198 | */ |
| 199 | public function getReport() |
| 200 | { |
| 201 | return $this->report; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @return DateTime|null |
| 206 | */ |
| 207 | public function getCreatedAt() |
| 208 | { |
| 209 | return $this->createdAt; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @return DateTime|null |
| 214 | */ |
| 215 | public function getUpdatedAt() |
| 216 | { |
| 217 | return $this->updatedAt; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** |
| 222 | * @return TaskLogCategorizedStatus |
| 223 | */ |
| 224 | public function getStatus() |
| 225 | { |
| 226 | return $this->status; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @return boolean |
| 231 | */ |
| 232 | public function isMasterStatus() |
| 233 | { |
| 234 | return (bool) $this->masterStatus; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns the file name from the generated report. |
| 239 | * |
| 240 | * CAUTION: it is not 100% sure that the returned string is really a file name because different reports set |
| 241 | * different values as data. So this return value can be any kind of string. Please check the file whether it exist |
| 242 | * or not before usage. |
| 243 | * |
| 244 | * @return string |
| 245 | */ |
| 246 | public function getFileNameFromReport() |
| 247 | { |
| 248 | $filename = ''; |
| 249 | |
| 250 | if ($this->getStatus()->isFailed() || is_null($this->getReport())) { |
| 251 | return $filename; |
| 252 | } |
| 253 | |
| 254 | /** @var Report $successReport */ |
| 255 | foreach ($this->getReport()->getSuccesses() as $successReport) { |
| 256 | $data = $successReport->getData(); |
| 257 | if (is_string($data)) { |
| 258 | $filename = $data; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return $filename; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @return array |
| 268 | */ |
| 269 | public function jsonSerialize(): array |
| 270 | { |
| 271 | return $this->toArray(); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * @return array |
| 276 | */ |
| 277 | public function toArray() |
| 278 | { |
| 279 | // add basic fields which always have values |
| 280 | $rs = [ |
| 281 | 'id' => $this->id, |
| 282 | 'taskName' => $this->taskName, |
| 283 | 'status' => (string) $this->status, |
| 284 | 'masterStatus' => (bool) $this->masterStatus, |
| 285 | 'statusLabel' => $this->status->getLabel() |
| 286 | ]; |
| 287 | |
| 288 | // add other fields only if they have values |
| 289 | if ($this->label) { |
| 290 | $rs['taskLabel'] = $this->label; |
| 291 | } |
| 292 | |
| 293 | if ($this->createdAt instanceof \DateTime) { |
| 294 | $rs['createdAt'] = $this->createdAt->getTimestamp(); |
| 295 | $rs['createdAtElapsed'] = (new \DateTime('now', new \DateTimeZone('UTC'))) |
| 296 | ->getTimestamp() - $this->createdAt->getTimestamp(); |
| 297 | } |
| 298 | |
| 299 | if ($this->updatedAt instanceof \DateTime) { |
| 300 | $rs['updatedAt'] = $this->updatedAt->getTimestamp(); |
| 301 | $rs['updatedAtElapsed'] = (new \DateTime('now', new \DateTimeZone('UTC'))) |
| 302 | ->getTimestamp() - $this->updatedAt->getTimestamp(); |
| 303 | } |
| 304 | |
| 305 | if ($this->report instanceof Report) { |
| 306 | $rs['report'] = $this->report->toArray(); |
| 307 | } |
| 308 | |
| 309 | return $rs; |
| 310 | } |
| 311 | } |