Complex classes like LocalChannelManager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 LocalChannelManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class LocalChannelManager implements ChannelManager |
||
20 | { |
||
21 | /** |
||
22 | * The list of stored channels. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $channels = []; |
||
27 | |||
28 | /** |
||
29 | * The list of users that joined the presence channel. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $users = []; |
||
34 | |||
35 | /** |
||
36 | * The list of users by socket and their attached id. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $userSockets = []; |
||
41 | |||
42 | /** |
||
43 | * Wether the current instance accepts new connections. |
||
44 | * |
||
45 | * @var bool |
||
46 | */ |
||
47 | protected $acceptsNewConnections = true; |
||
48 | |||
49 | /** |
||
50 | * The lock name to use on Array to avoid multiple |
||
51 | * actions that might lead to multiple processings. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected static $lockName = 'laravel-websockets:channel-manager:lock'; |
||
56 | |||
57 | /** |
||
58 | * Create a new channel manager instance. |
||
59 | * |
||
60 | * @param LoopInterface $loop |
||
61 | * @param string|null $factoryClass |
||
62 | * @return void |
||
|
|||
63 | */ |
||
64 | public function __construct(LoopInterface $loop, $factoryClass = null) |
||
68 | |||
69 | /** |
||
70 | * Find the channel by app & name. |
||
71 | * |
||
72 | * @param string|int $appId |
||
73 | * @param string $channel |
||
74 | * @return null|BeyondCode\LaravelWebSockets\Channels\Channel |
||
75 | */ |
||
76 | public function find($appId, string $channel) |
||
80 | |||
81 | /** |
||
82 | * Find a channel by app & name or create one. |
||
83 | * |
||
84 | * @param string|int $appId |
||
85 | * @param string $channel |
||
86 | * @return BeyondCode\LaravelWebSockets\Channels\Channel |
||
87 | */ |
||
88 | public function findOrCreate($appId, string $channel) |
||
98 | |||
99 | /** |
||
100 | * Get the local connections, regardless of the channel |
||
101 | * they are connected to. |
||
102 | * |
||
103 | * @return \React\Promise\PromiseInterface |
||
104 | */ |
||
105 | public function getLocalConnections(): PromiseInterface |
||
120 | |||
121 | /** |
||
122 | * Get all channels for a specific app |
||
123 | * for the current instance. |
||
124 | * |
||
125 | * @param string|int $appId |
||
126 | * @return \React\Promise\PromiseInterface[array] |
||
127 | */ |
||
128 | public function getLocalChannels($appId): PromiseInterface |
||
134 | |||
135 | /** |
||
136 | * Get all channels for a specific app |
||
137 | * across multiple servers. |
||
138 | * |
||
139 | * @param string|int $appId |
||
140 | * @return \React\Promise\PromiseInterface[array] |
||
141 | */ |
||
142 | public function getGlobalChannels($appId): PromiseInterface |
||
146 | |||
147 | /** |
||
148 | * Remove connection from all channels. |
||
149 | * |
||
150 | * @param \Ratchet\ConnectionInterface $connection |
||
151 | * @return PromiseInterface[bool] |
||
152 | */ |
||
153 | public function unsubscribeFromAllChannels(ConnectionInterface $connection): PromiseInterface |
||
179 | |||
180 | /** |
||
181 | * Subscribe the connection to a specific channel. |
||
182 | * |
||
183 | * @param \Ratchet\ConnectionInterface $connection |
||
184 | * @param string $channelName |
||
185 | * @param stdClass $payload |
||
186 | * @return PromiseInterface[bool] |
||
187 | */ |
||
188 | public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload): PromiseInterface |
||
196 | |||
197 | /** |
||
198 | * Unsubscribe the connection from the channel. |
||
199 | * |
||
200 | * @param \Ratchet\ConnectionInterface $connection |
||
201 | * @param string $channelName |
||
202 | * @param stdClass $payload |
||
203 | * @return PromiseInterface[bool] |
||
204 | */ |
||
205 | public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload): PromiseInterface |
||
213 | |||
214 | /** |
||
215 | * Subscribe the connection to a specific channel, returning |
||
216 | * a promise containing the amount of connections. |
||
217 | * |
||
218 | * @param string|int $appId |
||
219 | * @return PromiseInterface[int] |
||
220 | */ |
||
221 | public function subscribeToApp($appId): PromiseInterface |
||
225 | |||
226 | /** |
||
227 | * Unsubscribe the connection from the channel, returning |
||
228 | * a promise containing the amount of connections after decrement. |
||
229 | * |
||
230 | * @param string|int $appId |
||
231 | * @return PromiseInterface[int] |
||
232 | */ |
||
233 | public function unsubscribeFromApp($appId): PromiseInterface |
||
237 | |||
238 | /** |
||
239 | * Get the connections count on the app |
||
240 | * for the current server instance. |
||
241 | * |
||
242 | * @param string|int $appId |
||
243 | * @param string|null $channelName |
||
244 | * @return PromiseInterface[int] |
||
245 | */ |
||
246 | public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
261 | |||
262 | /** |
||
263 | * Get the connections count |
||
264 | * across multiple servers. |
||
265 | * |
||
266 | * @param string|int $appId |
||
267 | * @param string|null $channelName |
||
268 | * @return PromiseInterface[int] |
||
269 | */ |
||
270 | public function getGlobalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
274 | |||
275 | /** |
||
276 | * Broadcast the message across multiple servers. |
||
277 | * |
||
278 | * @param string|int $appId |
||
279 | * @param string|null $socketId |
||
280 | * @param string $channel |
||
281 | * @param stdClass $payload |
||
282 | * @param string|null $serverId |
||
283 | * @return PromiseInterface[bool] |
||
284 | */ |
||
285 | public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null): PromiseInterface |
||
289 | |||
290 | /** |
||
291 | * Handle the user when it joined a presence channel. |
||
292 | * |
||
293 | * @param \Ratchet\ConnectionInterface $connection |
||
294 | * @param stdClass $user |
||
295 | * @param string $channel |
||
296 | * @param stdClass $payload |
||
297 | * @return PromiseInterface[bool] |
||
298 | */ |
||
299 | public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload): PromiseInterface |
||
306 | |||
307 | /** |
||
308 | * Handle the user when it left a presence channel. |
||
309 | * |
||
310 | * @param \Ratchet\ConnectionInterface $connection |
||
311 | * @param stdClass $user |
||
312 | * @param string $channel |
||
313 | * @param stdClass $payload |
||
314 | * @return PromiseInterface[bool] |
||
315 | */ |
||
316 | public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel): PromiseInterface |
||
335 | |||
336 | /** |
||
337 | * Get the presence channel members. |
||
338 | * |
||
339 | * @param string|int $appId |
||
340 | * @param string $channel |
||
341 | * @return \React\Promise\PromiseInterface |
||
342 | */ |
||
343 | public function getChannelMembers($appId, string $channel): PromiseInterface |
||
353 | |||
354 | /** |
||
355 | * Get a member from a presence channel based on connection. |
||
356 | * |
||
357 | * @param \Ratchet\ConnectionInterface $connection |
||
358 | * @param string $channel |
||
359 | * @return \React\Promise\PromiseInterface |
||
360 | */ |
||
361 | public function getChannelMember(ConnectionInterface $connection, string $channel): PromiseInterface |
||
367 | |||
368 | /** |
||
369 | * Get the presence channels total members count. |
||
370 | * |
||
371 | * @param string|int $appId |
||
372 | * @param array $channelNames |
||
373 | * @return \React\Promise\PromiseInterface |
||
374 | */ |
||
375 | public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface |
||
388 | |||
389 | /** |
||
390 | * Get the socket IDs for a presence channel member. |
||
391 | * |
||
392 | * @param string|int $userId |
||
393 | * @param string|int $appId |
||
394 | * @param string $channelName |
||
395 | * @return \React\Promise\PromiseInterface |
||
396 | */ |
||
397 | public function getMemberSockets($userId, $appId, $channelName): PromiseInterface |
||
403 | |||
404 | /** |
||
405 | * Keep tracking the connections availability when they pong. |
||
406 | * |
||
407 | * @param \Ratchet\ConnectionInterface $connection |
||
408 | * @return PromiseInterface[bool] |
||
409 | */ |
||
410 | public function connectionPonged(ConnectionInterface $connection): PromiseInterface |
||
416 | |||
417 | /** |
||
418 | * Remove the obsolete connections that didn't ponged in a while. |
||
419 | * |
||
420 | * @return PromiseInterface[bool] |
||
421 | */ |
||
422 | public function removeObsoleteConnections(): PromiseInterface |
||
442 | |||
443 | /** |
||
444 | * Update the connection in all channels. |
||
445 | * |
||
446 | * @param ConnectionInterface $connection |
||
447 | * @return PromiseInterface[bool] |
||
448 | */ |
||
449 | public function updateConnectionInChannels($connection): PromiseInterface |
||
462 | |||
463 | /** |
||
464 | * Mark the current instance as unable to accept new connections. |
||
465 | * |
||
466 | * @return $this |
||
467 | */ |
||
468 | public function declineNewConnections() |
||
474 | |||
475 | /** |
||
476 | * Check if the current server instance |
||
477 | * accepts new connections. |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | public function acceptsNewConnections(): bool |
||
485 | |||
486 | /** |
||
487 | * Get the channel class by the channel name. |
||
488 | * |
||
489 | * @param string $channelName |
||
490 | * @return string |
||
491 | */ |
||
492 | protected function getChannelClassName(string $channelName): string |
||
504 | |||
505 | /** |
||
506 | * Get a new ArrayLock instance to avoid race conditions. |
||
507 | * |
||
508 | * @return \Illuminate\Cache\CacheLock |
||
509 | */ |
||
510 | protected function lock() |
||
514 | } |
||
515 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.