Total Complexity | 46 |
Total Lines | 373 |
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 | * Websocket server events. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $wsEvents = ['open', 'message', 'close']; |
||
49 | |||
50 | /** |
||
51 | * "onOpen" listener. |
||
52 | * |
||
53 | * @param \Swoole\Websocket\Server $server |
||
54 | * @param \Swoole\Http\Request $swooleRequest |
||
55 | */ |
||
56 | public function onOpen($server, $swooleRequest) |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * "onMessage" listener. |
||
88 | * |
||
89 | * @param \Swoole\Websocket\Server $server |
||
90 | * @param \Swoole\Websocket\Frame $frame |
||
91 | */ |
||
92 | public function onMessage($server, $frame) |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * "onClose" listener. |
||
126 | * |
||
127 | * @param \Swoole\Websocket\Server $server |
||
128 | * @param int $fd |
||
129 | * @param int $reactorId |
||
130 | */ |
||
131 | public function onClose($server, $fd, $reactorId) |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Indicates if a packet is websocket push action. |
||
156 | * |
||
157 | * @param mixed |
||
158 | */ |
||
159 | protected function isWebsocketPushPacket($packet) |
||
160 | { |
||
161 | if ( !is_array($packet)) { |
||
162 | return false; |
||
163 | } |
||
164 | |||
165 | return $this->isWebsocket |
||
166 | && array_key_exists('action', $packet) |
||
167 | && $packet['action'] === Websocket::PUSH_ACTION; |
||
168 | } |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Push websocket message to clients. |
||
173 | * |
||
174 | * @param \Swoole\Websocket\Server $server |
||
175 | * @param mixed $data |
||
176 | */ |
||
177 | public function pushMessage($server, array $data) |
||
178 | { |
||
179 | $push = Push::new($data); |
||
180 | $payload = $this->payloadParser->encode($push->getEvent(), $push->getMessage()); |
||
181 | |||
182 | // attach sender if not broadcast |
||
183 | if (! $push->isBroadcast() && $push->getSender() && ! $push->hasOwnDescriptor()) { |
||
184 | $push->addDescriptor($push->getSender()); |
||
185 | } |
||
186 | |||
187 | // check if to broadcast all clients |
||
188 | if ($push->isBroadcastToAllDescriptors()) { |
||
189 | $push->mergeDescriptor($this->filterWebsocket($server->connections)); |
||
190 | } |
||
191 | |||
192 | // push message to designated fds |
||
193 | foreach ($push->getDescriptors() as $descriptor) { |
||
194 | if ($server->exist($descriptor) || ! $push->isBroadcastToDescriptor((int) $descriptor)) { |
||
195 | $server->push($descriptor, $payload, $push->getOpcode()); |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Set frame parser for websocket. |
||
202 | * |
||
203 | * @param \SwooleTW\Http\Websocket\Parser $payloadParser |
||
204 | * |
||
205 | * @return \SwooleTW\Http\Concerns\InteractsWithWebsocket |
||
206 | */ |
||
207 | public function setPayloadParser(Parser $payloadParser) |
||
208 | { |
||
209 | $this->payloadParser = $payloadParser; |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get frame parser for websocket. |
||
216 | */ |
||
217 | public function getPayloadParser() |
||
218 | { |
||
219 | return $this->payloadParser; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Prepare settings if websocket is enabled. |
||
224 | */ |
||
225 | protected function prepareWebsocket() |
||
226 | { |
||
227 | $config = $this->container->make('config'); |
||
228 | $isWebsocket = $config->get('swoole_http.websocket.enabled'); |
||
229 | $parser = $config->get('swoole_websocket.parser'); |
||
230 | |||
231 | if ($isWebsocket) { |
||
232 | $this->events = array_merge($this->events ?? [], $this->wsEvents); |
||
233 | $this->isServerWebsocket = true; |
||
234 | $this->setPayloadParser(new $parser); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Check if it's a websocket fd. |
||
240 | * |
||
241 | * @param int $fd |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | protected function isServerWebsocket(int $fd): bool |
||
246 | { |
||
247 | $info = $this->container->make(Server::class)->connection_info($fd); |
||
248 | |||
249 | return Arr::has($info, 'websocket_status') && Arr::get($info, 'websocket_status'); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Returns all descriptors that are websocket |
||
254 | * |
||
255 | * @param array $descriptors |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | protected function filterWebsocket(array $descriptors): array |
||
260 | { |
||
261 | $callback = function ($descriptor) { |
||
262 | return $this->isServerWebsocket($descriptor); |
||
263 | }; |
||
264 | |||
265 | return collect($descriptors)->filter($callback)->toArray(); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Prepare websocket handler for onOpen and onClose callback. |
||
270 | * |
||
271 | * @throws \Exception |
||
272 | */ |
||
273 | protected function prepareWebsocketHandler() |
||
274 | { |
||
275 | $handlerClass = $this->container->make('config')->get('swoole_websocket.handler'); |
||
276 | |||
277 | if (! $handlerClass) { |
||
278 | throw new WebsocketNotSetInConfigException; |
||
279 | } |
||
280 | |||
281 | $this->setWebsocketHandler($this->app->make($handlerClass)); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Set websocket handler. |
||
286 | * |
||
287 | * @param \SwooleTW\Http\Websocket\HandlerContract $handler |
||
288 | * |
||
289 | * @return \SwooleTW\Http\Concerns\InteractsWithWebsocket |
||
290 | */ |
||
291 | public function setWebsocketHandler(HandlerContract $handler) |
||
292 | { |
||
293 | $this->websocketHandler = $handler; |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get websocket handler. |
||
300 | * |
||
301 | * @return \SwooleTW\Http\Websocket\HandlerContract |
||
302 | */ |
||
303 | public function getWebsocketHandler(): HandlerContract |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param string $class |
||
310 | * @param array $settings |
||
311 | * |
||
312 | * @return \SwooleTW\Http\Websocket\Rooms\RoomContract |
||
313 | */ |
||
314 | protected function createRoom(string $class, array $settings): RoomContract |
||
315 | { |
||
316 | return new $class($settings); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Bind room instance to Laravel app container. |
||
321 | */ |
||
322 | protected function bindRoom(): void |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Bind websocket instance to Laravel app container. |
||
338 | */ |
||
339 | protected function bindWebsocket() |
||
340 | { |
||
341 | $this->app->singleton(Websocket::class, function (Container $app) { |
||
342 | return new Websocket($app->make(RoomContract::class), new Pipeline($app)); |
||
343 | }); |
||
344 | |||
345 | $this->app->alias(Websocket::class, 'swoole.websocket'); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Load websocket routes file. |
||
350 | */ |
||
351 | protected function loadWebsocketRoutes() |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Normalize data for message push. |
||
365 | * |
||
366 | * @param array $data |
||
367 | * |
||
368 | * @return array |
||
369 | */ |
||
370 | public function normalizePushData(array $data) |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Indicates if the payload is websocket push. |
||
385 | * |
||
386 | * @param mixed $payload |
||
387 | * |
||
388 | * @return boolean |
||
389 | */ |
||
390 | protected function isWebsocketPushPayload($payload): bool |
||
401 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.