Sequence::addCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Command\Special;
6
7
use Cycle\ORM\Command\CommandInterface;
8
use Cycle\Database\DatabaseInterface;
9
10
/**
11
 * Wraps multiple commands into one sequence.
12
 */
13
final class Sequence implements CommandInterface, \IteratorAggregate, \Countable
14
{
15
    /** @var CommandInterface[] */
16
    private array $commands = [];
17
18 172
    public function __construct(
19
        private ?CommandInterface $primary = null,
20
        bool $aggregatePrimary = true,
21 172
    ) {
22
        if ($primary !== null && $aggregatePrimary) {
23
            $this->commands[] = $primary;
24
        }
25
    }
26
27
    public function getPrimaryCommand(): ?CommandInterface
28
    {
29
        return $this->primary;
30
    }
31 2
32
    public function isExecuted(): bool
33 2
    {
34
        foreach ($this->commands as $command) {
35
            if (!$command->isExecuted()) {
36
                return false;
37
            }
38 2
        }
39
        return true;
40
    }
41 2
42
    public function isReady(): bool
43
    {
44 2
        // always ready since check will be delegated to underlying nodes
45
        return true;
46
    }
47 170
48
    public function addCommand(CommandInterface ...$commands): self
49 170
    {
50 170
        foreach ($commands as $command) {
51
            $this->commands[] = $command;
52 170
        }
53
        return $this;
54
    }
55
56
    /**
57
     * Get array of underlying commands.
58
     *
59
     * @return CommandInterface[]
60 2
     */
61
    public function getCommands(): array
62 2
    {
63
        return $this->commands;
64
    }
65 170
66
    public function getIterator(): \Generator
67 170
    {
68 170
        foreach ($this->commands as $command) {
69
            if ($command instanceof \Traversable) {
70
                yield from $command;
71
                continue;
72
            }
73 170
74
            yield $command;
75
        }
76
    }
77 2
78
    public function count(): int
79 2
    {
80
        return \count($this->commands);
81
    }
82
83
    public function execute(): void
84
    {
85
        // nothing
86
    }
87
88
    public function getDatabase(): ?DatabaseInterface
89
    {
90
        return null;
91
    }
92
}
93