Total Complexity | 42 |
Total Lines | 381 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like InteractsWithWebsocket often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InteractsWithWebsocket, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | trait InteractsWithWebsocket |
||
27 | { |
||
28 | /** |
||
29 | * @var boolean |
||
30 | */ |
||
31 | protected $isServerWebsocket = false; |
||
32 | |||
33 | /** |
||
34 | * @var \SwooleTW\Http\Websocket\HandlerContract |
||
35 | */ |
||
36 | protected $websocketHandler; |
||
37 | |||
38 | /** |
||
39 | * @var \SwooleTW\Http\Websocket\Parser |
||
40 | */ |
||
41 | protected $payloadParser; |
||
42 | |||
43 | /** |
||
44 | * @var \SwooleTW\Http\Websocket\Rooms\RoomContract |
||
45 | */ |
||
46 | protected $websocketRoom; |
||
47 | |||
48 | /** |
||
49 | * Websocket server events. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $wsEvents = ['open', 'message', 'close']; |
||
54 | |||
55 | /** |
||
56 | * "onOpen" listener. |
||
57 | * |
||
58 | * @param \Swoole\Websocket\Server $server |
||
59 | * @param \Swoole\Http\Request $swooleRequest |
||
60 | */ |
||
61 | public function onOpen($server, $swooleRequest) |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param \Swoole\Http\Request $swooleRequest |
||
93 | * @param \Swoole\Http\Response $response |
||
94 | * |
||
95 | * @return bool |
||
96 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||
97 | */ |
||
98 | public function onHandShake($swooleRequest, $response) |
||
99 | { |
||
100 | $illuminateRequest = Request::make($swooleRequest)->toIlluminate(); |
||
101 | $websocket = $this->app->make(Websocket::class); |
||
102 | $sandbox = $this->app->make(Sandbox::class); |
||
103 | $handler = $this->container->make('config')->get('swoole_websocket.handshake.handler'); |
||
104 | |||
105 | try { |
||
106 | $websocket->reset(true)->setSender($swooleRequest->fd); |
||
107 | // set currnt request to sandbox |
||
108 | $sandbox->setRequest($illuminateRequest); |
||
109 | // enable sandbox |
||
110 | $sandbox->enable(); |
||
111 | |||
112 | if (! $this->app->make($handler)->handle($swooleRequest, $response)) { |
||
113 | return false; |
||
114 | } |
||
115 | // trigger 'connect' websocket event |
||
116 | if ($websocket->eventExists('connect')) { |
||
117 | // set sandbox container to websocket pipeline |
||
118 | $websocket->setContainer($sandbox->getApplication()); |
||
119 | $websocket->call('connect', $illuminateRequest); |
||
120 | } |
||
121 | |||
122 | return true; |
||
123 | } catch (Throwable $e) { |
||
124 | $this->logServerError($e); |
||
125 | } finally { |
||
126 | // disable and recycle sandbox resource |
||
127 | $sandbox->disable(); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * "onMessage" listener. |
||
133 | * |
||
134 | * @param \Swoole\Websocket\Server $server |
||
135 | * @param \Swoole\Websocket\Frame $frame |
||
136 | */ |
||
137 | public function onMessage($server, $frame) |
||
138 | { |
||
139 | // execute parser strategies and skip non-message packet |
||
140 | if ($this->payloadParser->execute($server, $frame)) { |
||
141 | return; |
||
142 | } |
||
143 | |||
144 | $websocket = $this->app->make(Websocket::class); |
||
145 | $sandbox = $this->app->make(Sandbox::class); |
||
146 | |||
147 | try { |
||
148 | // decode raw message via parser |
||
149 | $payload = $this->payloadParser->decode($frame); |
||
150 | |||
151 | $websocket->reset(true)->setSender($frame->fd); |
||
152 | |||
153 | // enable sandbox |
||
154 | $sandbox->enable(); |
||
155 | |||
156 | // dispatch message to registered event callback |
||
157 | ['event' => $event, 'data' => $data] = $payload; |
||
158 | $websocket->eventExists($event) |
||
159 | ? $websocket->call($event, $data) |
||
160 | : $this->websocketHandler->onMessage($frame); |
||
161 | } catch (Throwable $e) { |
||
162 | $this->logServerError($e); |
||
163 | } finally { |
||
164 | // disable and recycle sandbox resource |
||
165 | $sandbox->disable(); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * "onClose" listener. |
||
171 | * |
||
172 | * @param \Swoole\Websocket\Server $server |
||
173 | * @param int $fd |
||
174 | * @param int $reactorId |
||
175 | */ |
||
176 | public function onClose($server, $fd, $reactorId) |
||
177 | { |
||
178 | if (! $this->isServerWebsocket($fd) || ! $server instanceof WebsocketServer) { |
||
179 | return; |
||
180 | } |
||
181 | |||
182 | $websocket = $this->app->make(Websocket::class); |
||
183 | |||
184 | try { |
||
185 | $websocket->reset(true)->setSender($fd); |
||
186 | // trigger 'disconnect' websocket event |
||
187 | if ($websocket->eventExists('disconnect')) { |
||
188 | $websocket->call('disconnect'); |
||
189 | } else { |
||
190 | $this->websocketHandler->onClose($fd, $reactorId); |
||
191 | } |
||
192 | // leave all rooms |
||
193 | $websocket->leave(); |
||
194 | } catch (Throwable $e) { |
||
195 | $this->logServerError($e); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Indicates if a packet is websocket push action. |
||
201 | * |
||
202 | * @param mixed |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | protected function isWebsocketPushPacket($packet) |
||
207 | { |
||
208 | if (! is_array($packet)) { |
||
209 | return false; |
||
210 | } |
||
211 | |||
212 | return $this->isServerWebsocket |
||
213 | && array_key_exists('action', $packet) |
||
214 | && $packet['action'] === Websocket::PUSH_ACTION; |
||
215 | } |
||
216 | |||
217 | |||
218 | /** |
||
219 | * Push websocket message to clients. |
||
220 | * |
||
221 | * @param \Swoole\Websocket\Server $server |
||
222 | * @param mixed $data |
||
223 | */ |
||
224 | public function pushMessage($server, array $data) |
||
225 | { |
||
226 | $pusher = Pusher::make($data, $server); |
||
227 | $pusher->push($this->payloadParser->encode( |
||
228 | $pusher->getEvent(), |
||
229 | $pusher->getMessage() |
||
230 | )); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Set frame parser for websocket. |
||
235 | * |
||
236 | * @param \SwooleTW\Http\Websocket\Parser $payloadParser |
||
237 | * |
||
238 | * @return \SwooleTW\Http\Concerns\InteractsWithWebsocket |
||
239 | */ |
||
240 | public function setPayloadParser(Parser $payloadParser) |
||
241 | { |
||
242 | $this->payloadParser = $payloadParser; |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Get frame parser for websocket. |
||
249 | */ |
||
250 | public function getPayloadParser() |
||
251 | { |
||
252 | return $this->payloadParser; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Prepare settings if websocket is enabled. |
||
257 | */ |
||
258 | protected function prepareWebsocket() |
||
259 | { |
||
260 | $config = $this->container->make('config'); |
||
261 | $isWebsocket = $config->get('swoole_http.websocket.enabled'); |
||
262 | $parser = $config->get('swoole_websocket.parser'); |
||
263 | |||
264 | if ($isWebsocket) { |
||
265 | $handshake = $config->get('swoole_websocket.handshake.enabled'); |
||
266 | |||
267 | $this->events = array_merge($this->events ?? [], array_merge($this->wsEvents, $handshake ? ['handshake'] : [])); |
||
268 | $this->isServerWebsocket = true; |
||
269 | $this->prepareWebsocketRoom(); |
||
270 | $this->setPayloadParser(new $parser); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Check if it's a websocket fd. |
||
276 | * |
||
277 | * @param int $fd |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | protected function isServerWebsocket(int $fd): bool |
||
282 | { |
||
283 | return (bool) $this->container->make(Server::class) |
||
284 | ->connection_info($fd)['websocket_status'] ?? false; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Prepare websocket handler for onOpen and onClose callback. |
||
289 | * |
||
290 | * @throws \Exception |
||
291 | */ |
||
292 | protected function prepareWebsocketHandler() |
||
293 | { |
||
294 | $handlerClass = $this->container->make('config')->get('swoole_websocket.handler'); |
||
295 | |||
296 | if (! $handlerClass) { |
||
297 | throw new WebsocketNotSetInConfigException; |
||
298 | } |
||
299 | |||
300 | $this->setWebsocketHandler($this->app->make($handlerClass)); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Prepare websocket room. |
||
305 | */ |
||
306 | protected function prepareWebsocketRoom() |
||
307 | { |
||
308 | $config = $this->container->make('config'); |
||
309 | $driver = $config->get('swoole_websocket.default'); |
||
310 | $websocketConfig = $config->get("swoole_websocket.settings.{$driver}"); |
||
311 | $className = $config->get("swoole_websocket.drivers.{$driver}"); |
||
312 | |||
313 | $this->websocketRoom = new $className($websocketConfig); |
||
314 | $this->websocketRoom->prepare(); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Set websocket handler. |
||
319 | * |
||
320 | * @param \SwooleTW\Http\Websocket\HandlerContract $handler |
||
321 | * |
||
322 | * @return \SwooleTW\Http\Concerns\InteractsWithWebsocket |
||
323 | */ |
||
324 | public function setWebsocketHandler(HandlerContract $handler) |
||
325 | { |
||
326 | $this->websocketHandler = $handler; |
||
327 | |||
328 | return $this; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Get websocket handler. |
||
333 | * |
||
334 | * @return \SwooleTW\Http\Websocket\HandlerContract |
||
335 | */ |
||
336 | public function getWebsocketHandler(): HandlerContract |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param string $class |
||
343 | * @param array $settings |
||
344 | * |
||
345 | * @return \SwooleTW\Http\Websocket\Rooms\RoomContract |
||
346 | */ |
||
347 | protected function createRoom(string $class, array $settings): RoomContract |
||
348 | { |
||
349 | return new $class($settings); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Bind room instance to Laravel app container. |
||
354 | */ |
||
355 | protected function bindRoom(): void |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Bind websocket instance to Laravel app container. |
||
366 | */ |
||
367 | protected function bindWebsocket() |
||
368 | { |
||
369 | $this->app->singleton(Websocket::class, function (Container $app) { |
||
370 | return new Websocket($app->make(RoomContract::class), new Pipeline($app)); |
||
371 | }); |
||
372 | |||
373 | $this->app->alias(Websocket::class, 'swoole.websocket'); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Load websocket routes file. |
||
378 | */ |
||
379 | protected function loadWebsocketRoutes() |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Indicates if the payload is websocket push. |
||
393 | * |
||
394 | * @param mixed $payload |
||
395 | * |
||
396 | * @return boolean |
||
397 | */ |
||
398 | public function isWebsocketPushPayload($payload): bool |
||
409 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.