Completed
Pull Request — master (#67)
by Ross
02:01
created

BusBuilders::defaultBus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace League\Tactician\Bundle\DependencyInjection\Compiler\BusBuilder;
5
6
use League\Tactician\Bundle\DependencyInjection\InvalidCommandBusId;
7
8
final class BusBuilders implements \IteratorAggregate
9
{
10
    /**
11
     * @var BusBuilder[]
12
     */
13
    private $busBuilders = [];
14
15
    /**
16
     * @var string
17
     */
18
    private $defaultBusId;
19
20
    public function __construct(array $busBuilders, string $defaultBusId)
21
    {
22
        foreach ($busBuilders as $builder) {
23
            $this->busBuilders[$builder->id()] = $builder;
24
        }
25
26
        $this->assertValidBusId($defaultBusId);
27
        $this->defaultBusId = $defaultBusId;
28
    }
29
30
    public function getBusIds(): array
31
    {
32
        return array_keys($this->busBuilders);
33
    }
34
35
    public function defaultBus(): BusBuilder
36
    {
37
        return $this->get($this->defaultBusId);
38
    }
39
40
    private function get(string $busId): BusBuilder
41
    {
42
        $this->assertValidBusId($busId);
43
44
        return $this->busBuilders[$busId];
45
    }
46
47
    public function getIterator()
48
    {
49
        return new \ArrayIterator($this->busBuilders);
50
    }
51
52
    private function assertValidBusId($busId)
53
    {
54
        if (!isset($this->busBuilders[$busId])) {
55
            throw InvalidCommandBusId::ofName($busId, array_keys($this->busBuilders));
56
        }
57
    }
58
}
59