BusProxy   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 131
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setMethod() 0 4 1
A getName() 0 4 1
A setGate() 0 4 1
A getGate() 0 4 1
A setHandler() 0 4 1
A isRegistered() 0 4 1
A registers() 0 4 1
A register() 0 4 1
A dispatch() 0 8 2
A dispatchNow() 0 8 2
A dispatchSystemMessage() 0 15 3
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\Message\MessageInterface;
14
15
/**
16
 * @author      Iqbal Maulana <[email protected]>
17
 * @created     8/17/15
18
 */
19
class BusProxy implements BusInterface
20
{
21
    /**
22
     * @var GateInterface
23
     */
24
    private $gate;
25
26
    /**
27
     * @var BusInterface
28
     */
29
    private $bus;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param BusInterface $bus
35
     */
36
    public function __construct(BusInterface $bus)
37
    {
38
        $this->bus = $bus;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function setMethod($method)
45
    {
46
        $this->bus->setMethod($method);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getName()
53
    {
54
        return $this->bus->getName();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function setGate(GateInterface $gate)
61
    {
62
        $this->gate = $gate;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getGate()
69
    {
70
        return $this->gate;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function setHandler($handler)
77
    {
78
        $this->bus->setHandler($handler);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function isRegistered(MessageInterface $message)
85
    {
86
        return $this->bus->isRegistered($message);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function registers(array $messages)
93
    {
94
        $this->bus->registers($messages);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function register($message, $handler)
101
    {
102
        $this->bus->register($message, $handler);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function dispatch(MessageInterface $message)
109
    {
110
        if ($this->gate->has(BusInterface::SYSTEM_BUS)) {
111
            $this->dispatchSystemMessage($message);
112
        }
113
114
        return $this->bus->dispatch($message);
115
    }
116
    
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function dispatchNow(MessageInterface $message)
121
    {
122
        if ($this->gate->has(BusInterface::SYSTEM_BUS)) {
123
            $this->dispatchSystemMessage($message);
124
        }
125
    
126
        return $this->bus->dispatchNow($message);
127
    }
128
129
    /**
130
     * Dispatch system message.
131
     *
132
     * @param MessageInterface $message
133
     */
134
    private function dispatchSystemMessage(MessageInterface &$message)
135
    {
136
        $systemBus = $this->gate->get(BusInterface::SYSTEM_BUS);
137
        $system = $this->gate->getSystemMessage();
138
139
        if ($systemBus->isRegistered($system)) {
140
            $system->setMessage($message);
141
            $system->setBus($this->bus);
142
            $systemBus->dispatch($system);
143
        }
144
145
        if ($systemBus->isRegistered($message)) {
146
            $systemBus->dispatch($message);
147
        }
148
    }
149
}
150