Bus::assertValidObjectHandler()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 1
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\LogicException;
14
use Borobudur\Bus\Message\MessageInterface;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     8/17/15
19
 */
20
class Bus implements BusInterface
21
{
22
    /**
23
     * @var GateInterface
24
     */
25
    protected $gate;
26
27
    /**
28
     * @var mixed
29
     */
30
    protected $handler;
31
32
    /**
33
     * @var array
34
     */
35
    protected $messages = array();
36
37
    /**
38
     * @var string
39
     */
40
    protected $method = 'handle';
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getName()
46
    {
47
        return 'bus';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function setMethod($method)
54
    {
55
        $this->method = $method;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getGate()
62
    {
63
        return $this->gate;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function setGate(GateInterface $gate)
70
    {
71
        $this->gate = $gate;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function setHandler($handler)
78
    {
79
        $this->handler = $handler;
80
    }
81
82
    /**
83
     * Register messages.
84
     *
85
     * @param array $messages
86
     */
87
    public function registers(array $messages)
88
    {
89
        foreach ($messages as $message => $handler) {
90
            $this->register($message, $handler);
91
        }
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function register($message, $handler)
98
    {
99
        $this->messages[ltrim($message, '\\')] = $handler;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function isRegistered(MessageInterface $message)
106
    {
107
        return array_key_exists(get_class($message), $this->messages);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function dispatch(MessageInterface $message)
114
    {
115
        return $this->dispatchNow($message);
116
    }
117
    
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function dispatchNow(MessageInterface $message)
122
    {
123
        $class = ltrim(get_class($message), '\\');
124
        if (!isset($this->messages[$class])) {
125
            throw new LogicException(sprintf('Message "%s" does not have handler.', $class));
126
        }
127
    
128
        return $this->invoke($message, $this->messages[$class], $class);
129
    }
130
131
    /**
132
     * Invoke message.
133
     *
134
     * @param MessageInterface $message
135
     * @param mixed            $handler
136
     * @param string           $class
137
     *
138
     * @return mixed
139
     */
140
    protected function invoke(MessageInterface $message, $handler, $class)
141
    {
142
        if (null !== $this->handler) {
143
            return call_user_func_array($this->handler, func_get_args());
144
        }
145
146
        $handler = $this->buildHandler($handler);
147
        $this->assertValidHandler($class, $handler);
148
149
        return $this->handle($message, $handler);
150
    }
151
152
    /**
153
     * Default dispatcher for message.
154
     *
155
     * @param MessageInterface $message
156
     * @param mixed            $handler
157
     *
158
     * @return mixed
159
     */
160
    protected function handle(MessageInterface $message, $handler)
161
    {
162
        if (!is_callable($handler)) {
163
            return call_user_func(array($handler, $this->method), $message);
164
        }
165
166
        return call_user_func($handler, $message);
167
    }
168
169
    /**
170
     * Assert valid handler.
171
     *
172
     * @param string $class
173
     * @param mixed  $handler
174
     */
175
    protected function assertValidHandler($class, $handler)
176
    {
177
        if (!is_callable($handler) && !is_object($handler)) {
178
            throw new LogicException(sprintf(
179
                'Handler for message "%s" should be callable or object, but got "%s".',
180
                $class,
181
                gettype($handler)
182
            ));
183
        }
184
185
        $this->assertValidObjectHandler($handler);
186
    }
187
188
    /**
189
     * Assert valid object handler.
190
     *
191
     * @param mixed $handler
192
     */
193
    protected function assertValidObjectHandler($handler)
194
    {
195
        if (!is_callable($handler) && !method_exists($handler, $this->method)) {
196
            throw new LogicException(sprintf('Object handler should have method "->%s()".', $this->method));
197
        }
198
    }
199
200
    /**
201
     * Build class from string.
202
     *
203
     * @param string $class
204
     *
205
     * @return mixed|null
206
     */
207
    protected function buildClassString($class)
208
    {
209
        if (class_exists($class)) {
210
            return new $class();
211
        }
212
213
        return null;
214
    }
215
216
    /**
217
     * Build handler.
218
     *
219
     * @param mixed $handler
220
     *
221
     * @return mixed|null
222
     */
223
    protected function buildHandler($handler)
224
    {
225
        if (is_callable($handler)) {
226
            return $handler;
227
        }
228
229
        if (is_string($handler)) {
230
            $class = '\\' . ltrim($handler, '\\');
231
            if (null !== $object = $this->buildClassString($class)) {
232
                return $object;
233
            }
234
        }
235
236
        return $handler;
237
    }
238
}
239