Gate   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 108
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A enableSystemBus() 0 6 2
A disableSystemBus() 0 4 1
A attach() 0 17 2
A detach() 0 4 1
A get() 0 8 2
A has() 0 4 1
A getSystemMessage() 0 4 1
A assertNotRegisteredBus() 0 6 2
A assertValidSystemBus() 0 6 2
1
<?php
2
/*
3
 * This file is part of the Borobudur-Bus package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Bus;
12
13
use Borobudur\Bus\Exception\InvalidArgumentException;
14
use Borobudur\Bus\Message\SystemMessage;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     8/17/15
19
 */
20
class Gate implements GateInterface
21
{
22
    /**
23
     * @var BusInterface[]
24
     */
25
    private $buses = array();
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function enableSystemBus()
31
    {
32
        if (!isset($this->buses[BusInterface::SYSTEM_BUS])) {
33
            $this->attach(new SystemBus());
34
        }
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function disableSystemBus()
41
    {
42
        $this->detach(BusInterface::SYSTEM_BUS);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function attach(BusInterface $bus)
49
    {
50
        $name = $bus->getName();
51
52
        if (BusInterface::SYSTEM_BUS === $name) {
53
            $this->assertValidSystemBus($bus);
54
            $this->buses[$name] = $bus;
55
56
            return;
57
        }
58
59
        $this->assertNotRegisteredBus($name);
60
61
        $bus = new BusProxy($bus);
62
        $bus->setGate($this);
63
        $this->buses[$name] = $bus;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function detach($busName)
70
    {
71
        unset($this->buses[$busName]);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function get($busName)
78
    {
79
        if (!isset($this->buses[$busName])) {
80
            throw new InvalidArgumentException(sprintf('Bus "%s" is not registered.', $busName));
81
        }
82
83
        return $this->buses[$busName];
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function has($busName)
90
    {
91
        return array_key_exists($busName, $this->buses);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getSystemMessage()
98
    {
99
        return new SystemMessage();
100
    }
101
102
    /**
103
     * Assert that bus with specified name is not registered.
104
     *
105
     * @param string $name
106
     *
107
     * @throws InvalidArgumentException
108
     */
109
    private function assertNotRegisteredBus($name)
110
    {
111
        if (isset($this->buses[$name])) {
112
            throw new InvalidArgumentException(sprintf('Bus with name "%s" is already attached.', $name));
113
        }
114
    }
115
116
    /**
117
     * Assert valid system bus.
118
     *
119
     * @param BusInterface $bus
120
     */
121
    private function assertValidSystemBus(BusInterface $bus)
122
    {
123
        if (false === $bus instanceof SystemBus) {
124
            throw new InvalidArgumentException(sprintf('Bus with name "%s" reserved for system bus.', $bus->getName()));
125
        }
126
    }
127
}
128