Passed
Push — master ( 0675d7...59fbae )
by Kevin
02:33
created

CompoundTask::addMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task;
4
5
use Zenstruck\ScheduleBundle\Schedule\Task;
6
7
/**
8
 * @author Kevin Bond <[email protected]>
9
 */
10
final class CompoundTask extends Task implements \IteratorAggregate
11
{
12
    /** @var Task[] */
13
    private $tasks = [];
14
15 6
    public function __construct()
16
    {
17 6
        parent::__construct('compound task');
18 6
    }
19
20 6
    public function add(Task $task): self
21
    {
22 6
        if ($task instanceof self) {
23 1
            throw new \LogicException('Cannot nest compound tasks.');
24
        }
25
26 5
        $this->tasks[] = $task;
27
28 5
        return $this;
29
    }
30
31
    /**
32
     * @see CommandTask::__construct()
33
     *
34
     * @param string|null $description optional description
35
     */
36 2
    public function addCommand(string $name, array $arguments = [], ?string $description = null): self
37
    {
38 2
        return $this->addWithDescription(new CommandTask($name, ...$arguments), $description);
39
    }
40
41
    /**
42
     * @see CallbackTask::__construct()
43
     *
44
     * @param string|null $description optional description
45
     */
46 2
    public function addCallback(callable $callback, ?string $description = null): self
47
    {
48 2
        return $this->addWithDescription(new CallbackTask($callback), $description);
49
    }
50
51
    /**
52
     * @see ProcessTask::__construct()
53
     *
54
     * @param string|null $description optional description
55
     */
56 2
    public function addProcess($process, ?string $description = null): self
57
    {
58 2
        return $this->addWithDescription(new ProcessTask($process), $description);
59
    }
60
61
    /**
62
     * @see PingTask::__construct()
63
     *
64
     * @param string|null $description optional description
65
     */
66 1
    public function addPing(string $url, string $method = 'GET', array $options = [], ?string $description = null): self
67
    {
68 1
        return $this->addWithDescription(new PingTask($url, $method, $options), $description);
69
    }
70
71
    /**
72
     * @see MessageTask::__construct()
73
     *
74
     * @param string|null $description optional description
75
     */
76 1
    public function addMessage(object $message, array $stamps = [], ?string $description = null): self
77
    {
78 1
        return $this->addWithDescription(new MessageTask($message, $stamps), $description);
79
    }
80
81
    /**
82
     * @return Task[]
83
     */
84 5
    public function getIterator(): iterable
85
    {
86 5
        foreach ($this->tasks as $task) {
87 5
            $task->cron($this->getExpression());
88
89 5
            if ($this->getTimezone()) {
90 1
                $task->timezone($this->getTimezone());
91
            }
92
93 5
            foreach ($this->getExtensions() as $extension) {
94 4
                $task->addExtension($extension);
95
            }
96
97 5
            yield $task;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $task returns the type Generator which is incompatible with the documented return type Zenstruck\ScheduleBundle\Schedule\Task[].
Loading history...
98
        }
99 5
    }
100
101 2
    private function addWithDescription(Task $task, ?string $description = null): self
102
    {
103 2
        if ($description) {
104 2
            $task->description($description);
105
        }
106
107 2
        return $this->add($task);
108
    }
109
}
110