Passed
Push — master ( 8ecb46...0675d7 )
by Kevin
03:22 queued 22s
created

CompoundTask::addPing()   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 4
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
     * @return Task[]
73
     */
74 5
    public function getIterator(): iterable
75
    {
76 5
        foreach ($this->tasks as $task) {
77 5
            $task->cron($this->getExpression());
78
79 5
            if ($this->getTimezone()) {
80 1
                $task->timezone($this->getTimezone());
81
            }
82
83 5
            foreach ($this->getExtensions() as $extension) {
84 4
                $task->addExtension($extension);
85
            }
86
87 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...
88
        }
89 5
    }
90
91 2
    private function addWithDescription(Task $task, ?string $description = null): self
92
    {
93 2
        if ($description) {
94 2
            $task->description($description);
95
        }
96
97 2
        return $this->add($task);
98
    }
99
}
100