Completed
Push — master ( c12270...10319f )
by Sergii
04:52
created

Router::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
     * Events.
123
     *
124
     * @var array
125
     *
126
     * @author Donii Sergii <[email protected]>
127
     */
128
    protected static $events = [
129
        self::EVENT_ROUTER_START     => [['handleRouterStart', 10]],
130
        self::EVENT_ROUTER_STOP      => [['handleRouterStop', 10]],
131
    ];
132
133
    /**
134
     * Router constructor.
135
     *
136
     * @param \sonrac\WAMP\Contracts\RPCRouterInterface    $RPCRouter    RPC router
137
     * @param \sonrac\WAMP\Contracts\PubSubRouterInterface $pubSubRouter Publisher/subscription router
138
     * @param \React\EventLoop\LoopInterface|null          $loop         Loop object
139
     */
140 5
    public function __construct(
141
        \sonrac\WAMP\Contracts\RPCRouterInterface $RPCRouter,
142
        \sonrac\WAMP\Contracts\PubSubRouterInterface $pubSubRouter,
143
        \React\EventLoop\LoopInterface $loop = null
144
    ) {
145 5
        $this->rpcRouter = $RPCRouter;
146 5
        $this->pubSubRouter = $pubSubRouter;
147 5
        $this->loop = $loop;
148
149 5
        parent::__construct($loop);
150 5
    }
151
152
    /**
153
     * Register all events, subscribers, publisher & procedures.
154
     *
155
     * @author Donii Sergii <[email protected]>
156
     */
157
    public function register()
158
    {
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 5
    public static function getSubscribedEvents()
165
    {
166 5
        return static::$events;
167
    }
168
169
    /**
170
     * Add route to RPC router.
171
     *
172
     * @param string          $path     Route path
173
     * @param \Closure|string $callback Handler
174
     */
175
    public function addRPCRoute($path, $callback)
176
    {
177
        $this->getClient()->getSession()->subscribe($path, $callback);
178
    }
179
180
    /**
181
     * Add procedure.
182
     *
183
     * @author Donii Sergii <[email protected]>
184
     */
185
    public function addRoute($name, $procedure)
186
    {
187
        $this->getClient()->getSession()->register($name, $procedure);
188
    }
189
190
    /**
191
     * Add open event listener.
192
     *
193
     * @param \Closure|string $callback Callback
194
     * @param int             $priority Callback priority
195
     *
196
     * @author Donii Sergii <[email protected]>
197
     */
198 3
    public function onConnectionOpen($callback, $priority = 0)
199
    {
200 3
        $this->addEvent($callback, static::EVENT_CONNECTION_OPEN, $priority);
201 3
    }
202
203
    /**
204
     * Add stop router event listener.
205
     *
206
     * @param \Closure|string $callback Callback
207
     * @param int             $priority Callback priority
208
     *
209
     * @throws \Exception
210
     *
211
     * @author Donii Sergii <[email protected]>
212
     */
213
    public function onRouterStop($callback, $priority = 0)
214
    {
215
        $this->addEvent($callback, static::EVENT_ROUTER_STOP, $priority);
216
    }
217
218
    /**
219
     * Add connection close event listener.
220
     *
221
     * @param \Closure|string $callback Callback
222
     * @param int             $priority Callback priority
223
     *
224
     * @throws \Exception
225
     *
226
     * @author Donii Sergii <[email protected]>
227
     */
228
    public function onConnectionClose($callback, $priority = 0)
229
    {
230
        $this->addEvent($callback, static::EVENT_CONNECTION_CLOSE, $priority);
231
    }
232
233
    /**
234
     * Remove event listener.
235
     *
236
     * @param string $eventName Event name
237
     * @param mixed  $callback  Callback
238
     *
239
     * @author Donii Sergii <[email protected]>
240
     */
241 3
    public function removeEvent($eventName, $callback)
242
    {
243 3
        $this->getEventDispatcher()->removeListener($eventName, $callback);
244
245 3
        if (count(static::$events[$eventName]) === 0) {
246 3
            unset(static::$events[$eventName]);
247
        }
248 3
    }
249
250
    /**
251
     * Handle start router.
252
     *
253
     * @author Donii Sergii <[email protected]>
254
     */
255
    public static function handleRouterStart()
256
    {
257
    }
258
259
    /**
260
     * Handle start router.
261
     */
262
    public static function handleRouterStop()
263
    {
264
    }
265
266
    /**
267
     * Add open event listener.
268
     *
269
     * @param \Closure|string $callback Callback
270
     * @param int             $priority Callback priority
271
     *
272
     * @throws \Exception
273
     *
274
     * @author Donii Sergii <[email protected]>
275
     */
276
    public function onRouterStart($callback, $priority = 0)
277
    {
278
        $this->addEvent($callback, static::EVENT_ROUTER_START, $priority);
279
    }
280
281
    /**
282
     * Add event.
283
     *
284
     * @param \Closure|string $callback  Callback
285
     * @param string          $eventName Event name
286
     * @param int             $priority  Priority
287
     *
288
     * @author Donii Sergii <[email protected]>
289
     */
290 3
    protected function addEvent($callback, $eventName, $priority)
291
    {
292 3
        $this->checkEventKeyExistsOrCreate($eventName);
293
294 3
        if (is_string($callback)) {
295
            list($class, $method) = explode('&', $callback);
296
297
            $callback = function (ConnectionOpenEvent $event) use ($class, $method) {
298
                return $class::{$method}($event);
299
            };
300
        }
301
302 3
        $this->getEventDispatcher()->addListener($eventName, $callback, $priority);
303 3
    }
304
305
    /**
306
     * Get client.
307
     *
308
     * @return \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client
309
     *
310
     * @author Donii Sergii <[email protected]>
311
     */
312 2
    public function getClient()
313
    {
314 2
        return $this->client;
315
    }
316
317
    /**
318
     * Set client
319
     *
320
     * @param \Thruway\Peer\ClientInterface $client Client
321
     *
322
     * @author Donii Sergii <[email protected]>
323
     */
324 2
    public function setClient(ClientInterface $client)
325
    {
326 2
        $this->client = $client;
327 2
    }
328
329
    /**
330
     * Check event exists in array or exists.
331
     *
332
     * @param string $key     Key
333
     * @param mixed  $default Default value
334
     *
335
     * @author Donii Sergii <[email protected]>
336
     */
337 3
    private function checkEventKeyExistsOrCreate($key, $default = null)
338
    {
339 3
        if (!isset(static::$events[$key])) {
340 3
            static::$events[$key] = $default ? [$default] : [];
341
        }
342 3
    }
343
}
344