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

Job::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 7
Bugs 0 Features 1
Metric Value
eloc 7
c 7
b 0
f 1
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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