Completed
Push — master ( 9bf1e4...223d51 )
by Sergii
04:53
created

Router::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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