TaskRunContext   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 19
c 1
b 0
f 0
dl 0
loc 71
ccs 25
cts 25
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 5 1
A setResult() 0 11 2
A __toString() 0 3 1
A isFailure() 0 3 1
A getTask() 0 3 1
A getScheduleRunContext() 0 3 1
A isSuccessful() 0 3 1
A __construct() 0 6 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task;
4
5
use Zenstruck\ScheduleBundle\Schedule\RunContext;
6
use Zenstruck\ScheduleBundle\Schedule\ScheduleRunContext;
7
use Zenstruck\ScheduleBundle\Schedule\Task;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
final class TaskRunContext extends RunContext
13
{
14
    /** @var ScheduleRunContext */
15
    private $scheduleRunContext;
16
17
    /** @var Task */
18
    private $task;
19 68
20
    /** @var Result */
21 68
    private $result;
22 68
23
    public function __construct(ScheduleRunContext $scheduleRunContext, Task $task)
24 68
    {
25 68
        $this->scheduleRunContext = $scheduleRunContext;
26
        $this->task = $task;
27 1
28
        parent::__construct();
29 1
    }
30
31
    public function __toString(): string
32 23
    {
33
        return (string) $this->getTask();
34 23
    }
35
36
    public function getScheduleRunContext(): ScheduleRunContext
37 68
    {
38
        return $this->scheduleRunContext;
39 68
    }
40
41
    public function getTask(): Task
42
    {
43
        return $this->task;
44
    }
45 64
46
    /**
47 64
     * @throws \LogicException if has not yet run
48
     */
49 63
    public function getResult(): Result
50
    {
51
        $this->ensureHasRun();
52 65
53
        return $this->result;
54 65
    }
55
56 65
    public function setResult(Result $result): void
57 1
    {
58
        $resultTask = $result->getTask();
59
60 64
        if ($resultTask->getId() !== $this->getTask()->getId()) {
61
            throw new \LogicException("The result's task ({$resultTask}) does not match the context's task ({$this->getTask()}).");
62 64
        }
63 64
64
        $this->markAsRun(\memory_get_usage(true));
65
66
        $this->result = $result;
67
    }
68 63
69
    /**
70 63
     * @throws \LogicException if has not yet run
71
     */
72
    public function isSuccessful(): bool
73
    {
74
        return $this->getResult()->isSuccessful();
75
    }
76 63
77
    /**
78 63
     * @throws \LogicException if has not yet run
79
     */
80
    public function isFailure(): bool
81
    {
82
        return $this->getResult()->isFailure();
83
    }
84
}
85