BusBuilders::createBlankRouting()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace League\Tactician\Bundle\DependencyInjection\Compiler\BusBuilder;
5
6
use League\Tactician\Bundle\DependencyInjection\DuplicatedCommandBusId;
7
use League\Tactician\Bundle\DependencyInjection\HandlerMapping\Routing;
8
use League\Tactician\Bundle\DependencyInjection\InvalidCommandBusId;
9
use ArrayIterator;
10
11
final class BusBuilders implements \IteratorAggregate
12
{
13
    /**
14
     * @var BusBuilder[]
15
     */
16
    private $busBuilders = [];
17
18
    /**
19
     * @var string
20
     */
21
    private $defaultBusId;
22
23 102
    public function __construct(array $busBuilders, string $defaultBusId)
24
    {
25 102
        foreach ($busBuilders as $builder) {
26 102
            $this->add($builder);
27
        }
28
29 99
        $this->assertValidBusId($defaultBusId);
30 96
        $this->defaultBusId = $defaultBusId;
31 96
    }
32
33 78
    public function createBlankRouting(): Routing
34
    {
35 78
        return new Routing(array_keys($this->busBuilders));
36
    }
37
38 78
    public function defaultBus(): BusBuilder
39
    {
40 78
        return $this->get($this->defaultBusId);
41
    }
42
43 78
    private function get(string $busId): BusBuilder
44
    {
45 78
        $this->assertValidBusId($busId);
46
47 78
        return $this->busBuilders[$busId];
48
    }
49
50
    /**
51
     * @return ArrayIterator|BusBuilder[]
52
     */
53 84
    public function getIterator()
54
    {
55 84
        return new ArrayIterator($this->busBuilders);
56
    }
57
58 99
    private function assertValidBusId($busId)
59
    {
60 99
        if (!isset($this->busBuilders[$busId])) {
61 3
            throw InvalidCommandBusId::ofName($busId, array_keys($this->busBuilders));
62
        }
63 96
    }
64
65 102
    private function add(BusBuilder $builder)
66
    {
67 102
        $id = $builder->id();
68
69 102
        if (isset($this->busBuilders[$id])) {
70 3
            throw DuplicatedCommandBusId::withId($id);
71
        }
72
73 102
        $this->busBuilders[$id] = $builder;
74 102
    }
75
}
76