Passed
Pull Request — master (#4)
by Kevin
07:28
created

TaskRunContext   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
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 58
ccs 25
cts 25
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A task() 0 3 1
A setResult() 0 11 2
A __toString() 0 3 1
A isFailure() 0 3 1
A result() 0 5 1
A scheduleRunContext() 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
    private $scheduleRunContext;
15
    private $task;
16
17
    private $result;
18
19 54
    public function __construct(ScheduleRunContext $scheduleRunContext, Task $task)
20
    {
21 54
        $this->scheduleRunContext = $scheduleRunContext;
22 54
        $this->task = $task;
23
24 54
        parent::__construct();
25 54
    }
26
27 1
    public function __toString(): string
28
    {
29 1
        return "{$this->task()->getType()}: {$this->task()}";
30
    }
31
32 2
    public function scheduleRunContext(): ScheduleRunContext
33
    {
34 2
        return $this->scheduleRunContext;
35
    }
36
37 44
    public function task(): Task
38
    {
39 44
        return $this->task;
40
    }
41
42 29
    public function result(): Result
43
    {
44 29
        $this->ensureHasRun();
45
46 28
        return $this->result;
47
    }
48
49 29
    public function setResult(Result $result): void
50
    {
51 29
        $resultTask = $result->getTask();
52
53 29
        if ($resultTask->getId() !== $this->task()->getId()) {
54 1
            throw new \LogicException(\sprintf("The result's task (%s: %s) does not match the context's task (%s: %s).", $resultTask->getType(), $resultTask, $this->task()->getType(), $this->task()));
55
        }
56
57 28
        $this->markAsRun(\memory_get_usage(true));
58
59 28
        $this->result = $result;
60 28
    }
61
62 28
    public function isSuccessful(): bool
63
    {
64 28
        return $this->result()->isSuccessful();
65
    }
66
67 28
    public function isFailure(): bool
68
    {
69 28
        return $this->result()->isFailure();
70
    }
71
}
72