Completed
Push — master ( 8b0725...4a307c )
by Sergii
05:41
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\Router as PeerRouter;
15
16
/**
17
 * Class Router
18
 * Router class
19
 *
20
 * <h2>Events list</h2>
21
 * <table class="table table-bordered table-stripped table-hover table-responsive">
22
 * <thead>
23
 * <tr>
24
 * <td>Event name</td>
25
 * <td>Constant name</td>
26
 * <td>Description</td>
27
 * <td>Method for subscribe (is static for class <i>sonrac\WAMP\Routers\Router</i>)</td>
28
 * </tr>
29
 * </thead>
30
 * <tbody>
31
 * <tr>
32
 * <td>connection_open</td>
33
 * <td><i>sonrac\WAMP\Routers\Router::EVENT_CONNECTION_OPEN</i></td>
34
 * <td>Connection opened</td>
35
 * <td><i>sonrac\WAMP\Routers\Router::onOpen</i></td>
36
 * </tr>
37
 * </tbody>
38
 * </table>
39
 *
40
 * <h2>Example events adding</h2>
41
 *
42
 * <code>
43
 * $app->router->onOpen()
44
 * </code>
45
 *
46
 * @package sonrac\WAMP\Routers
47
 */
48
class Router extends PeerRouter implements WAMPRouterInterface
49
{
50
    use RouterTrait;
51
52
    const EVENT_CONNECTION_OPEN = 'connection_open';
53
    const EVENT_CONNECTION_CLOSE = 'connection_close';
54
    const EVENT_ROUTER_START = 'router.start';
55
    const EVENT_ROUTER_STOP = 'router.stop';
56
57
    /**
58
     * RPC router
59
     *
60
     * @var \sonrac\WAMP\Contracts\RPCRouterInterface
61
     */
62
    protected $rpcRouter;
63
64
    /**
65
     * Publisher/subscription router
66
     *
67
     * @var \sonrac\WAMP\Contracts\PubSubRouterInterface
68
     */
69
    protected $pubSubRouter;
70
71
    /**
72
     * Loop object
73
     *
74
     * @var null|\React\EventLoop\LoopInterface
75
     *
76
     * @author Donii Sergii <[email protected]>
77
     */
78
    protected $loop;
79
    /**
80
     * Routes
81
     *
82
     * @var array
83
     */
84
    protected $routes = [];
85
86
    /**
87
     * Events
88
     *
89
     * @var array
90
     *
91
     * @author Donii Sergii <[email protected]>
92
     */
93
    protected static $events = [
94
        self::EVENT_CONNECTION_OPEN  => ['', 10],
95
        self::EVENT_CONNECTION_CLOSE => ['', 10],
96
        self::EVENT_ROUTER_START     => ['', 10],
97
        self::EVENT_ROUTER_STOP      => ['', 10],
98
    ];
99
100
    /**
101
     * Router constructor.
102
     *
103
     * @param \sonrac\WAMP\Contracts\RPCRouterInterface    $RPCRouter    RPC router
104
     * @param \sonrac\WAMP\Contracts\PubSubRouterInterface $pubSubRouter Publisher/subscription router
105
     * @param \React\EventLoop\LoopInterface|null          $loop         Loop object
106
     */
107
    public function __construct(
108
        \sonrac\WAMP\Contracts\RPCRouterInterface $RPCRouter,
109
        \sonrac\WAMP\Contracts\PubSubRouterInterface $pubSubRouter,
110
        \React\EventLoop\LoopInterface $loop = null
111
    ) {
112
        $this->rpcRouter = $RPCRouter;
113
        $this->pubSubRouter = $pubSubRouter;
114
        $this->loop = $loop;
115
116
        parent::__construct($loop);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public static function getSubscribedEvents()
123
    {
124
        return static::$events;
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130
    public function dispatch()
131
    {
132
    }
133
134
    /**
135
     * Add route to RPC router
136
     *
137
     * @param string          $path     Route path
138
     * @param \Closure|string $callback Handler
139
     */
140
    public function addRPCRoute($path, $callback)
141
    {
142
        $this->routes[$path] = $callback;
143
    }
144
145
    /**
146
     * Add open event listener
147
     *
148
     * @param \Closure|string $callback
149
     *
150
     * @throws \Exception
151
     *
152
     * @author Donii Sergii <[email protected]>
153
     */
154
    public static function onOpen($callback)
155
    {
156
        static::checkEventKeyExistsOrCreate(self::EVENT_CONNECTION_OPEN);
157
158
        if (is_string($callback)) {
159
            list($class, $method) = explode('&', $callback);
160
161
            $callback = function (ConnectionOpenEvent $event) use ($class, $method) {
162
                $class = app()->make($class);
163
164
                return $class->{$method}($event);
165
            };
166
        }
167
168
        if (!($callback instanceof \Closure)) {
169
            throw new \Exception('Invalid callback');
170
        }
171
172
        static::$events[self::EVENT_CONNECTION_OPEN][] = [$callback, 10];
173
    }
174
175
    /**
176
     * Check event exists in array or exists
177
     *
178
     * @param string $key     Key
179
     * @param mixed  $default Default value
180
     *
181
     * @author Donii Sergii <[email protected]>
182
     */
183
    private static function checkEventKeyExistsOrCreate($key, $default = null)
184
    {
185
        if (!isset(static::$events[$key])) {
186
            static::$events[$key] = $default ? [$default] : [];
187
        }
188
    }
189
}
190