Completed
Pull Request — master (#115)
by Šimon
01:10
created

BusBuilders   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 60
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A defaultBus() 0 4 1
A get() 0 6 1
A getIterator() 0 4 1
A assertValidBusId() 0 6 2
A add() 0 10 2
1
<?php
2
declare(strict_types=1);
3
4
namespace League\Tactician\Bundle\DependencyInjection\Compiler\BusBuilder;
5
6
use ArrayIterator;
7
use IteratorAggregate;
8
use League\Tactician\Bundle\DependencyInjection\DuplicatedCommandBusId;
9
use League\Tactician\Bundle\DependencyInjection\InvalidCommandBusId;
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 defaultBus(): BusBuilder
34
    {
35 78
        return $this->get($this->defaultBusId);
36
    }
37
38 78
    private function get(string $busId): BusBuilder
39
    {
40 78
        $this->assertValidBusId($busId);
41
42
        return $this->busBuilders[$busId];
43 78
    }
44
45 78
    /**
46
     * @return ArrayIterator|BusBuilder[]
47 78
     */
48
    public function getIterator()
49
    {
50
        return new ArrayIterator($this->busBuilders);
51
    }
52
53 84
    private function assertValidBusId($busId) : void
54
    {
55 84
        if (!isset($this->busBuilders[$busId])) {
56
            throw InvalidCommandBusId::ofName($busId, array_keys($this->busBuilders));
57
        }
58 99
    }
59
60 99
    private function add(BusBuilder $builder) : void
61 3
    {
62
        $id = $builder->id();
63 96
64
        if (isset($this->busBuilders[$id])) {
65 102
            throw DuplicatedCommandBusId::withId($id);
66
        }
67 102
68
        $this->busBuilders[$id] = $builder;
69 102
    }
70
}
71