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