-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathTaskLog.php
91 lines (79 loc) · 1.93 KB
/
TaskLog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Tasks.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Tasks;
use CodeIgniter\I18n\Time;
use Exception;
use Throwable;
/**
* @property ?Throwable $error
* @property ?string $output
* @property Time $runStart
* @property Task $task
*/
class TaskLog
{
protected Task $task;
protected ?string $output = null;
protected Time $runStart;
protected Time $runEnd;
/**
* The exception thrown during execution, if any.
*/
protected ?Throwable $error = null;
/**
* TaskLog constructor.
*/
public function __construct(array $data)
{
foreach ($data as $key => $value) {
if ($key === 'output') {
$this->output = $this->setOutput($value);
} elseif (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
}
/**
* Returns the duration of the task in seconds and fractions of a second.
*
* @return string
*
* @throws Exception
*/
public function duration()
{
return number_format((float) $this->runEnd->format('U.u') - (float) $this->runStart->format('U.u'), 2);
}
/**
* Magic getter.
*/
public function __get(string $key)
{
if (property_exists($this, $key)) {
return $this->{$key};
}
}
/**
* Unify output to string.
*
* @param array<int, string>|bool|int|string|null $value
*/
private function setOutput($value): ?string
{
if (is_string($value) || $value === null) {
return $value;
}
if (is_array($value)) {
return implode(PHP_EOL, $value);
}
return (string) $value;
}
}