Completed
Push — master ( 59d48e...ffd6fa )
by Sergii
04:52
created

Router::addSubscriber()   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
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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
    ];
130
131
    /**
132
     * Router constructor.
133
     *
134
     * @param \sonrac\WAMP\Contracts\RPCRouterInterface|\sonrac\WAMP\Routers\RPCRouter       $RPCRouter    RPC router
135
     * @param \sonrac\WAMP\Contracts\PubSubRouterInterface|\sonrac\WAMP\Routers\PubSubRouter $pubSubRouter Publisher/subscription
136
     *                                                                                                     router
137
     * @param \React\EventLoop\LoopInterface|null                                            $loop         Loop object
138
     */
139 17
    public function __construct(
140
        \sonrac\WAMP\Contracts\RPCRouterInterface $RPCRouter,
141
        \sonrac\WAMP\Contracts\PubSubRouterInterface $pubSubRouter,
142
        \React\EventLoop\LoopInterface $loop = null
143
    ) {
144 17
        $this->rpcRouter = $RPCRouter;
145 17
        $this->pubSubRouter = $pubSubRouter;
146 17
        $this->loop = $loop;
147
148 17
        parent::__construct($loop);
149 17
    }
150
151
    /**
152
     * Add subscriber.
153
     *
154
     * @param string          $path     Route path
155
     * @param \Closure|string $callback Handler
156
     *
157
     * @return \React\Promise\Promise
158
     */
159
    public function addSubscriber($path, $callback)
160
    {
161
        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

161
        return $this->pubSubRouter->/** @scrutinizer ignore-call */ addRoute($path, $callback);
Loading history...
162
    }
163
164
    /**
165
     * Add procedure.
166
     *
167
     * @param string          $name      Name
168
     * @param string|\Closure $procedure Procedure
169
     *
170
     * @return \React\Promise\Promise
171
     *
172
     * @author Donii Sergii <[email protected]>
173
     */
174 1
    public function addRoute($name, $procedure)
175
    {
176 1
        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

176
        return $this->rpcRouter->/** @scrutinizer ignore-call */ addRoute($name, $procedure);
Loading history...
177
    }
178
179
    /**
180
     * Add open event listener.
181
     *
182
     * @param \Closure|string $callback Callback
183
     * @param int             $priority Callback priority
184
     *
185
     * @author Donii Sergii <[email protected]>
186
     */
187 6
    public function onConnectionOpen($callback, $priority = 0)
188
    {
189 6
        $this->addEvent($callback, static::EVENT_CONNECTION_OPEN, $priority);
190 6
    }
191
192
    /**
193
     * Add stop router event listener.
194
     *
195
     * @param \Closure|string $callback Callback
196
     * @param int             $priority Callback priority
197
     *
198
     * @throws \Exception
199
     *
200
     * @author Donii Sergii <[email protected]>
201
     */
202 1
    public function onRouterStop($callback, $priority = 0)
203
    {
204 1
        $this->addEvent($callback, static::EVENT_ROUTER_STOP, $priority);
205 1
    }
206
207
    /**
208
     * Add connection close event listener.
209
     *
210
     * @param \Closure|string $callback Callback
211
     * @param int             $priority Callback priority
212
     *
213
     * @throws \Exception
214
     *
215
     * @author Donii Sergii <[email protected]>
216
     */
217 1
    public function onConnectionClose($callback, $priority = 0)
218
    {
219 1
        $this->addEvent($callback, static::EVENT_CONNECTION_CLOSE, $priority);
220 1
    }
221
222
    /**
223
     * Remove event listener.
224
     *
225
     * @param string $eventName Event name
226
     * @param mixed  $callback  Callback
227
     *
228
     * @author Donii Sergii <[email protected]>
229
     */
230 7
    public function removeEvent($eventName, $callback)
231
    {
232 7
        $this->getEventDispatcher()->removeListener($eventName, $callback);
233
234 7
        if (isset(static::$events[$eventName]) && count(static::$events[$eventName]) === 0) {
235
            unset(static::$events[$eventName]);
236
        }
237 7
    }
238
239
    /**
240
     * Add open event listener.
241
     *
242
     * @param \Closure|string $callback Callback
243
     * @param int             $priority Callback priority
244
     *
245
     * @throws \Exception
246
     *
247
     * @author Donii Sergii <[email protected]>
248
     */
249 1
    public function onRouterStart($callback, $priority = 0)
250
    {
251 1
        $this->addEvent($callback, static::EVENT_ROUTER_START, $priority);
252 1
    }
253
254
    /**
255
     * Add event.
256
     *
257
     * @param \Closure|string $callback  Callback
258
     * @param string          $eventName Event name
259
     * @param int             $priority  Priority
260
     *
261
     * @author Donii Sergii <[email protected]>
262
     */
263 9
    protected function addEvent($callback, $eventName, $priority)
264
    {
265 9
        if (is_string($callback)) {
266
            list($class, $method) = explode('&', $callback);
267
268
            $callback = function (ConnectionOpenEvent $event) use ($class, $method) {
269
                return $class::{$method}($event);
270
            };
271
        }
272
273 9
        $this->getEventDispatcher()->addListener($eventName, $callback, $priority);
274 9
    }
275
276
    /**
277
     * Get client.
278
     *
279
     * @return \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client
280
     *
281
     * @author Donii Sergii <[email protected]>
282
     */
283 5
    public function getClient()
284
    {
285 5
        return $this->client;
286
    }
287
288
    /**
289
     * Set client
290
     *
291
     * @param \Thruway\Peer\ClientInterface $client Client
292
     *
293
     * @author Donii Sergii <[email protected]>
294
     */
295 6
    public function setClient(ClientInterface $client)
296
    {
297 6
        $this->client = $client;
298 6
    }
299
}
300