Passed
Push — master ( 9ed588...0002a7 )
by Kevin
01:39
created

CompoundTask::addNull()   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 1
crap 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task;
4
5
use Symfony\Component\Process\Process;
6
use Zenstruck\ScheduleBundle\Schedule\Task;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class CompoundTask extends Task implements \IteratorAggregate
12
{
13
    /** @var Task[] */
14
    private $tasks = [];
15
16 3
    public function __construct()
17
    {
18 3
        parent::__construct('compound task');
19 3
    }
20
21 3
    public function add(Task $task): self
22
    {
23 3
        if ($task instanceof self) {
24 1
            throw new \LogicException('Cannot nest compound tasks.');
25
        }
26
27 2
        $this->tasks[] = $task;
28
29 2
        return $this;
30
    }
31
32
    /**
33
     * @param string $name Command class or name (my:command)
34
     */
35 1
    public function addCommand(string $name, array $arguments = [], string $description = null): self
36
    {
37 1
        return $this->addWithDescription(new CommandTask($name, ...$arguments), $description);
38
    }
39
40
    /**
41
     * @param callable $callback Return value is considered "output"
42
     */
43 1
    public function addCallback(callable $callback, string $description = null): self
44
    {
45 1
        return $this->addWithDescription(new CallbackTask($callback), $description);
46
    }
47
48
    /**
49
     * @param string|Process $process
50
     */
51 1
    public function addProcess($process, string $description = null): self
52
    {
53 1
        return $this->addWithDescription(new ProcessTask($process), $description);
54
    }
55
56 1
    public function addNull(string $description): self
57
    {
58 1
        return $this->add(new NullTask($description));
59
    }
60
61
    /**
62
     * @return Task[]
63
     */
64 2
    public function getIterator(): \Generator
65
    {
66 2
        foreach ($this->tasks as $task) {
67 2
            $task->cron($this->getExpression());
68
69 2
            if ($this->getTimezone()) {
70 1
                $task->timezone($this->getTimezone()->getName());
71
            }
72
73 2
            foreach ($this->getExtensions() as $extension) {
74 1
                $task->addExtension($extension);
75
            }
76
77 2
            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...
78
        }
79 2
    }
80
81 1
    private function addWithDescription(Task $task, string $description = null): self
82
    {
83 1
        if ($description) {
84 1
            $task->description($description);
85
        }
86
87 1
        return $this->add($task);
88
    }
89
}
90