TaskLog   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 11
eloc 19
c 2
b 1
f 0
dl 0
loc 65
ccs 0
cts 7
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 4
A duration() 0 3 1
A setOutput() 0 10 4
A __get() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of BlitzPHP Tasks.
7
 *
8
 * (c) 2025 Dimitri Sitchet Tomkeu <[email protected]>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13
14
namespace BlitzPHP\Tasks;
15
16
use BlitzPHP\Utilities\Date;
17
use Exception;
18
use Throwable;
19
20
/**
21
 * @property ?Throwable $error
22
 * @property ?string    $output
23
 * @property Date       $runStart
24
 * @property Task       $task
25
 *
26
 * @credit <a href="https://tasks.codeigniter.com">CodeIgniter4 - CodeIgniter\Tasks\TaskLog</a>
27
 */
28
class TaskLog
29
{
30
    protected Task $task;
31
    protected ?string $output = null;
32
    protected Date $runStart;
33
    protected Date $runEnd;
34
35
    /**
36
     * L'exception levée pendant l'exécution, le cas échéant.
37
     */
38
    protected ?Throwable $error = null;
39
40
    /**
41
     * Constructeur TaskLog.
42
     *
43
     * @param array<string,mixed> $data
44
     */
45
    public function __construct(array $data)
46
    {
47
        foreach ($data as $key => $value) {
48
            if ($key === 'output') {
49
                $this->output = $this->setOutput($value);
50
            } elseif (property_exists($this, $key)) {
51
                $this->{$key} = $value;
52
            }
53
        }
54
    }
55
56
    /**
57
     * Renvoie la durée de la tâche en secondes et fractions de seconde.
58
     *
59
     * @throws Exception
60
     */
61
    public function duration(): string
62
    {
63
        return number_format((float) $this->runEnd->format('U.u') - (float) $this->runStart->format('U.u'), 2);
64
    }
65
66
    /**
67
     * Getter magique.
68
     *
69
     * @return mixed
70
     */
71
    public function __get(string $key)
72
    {
73
        if (property_exists($this, $key)) {
74
            return $this->{$key};
75
        }
76
    }
77
78
    /**
79
     * Unifier la sortie en chaîne de caractères.
80
     *
81
     * @param array<int, string>|bool|int|string|null $value
82
     */
83
    private function setOutput($value): ?string
84
    {
85
        if (is_string($value) || $value === null) {
86
            return $value;
87
        }
88
        if (is_array($value)) {
89
            return implode(PHP_EOL, $value);
90
        }
91
92
        return (string) $value;
93
    }
94
}
95