Completed
Push — master ( 7235dd...b44091 )
by Sergii
05:14
created

Router   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Test Coverage

Coverage 85.11%

Importance

Changes 0
Metric Value
dl 0
loc 293
ccs 40
cts 47
cp 0.8511
rs 10
c 0
b 0
f 0
wmc 17

14 Methods

Rating   Name   Duplication   Size   Complexity  
A addSubscriber() 0 3 1
A addRoute() 0 3 1
A __construct() 0 10 1
A onRouterStop() 0 3 1
A getClient() 0 3 1
A onConnectionOpen() 0 3 1
A onConnectionClose() 0 3 1
A removeEvent() 0 6 3
A setClient() 0 3 1
A getControllerNamespace() 0 3 1
A onRouterStart() 0 3 1
A addEvent() 0 11 2
A setControllerNamespace() 0 3 1
A parseGroups() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 *
5
 * @author Donii Sergii <[email protected]>
6
 * Date: 10/25/17
7
 * Time: 11:56 AM
8
 */
9
10
namespace sonrac\WAMP\Routers;
11
12
use sonrac\WAMP\Contracts\WAMPRouterInterface;
13
use Thruway\Event\ConnectionOpenEvent;
14
use Thruway\Peer\ClientInterface;
15
use Thruway\Peer\Router as PeerRouter;
16
17
/**
18
 * Class Router
19
 * Router class.
20
 *
21
 * <h2>Events list</h2>
22
 * <table class="table table-bordered table-stripped table-hover table-responsive">
23
 * <thead>
24
 * <tr>
25
 * <td>Event name</td>
26
 * <td>Constant name</td>
27
 * <td>Description</td>
28
 * <td>Method for subscribe (is static for class <i>sonrac\WAMP\Routers\Router</i>)</td>
29
 * </tr>
30
 * </thead>
31
 * <tbody>
32
 * <tr>
33
 * <td>connection_open</td>
34
 * <td><i>sonrac\WAMP\Routers\Router::EVENT_CONNECTION_OPEN</i></td>
35
 * <td>Connection opened</td>
36
 * <td><i>sonrac\WAMP\Routers\Router::onOpen</i></td>
37
 * </tr>
38
 * </tbody>
39
 * </table>
40
 *
41
 * <h2>Example events adding</h2>
42
 *
43
 * <code>
44
 * $app->router->onOpen()
45
 * </code>
46
 */
47
class Router extends PeerRouter implements WAMPRouterInterface
48
{
49
    use RouterTrait;
50
51
    /**
52
     * Connection open event name.
53
     *
54
     * @var string
55
     * @const
56
     */
57
    const EVENT_CONNECTION_OPEN = 'connection_open';
58
59
    /**
60
     * Connection close event name.
61
     *
62
     * @var string
63
     * @const
64
     */
65
    const EVENT_CONNECTION_CLOSE = 'connection_close';
66
67
    /**
68
     * Router start event name.
69
     *
70
     * @var string
71
     * @const
72
     */
73
    const EVENT_ROUTER_START = 'router.start';
74
75
    /**
76
     * Router stop event name.
77
     *
78
     * @var string
79
     * @const
80
     */
81
    const EVENT_ROUTER_STOP = 'router.stop';
82
83
    /**
84
     * RPC router.
85
     *
86
     * @var \sonrac\WAMP\Contracts\RPCRouterInterface
87
     */
88
    protected $rpcRouter;
89
90
    /**
91
     * Publisher/subscription router.
92
     *
93
     * @var \sonrac\WAMP\Contracts\PubSubRouterInterface
94
     */
95
    protected $pubSubRouter;
96
97
    /**
98
     * Loop object.
99
     *
100
     * @var null|\React\EventLoop\LoopInterface
101
     *
102
     * @author Donii Sergii <[email protected]>
103
     */
104
    protected $loop;
105
    /**
106
     * Routes.
107
     *
108
     * @var array
109
     */
110
    protected $routes = [];
111
112
    /**
113
     * Peer client.
114
     *
115
     * @var \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client
116
     *
117
     * @author Donii Sergii <[email protected]>
118
     */
119
    protected $client = null;
120
121
    /**
122
     * Default controller namespaces.
123
     *
124
     * @var null|string
125
     *
126
     * @author Donii Sergii <[email protected]>
127
     */
128
    protected $controllerNamespace = null;
129
130
    /**
131
     * Events.
132
     *
133
     * @var array
134
     *
135
     * @author Donii Sergii <[email protected]>
136
     */
137
    protected static $events = [
138
    ];
139
140
    /**
141
     * Router constructor.
142
     *
143
     * @param \sonrac\WAMP\Contracts\RPCRouterInterface|\sonrac\WAMP\Routers\RPCRouter       $RPCRouter    RPC router
144
     * @param \sonrac\WAMP\Contracts\PubSubRouterInterface|\sonrac\WAMP\Routers\PubSubRouter $pubSubRouter Publisher/subscription
145
     *                                                                                                     router
146
     * @param \React\EventLoop\LoopInterface|null                                            $loop         Loop object
147
     */
148 21
    public function __construct(
149
        \sonrac\WAMP\Contracts\RPCRouterInterface $RPCRouter,
150
        \sonrac\WAMP\Contracts\PubSubRouterInterface $pubSubRouter,
151
        \React\EventLoop\LoopInterface $loop = null
152
    ) {
153 21
        $this->rpcRouter = $RPCRouter;
154 21
        $this->pubSubRouter = $pubSubRouter;
155 21
        $this->loop = $loop;
156
157 21
        parent::__construct($loop);
158 21
    }
159
160
    /**
161
     * Add subscriber.
162
     *
163
     * @param string          $path     Route path
164
     * @param \Closure|string $callback Handler
165
     *
166
     * @return \React\Promise\Promise
167
     */
168 1
    public function addSubscriber($path, $callback)
169
    {
170 1
        return $this->pubSubRouter->addRoute($path, $callback);
0 ignored issues
show
Bug introduced by
The method addRoute() does not exist on sonrac\WAMP\Contracts\PubSubRouterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to sonrac\WAMP\Contracts\PubSubRouterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
        return $this->pubSubRouter->/** @scrutinizer ignore-call */ addRoute($path, $callback);
Loading history...
171
    }
172
173
    /**
174
     * Add procedure.
175
     *
176
     * @param string          $name      Name
177
     * @param string|\Closure $procedure Procedure
178
     *
179
     * @return \React\Promise\Promise
180
     *
181
     * @author Donii Sergii <[email protected]>
182
     */
183
    public function addRoute($name, $procedure)
184
    {
185
        return $this->rpcRouter->addRoute($name, $procedure);
0 ignored issues
show
Bug introduced by
The method addRoute() does not exist on sonrac\WAMP\Contracts\RPCRouterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to sonrac\WAMP\Contracts\RPCRouterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
        return $this->rpcRouter->/** @scrutinizer ignore-call */ addRoute($name, $procedure);
Loading history...
186
    }
187
188
    /**
189
     * Set default controller namespace.
190
     *
191
     * @param string $namespace
192
     *
193
     * @author Donii Sergii <[email protected]>
194
     */
195 1
    public function setControllerNamespace($namespace)
196
    {
197 1
        $this->controllerNamespace = $namespace;
198 1
    }
199
200
    /**
201
     * Get default controller namespace.
202
     *
203
     * @return null|string
204
     *
205
     * @author Donii Sergii <[email protected]>
206
     */
207 2
    public function getControllerNamespace()
208
    {
209 2
        return $this->controllerNamespace;
210
    }
211
212
    /**
213
     * Add open event listener.
214
     *
215
     * @param \Closure|string $callback Callback
216
     * @param int             $priority Callback priority
217
     *
218
     * @author Donii Sergii <[email protected]>
219
     */
220 6
    public function onConnectionOpen($callback, $priority = 0)
221
    {
222 6
        $this->addEvent($callback, static::EVENT_CONNECTION_OPEN, $priority);
223 6
    }
224
225
    /**
226
     * Add stop router event listener.
227
     *
228
     * @param \Closure|string $callback Callback
229
     * @param int             $priority Callback priority
230
     *
231
     * @throws \Exception
232
     *
233
     * @author Donii Sergii <[email protected]>
234
     */
235 1
    public function onRouterStop($callback, $priority = 0)
236
    {
237 1
        $this->addEvent($callback, static::EVENT_ROUTER_STOP, $priority);
238 1
    }
239
240
    /**
241
     * Add connection close event listener.
242
     *
243
     * @param \Closure|string $callback Callback
244
     * @param int             $priority Callback priority
245
     *
246
     * @throws \Exception
247
     *
248
     * @author Donii Sergii <[email protected]>
249
     */
250 1
    public function onConnectionClose($callback, $priority = 0)
251
    {
252 1
        $this->addEvent($callback, static::EVENT_CONNECTION_CLOSE, $priority);
253 1
    }
254
255
    /**
256
     * Remove event listener.
257
     *
258
     * @param string $eventName Event name
259
     * @param mixed  $callback  Callback
260
     *
261
     * @author Donii Sergii <[email protected]>
262
     */
263 7
    public function removeEvent($eventName, $callback)
264
    {
265 7
        $this->getEventDispatcher()->removeListener($eventName, $callback);
266
267 7
        if (isset(static::$events[$eventName]) && count(static::$events[$eventName]) === 0) {
268
            unset(static::$events[$eventName]);
269
        }
270 7
    }
271
272
    /**
273
     * Add open event listener.
274
     *
275
     * @param \Closure|string $callback Callback
276
     * @param int             $priority Callback priority
277
     *
278
     * @throws \Exception
279
     *
280
     * @author Donii Sergii <[email protected]>
281
     */
282 1
    public function onRouterStart($callback, $priority = 0)
283
    {
284 1
        $this->addEvent($callback, static::EVENT_ROUTER_START, $priority);
285 1
    }
286
287
    /**
288
     * Get client.
289
     *
290
     * @return \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client
291
     *
292
     * @author Donii Sergii <[email protected]>
293
     */
294 7
    public function getClient()
295
    {
296 7
        return $this->client;
297
    }
298
299
    /**
300
     * Set client
301
     *
302
     * @param \Thruway\Peer\ClientInterface $client Client
303
     *
304
     * @author Donii Sergii <[email protected]>
305
     */
306 7
    public function setClient(ClientInterface $client)
307
    {
308 7
        $this->client = $client;
309 7
    }
310
311
    /**
312
     * {@inheritdoc}
313
     */
314 1
    public function parseGroups()
315
    {
316 1
        $this->rpcRouter->parseGroups();
0 ignored issues
show
Bug introduced by
The method parseGroups() does not exist on sonrac\WAMP\Contracts\RPCRouterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to sonrac\WAMP\Contracts\RPCRouterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

316
        $this->rpcRouter->/** @scrutinizer ignore-call */ 
317
                          parseGroups();
Loading history...
317 1
        $this->pubSubRouter->parseGroups();
0 ignored issues
show
Bug introduced by
The method parseGroups() does not exist on sonrac\WAMP\Contracts\PubSubRouterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to sonrac\WAMP\Contracts\PubSubRouterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

317
        $this->pubSubRouter->/** @scrutinizer ignore-call */ 
318
                             parseGroups();
Loading history...
318 1
    }
319
320
    /**
321
     * Add event.
322
     *
323
     * @param \Closure|string $callback  Callback
324
     * @param string          $eventName Event name
325
     * @param int             $priority  Priority
326
     *
327
     * @author Donii Sergii <[email protected]>
328
     */
329 9
    protected function addEvent($callback, $eventName, $priority)
330
    {
331 9
        if (is_string($callback)) {
332
            list($class, $method) = explode('&', $callback);
333
334
            $callback = function (ConnectionOpenEvent $event) use ($class, $method) {
335
                return $class::{$method}($event);
336
            };
337
        }
338
339
        $this->getEventDispatcher()->addListener($eventName, $callback, $priority);
340
    }
341
}
342