MergeCommand   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 83
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimaryCommand() 0 3 1
A count() 0 3 1
A getCommands() 0 3 1
A getDatabase() 0 3 1
A addCommand() 0 3 1
A __construct() 0 12 4
A execute() 0 2 1
A isReady() 0 3 1
A isExecuted() 0 3 1
A getIterator() 0 14 5
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\ORM\Command\StoreCommand;
9
use Cycle\ORM\Command\StoreCommandInterface;
10
use Cycle\Database\DatabaseInterface;
11
12
/**
13
 * Wraps multiple commands and merge into one.
14
 */
15
final class MergeCommand implements CommandInterface, \IteratorAggregate, \Countable
16
{
17
    private StoreCommandInterface $primary;
18
19
    /** @var CommandInterface[] */
20
    private array $commands = [];
21
22
    /**
23
     * @param StoreCommandInterface $primary
24
     */
25
    public function __construct(CommandInterface $primary)
26
    {
27
        if ($primary instanceof Sequence || $primary instanceof self) {
28
            $primary = $primary->getPrimaryCommand();
29
        }
30
        if (!$primary instanceof StoreCommandInterface) {
31
            throw new \InvalidArgumentException(sprintf(
32
                'Parameter `$primary` must be instance of %s.',
33
                StoreCommandInterface::class
34
            ));
35
        }
36
        $this->primary = $primary;
37
    }
38
39
    public function getPrimaryCommand(): StoreCommandInterface
40
    {
41
        return $this->primary;
42
    }
43
44
    public function isExecuted(): bool
45
    {
46
        return $this->primary->isExecuted();
47
    }
48
49
    public function isReady(): bool
50
    {
51
        return $this->primary->isReady();
52
    }
53
54
    public function addCommand(StoreCommandInterface $command): void
55
    {
56
        $this->commands[] = $command;
57
    }
58
59
    /**
60
     * Get array of underlying commands.
61
     *
62
     * @return CommandInterface[]
63
     */
64
    public function getCommands(): array
65
    {
66
        return $this->commands;
67
    }
68
69
    public function getIterator(): \Generator
70
    {
71
        // Build primary command
72
        foreach ($this->commands as $command) {
73
            if (!$command instanceof StoreCommand || $command->getDatabase() !== $this->primary->getDatabase()) {
74
                continue;
75
            }
76
            foreach ($command->getStoreData() as $column => $value) {
77
                $this->primary->registerColumn($column, $value);
78
            }
79
            $command->setDatabase(null);
80
        }
81
        yield $this->primary;
82
        yield from $this->commands;
83
    }
84
85
    public function count(): int
86
    {
87
        return \count($this->commands);
88
    }
89
90
    public function execute(): void
91
    {
92
        // nothing
93
    }
94
95
    public function getDatabase(): ?DatabaseInterface
96
    {
97
        return $this->primary->getDatabase();
98
    }
99
}
100