Passed
Push — master ( 7b0bcd...6035dc )
by Jakub
02:12
created

Job   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 1 Features 1
Metric Value
eloc 24
c 10
b 1
f 1
dl 0
loc 57
rs 10
ccs 20
cts 20
cp 1
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOutput() 0 3 1
A getResult() 0 3 1
A __construct() 0 12 1
A execute() 0 11 2
A getCallback() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MyTester;
6
7
/**
8
 * One job of the test suite
9
 *
10
 * @author Jakub Konečný
11
 * @property-read callable $callback
12
 * @property-read JobResult $result
13
 * @property-read string $output @internal
14
 * @method void onAfterExecute()
15
 */
16
final class Job
17
{
18
    use \Nette\SmartObject;
19
20
    public readonly string $name;
21
    /** @var callable Task */
22
    protected $callback;
23
    public readonly array $params;
24
    public readonly bool|string $skip;
25
    protected JobResult $result = JobResult::PASSED;
26
    protected string $output = "";
27
    /** @var callable[] */
28
    public array $onAfterExecute = [];
29
30 1
    public function __construct(
31
        string $name,
32
        callable $callback,
33
        array $params = [],
34
        bool|string $skip = false,
35
        array $onAfterExecute = []
36
    ) {
37 1
        $this->name = $name;
38 1
        $this->callback = $callback;
39 1
        $this->params = $params;
40 1
        $this->skip = $skip;
41 1
        $this->onAfterExecute = $onAfterExecute;
42
    }
43
44 1
    protected function getCallback(): callable
45
    {
46 1
        return $this->callback;
47
    }
48
49 1
    protected function getResult(): JobResult
50
    {
51 1
        return $this->result;
52
    }
53
54 1
    protected function getOutput(): string
55
    {
56 1
        return $this->output;
57
    }
58
59
    /**
60
     * Executes the task
61
     */
62 1
    public function execute(): void
63
    {
64 1
        if (!$this->skip) {
65 1
            ob_start();
66 1
            call_user_func_array($this->callback, $this->params);
67 1
            $this->onAfterExecute();
68
            /** @var string $output */
69 1
            $output = ob_get_clean();
70 1
            $this->output = $output;
71
        }
72 1
        $this->result = JobResult::fromJob($this);
73
    }
74
}
75