Completed
Pull Request — master (#67)
by Ross
05:39
created

BusBuilders   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 45
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getBusIds() 0 4 1
A defaultBus() 0 4 1
A get() 0 8 2
A getIterator() 0 4 1
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->defaultBusId = $defaultBusId;
27
    }
28
29
    public function getBusIds(): array
30
    {
31
        return array_keys($this->busBuilders);
32
    }
33
34
    public function defaultBus(): BusBuilder
35
    {
36
        return $this->get($this->defaultBusId);
37
    }
38
39
    private function get(string $busId): BusBuilder
40
    {
41
        if (!isset($this->busBuilders[$busId])) {
42
            throw InvalidCommandBusId::ofName($busId, array_keys($this->busBuilders));
43
        }
44
45
        return $this->busBuilders[$busId];
46
    }
47
48
    public function getIterator()
49
    {
50
        return new \ArrayIterator($this->busBuilders);
51
    }
52
}