Completed
Push — master ( 2bc5ee...3171bd )
by Sergii
04:59
created

Router::addEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.1852

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 11
ccs 2
cts 6
cp 0.3333
crap 3.1852
rs 9.4285
c 0
b 0
f 0
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 20
    public function __construct(
149
        \sonrac\WAMP\Contracts\RPCRouterInterface $RPCRouter,
150
        \sonrac\WAMP\Contracts\PubSubRouterInterface $pubSubRouter,
151
        \React\EventLoop\LoopInterface $loop = null
152
    ) {
153 20
        $this->rpcRouter = $RPCRouter;
154 20
        $this->pubSubRouter = $pubSubRouter;
155 20
        $this->loop = $loop;
156
157 20
        parent::__construct($loop);
158 20
    }
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 1
        $this->controllerNamespace = $namespace;
197 1
    }
198
199
    /**
200
     * Get default controller namespace.
201
     *
202
     * @return null|string
203
     *
204
     * @author Donii Sergii <[email protected]>
205
     */
206 2
    public function getControllerNamespace() {
207 2
        return $this->controllerNamespace;
208
    }
209
210
    /**
211
     * Add open event listener.
212
     *
213
     * @param \Closure|string $callback Callback
214
     * @param int             $priority Callback priority
215
     *
216
     * @author Donii Sergii <[email protected]>
217
     */
218 6
    public function onConnectionOpen($callback, $priority = 0)
219
    {
220 6
        $this->addEvent($callback, static::EVENT_CONNECTION_OPEN, $priority);
221 6
    }
222
223
    /**
224
     * Add stop router event listener.
225
     *
226
     * @param \Closure|string $callback Callback
227
     * @param int             $priority Callback priority
228
     *
229
     * @throws \Exception
230
     *
231
     * @author Donii Sergii <[email protected]>
232
     */
233 1
    public function onRouterStop($callback, $priority = 0)
234
    {
235 1
        $this->addEvent($callback, static::EVENT_ROUTER_STOP, $priority);
236 1
    }
237
238
    /**
239
     * Add connection close event listener.
240
     *
241
     * @param \Closure|string $callback Callback
242
     * @param int             $priority Callback priority
243
     *
244
     * @throws \Exception
245
     *
246
     * @author Donii Sergii <[email protected]>
247
     */
248 1
    public function onConnectionClose($callback, $priority = 0)
249
    {
250 1
        $this->addEvent($callback, static::EVENT_CONNECTION_CLOSE, $priority);
251 1
    }
252
253
    /**
254
     * Remove event listener.
255
     *
256
     * @param string $eventName Event name
257
     * @param mixed  $callback  Callback
258
     *
259
     * @author Donii Sergii <[email protected]>
260
     */
261 7
    public function removeEvent($eventName, $callback)
262
    {
263 7
        $this->getEventDispatcher()->removeListener($eventName, $callback);
264
265 7
        if (isset(static::$events[$eventName]) && count(static::$events[$eventName]) === 0) {
266
            unset(static::$events[$eventName]);
267
        }
268 7
    }
269
270
    /**
271
     * Add open event listener.
272
     *
273
     * @param \Closure|string $callback Callback
274
     * @param int             $priority Callback priority
275
     *
276
     * @throws \Exception
277
     *
278
     * @author Donii Sergii <[email protected]>
279
     */
280 1
    public function onRouterStart($callback, $priority = 0)
281
    {
282 1
        $this->addEvent($callback, static::EVENT_ROUTER_START, $priority);
283 1
    }
284
285
    /**
286
     * Get client.
287
     *
288
     * @return \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client
289
     *
290
     * @author Donii Sergii <[email protected]>
291
     */
292 7
    public function getClient()
293
    {
294 7
        return $this->client;
295
    }
296
297
    /**
298
     * Set client
299
     *
300
     * @param \Thruway\Peer\ClientInterface $client Client
301
     *
302
     * @author Donii Sergii <[email protected]>
303
     */
304 7
    public function setClient(ClientInterface $client)
305
    {
306 7
        $this->client = $client;
307 7
    }
308
309
    /**
310
     * {@inheritdoc}
311
     */
312
    public function parseGroups()
313
    {
314
        $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

314
        $this->rpcRouter->/** @scrutinizer ignore-call */ 
315
                          parseGroups();
Loading history...
315
        $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

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