Complex classes like RedisChannelManager 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 RedisChannelManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class RedisChannelManager extends LocalChannelManager |
||
20 | { |
||
21 | /** |
||
22 | * The running loop. |
||
23 | * |
||
24 | * @var LoopInterface |
||
25 | */ |
||
26 | protected $loop; |
||
27 | |||
28 | /** |
||
29 | * The unique server identifier. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $serverId; |
||
34 | |||
35 | /** |
||
36 | * The pub client. |
||
37 | * |
||
38 | * @var Client |
||
39 | */ |
||
40 | protected $publishClient; |
||
41 | |||
42 | /** |
||
43 | * The sub client. |
||
44 | * |
||
45 | * @var Client |
||
46 | */ |
||
47 | protected $subscribeClient; |
||
48 | |||
49 | /** |
||
50 | * The Redis manager instance. |
||
51 | * |
||
52 | * @var \Illuminate\Redis\RedisManager |
||
53 | */ |
||
54 | protected $redis; |
||
55 | |||
56 | /** |
||
57 | * The lock name to use on Redis to avoid multiple |
||
58 | * actions that might lead to multiple processings. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected static $redisLockName = 'laravel-websockets:channel-manager:lock'; |
||
63 | |||
64 | /** |
||
65 | * Create a new channel manager instance. |
||
66 | * |
||
67 | * @param LoopInterface $loop |
||
68 | * @param string|null $factoryClass |
||
69 | * @return void |
||
|
|||
70 | */ |
||
71 | public function __construct(LoopInterface $loop, $factoryClass = null) |
||
93 | |||
94 | /** |
||
95 | * Get the local connections, regardless of the channel |
||
96 | * they are connected to. |
||
97 | * |
||
98 | * @return \React\Promise\PromiseInterface |
||
99 | */ |
||
100 | public function getLocalConnections(): PromiseInterface |
||
104 | |||
105 | /** |
||
106 | * Get all channels for a specific app |
||
107 | * for the current instance. |
||
108 | * |
||
109 | * @param string|int $appId |
||
110 | * @return \React\Promise\PromiseInterface[array] |
||
111 | */ |
||
112 | public function getLocalChannels($appId): PromiseInterface |
||
116 | |||
117 | /** |
||
118 | * Get all channels for a specific app |
||
119 | * across multiple servers. |
||
120 | * |
||
121 | * @param string|int $appId |
||
122 | * @return \React\Promise\PromiseInterface[array] |
||
123 | */ |
||
124 | public function getGlobalChannels($appId): PromiseInterface |
||
130 | |||
131 | /** |
||
132 | * Remove connection from all channels. |
||
133 | * |
||
134 | * @param \Ratchet\ConnectionInterface $connection |
||
135 | * @return void |
||
136 | */ |
||
137 | public function unsubscribeFromAllChannels(ConnectionInterface $connection) |
||
150 | |||
151 | /** |
||
152 | * Subscribe the connection to a specific channel. |
||
153 | * |
||
154 | * @param \Ratchet\ConnectionInterface $connection |
||
155 | * @param string $channelName |
||
156 | * @param stdClass $payload |
||
157 | * @return void |
||
158 | */ |
||
159 | public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload) |
||
171 | |||
172 | /** |
||
173 | * Unsubscribe the connection from the channel. |
||
174 | * |
||
175 | * @param \Ratchet\ConnectionInterface $connection |
||
176 | * @param string $channelName |
||
177 | * @param stdClass $payload |
||
178 | * @return void |
||
179 | */ |
||
180 | public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload) |
||
206 | |||
207 | /** |
||
208 | * Subscribe the connection to a specific channel. |
||
209 | * |
||
210 | * @param string|int $appId |
||
211 | * @return void |
||
212 | */ |
||
213 | public function subscribeToApp($appId) |
||
219 | |||
220 | /** |
||
221 | * Unsubscribe the connection from the channel. |
||
222 | * |
||
223 | * @param string|int $appId |
||
224 | * @return void |
||
225 | */ |
||
226 | public function unsubscribeFromApp($appId) |
||
232 | |||
233 | /** |
||
234 | * Get the connections count on the app |
||
235 | * for the current server instance. |
||
236 | * |
||
237 | * @param string|int $appId |
||
238 | * @param string|null $channelName |
||
239 | * @return \React\Promise\PromiseInterface |
||
240 | */ |
||
241 | public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
245 | |||
246 | /** |
||
247 | * Get the connections count |
||
248 | * across multiple servers. |
||
249 | * |
||
250 | * @param string|int $appId |
||
251 | * @param string|null $channelName |
||
252 | * @return \React\Promise\PromiseInterface |
||
253 | */ |
||
254 | public function getGlobalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
262 | |||
263 | /** |
||
264 | * Broadcast the message across multiple servers. |
||
265 | * |
||
266 | * @param string|int $appId |
||
267 | * @param string|null $socketId |
||
268 | * @param string $channel |
||
269 | * @param stdClass $payload |
||
270 | * @param string|null $serverId |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null) |
||
283 | |||
284 | /** |
||
285 | * Handle the user when it joined a presence channel. |
||
286 | * |
||
287 | * @param \Ratchet\ConnectionInterface $connection |
||
288 | * @param stdClass $user |
||
289 | * @param string $channel |
||
290 | * @param stdClass $payload |
||
291 | * @return void |
||
292 | */ |
||
293 | public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload) |
||
303 | |||
304 | /** |
||
305 | * Handle the user when it left a presence channel. |
||
306 | * |
||
307 | * @param \Ratchet\ConnectionInterface $connection |
||
308 | * @param stdClass $user |
||
309 | * @param string $channel |
||
310 | * @param stdClass $payload |
||
311 | * @return void |
||
312 | */ |
||
313 | public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel) |
||
323 | |||
324 | /** |
||
325 | * Get the presence channel members. |
||
326 | * |
||
327 | * @param string|int $appId |
||
328 | * @param string $channel |
||
329 | * @return \React\Promise\PromiseInterface |
||
330 | */ |
||
331 | public function getChannelMembers($appId, string $channel): PromiseInterface |
||
344 | |||
345 | /** |
||
346 | * Get a member from a presence channel based on connection. |
||
347 | * |
||
348 | * @param \Ratchet\ConnectionInterface $connection |
||
349 | * @param string $channel |
||
350 | * @return \React\Promise\PromiseInterface |
||
351 | */ |
||
352 | public function getChannelMember(ConnectionInterface $connection, string $channel): PromiseInterface |
||
358 | |||
359 | /** |
||
360 | * Get the presence channels total members count. |
||
361 | * |
||
362 | * @param string|int $appId |
||
363 | * @param array $channelNames |
||
364 | * @return \React\Promise\PromiseInterface |
||
365 | */ |
||
366 | public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface |
||
381 | |||
382 | /** |
||
383 | * Get the socket IDs for a presence channel member. |
||
384 | * |
||
385 | * @param string|int $userId |
||
386 | * @param string|int $appId |
||
387 | * @param string $channelName |
||
388 | * @return \React\Promise\PromiseInterface |
||
389 | */ |
||
390 | public function getMemberSockets($userId, $appId, $channelName): PromiseInterface |
||
396 | |||
397 | /** |
||
398 | * Keep tracking the connections availability when they pong. |
||
399 | * |
||
400 | * @param \Ratchet\ConnectionInterface $connection |
||
401 | * @return bool |
||
402 | */ |
||
403 | public function connectionPonged(ConnectionInterface $connection): bool |
||
410 | |||
411 | /** |
||
412 | * Remove the obsolete connections that didn't ponged in a while. |
||
413 | * |
||
414 | * @return bool |
||
415 | */ |
||
416 | public function removeObsoleteConnections(): bool |
||
431 | |||
432 | /** |
||
433 | * Handle a message received from Redis on a specific channel. |
||
434 | * |
||
435 | * @param string $redisChannel |
||
436 | * @param string $payload |
||
437 | * @return void |
||
438 | */ |
||
439 | public function onMessage(string $redisChannel, string $payload) |
||
463 | |||
464 | /** |
||
465 | * Build the Redis connection URL from Laravel database config. |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | protected function getConnectionUri() |
||
491 | |||
492 | /** |
||
493 | * Get the Subscribe client instance. |
||
494 | * |
||
495 | * @return Client |
||
496 | */ |
||
497 | public function getSubscribeClient() |
||
501 | |||
502 | /** |
||
503 | * Get the Publish client instance. |
||
504 | * |
||
505 | * @return Client |
||
506 | */ |
||
507 | public function getPublishClient() |
||
511 | |||
512 | /** |
||
513 | * Get the unique identifier for the server. |
||
514 | * |
||
515 | * @return string |
||
516 | */ |
||
517 | public function getServerId() |
||
521 | |||
522 | /** |
||
523 | * Increment the subscribed count number. |
||
524 | * |
||
525 | * @param string|int $appId |
||
526 | * @param string|null $channel |
||
527 | * @param int $increment |
||
528 | * @return PromiseInterface |
||
529 | */ |
||
530 | public function incrementSubscriptionsCount($appId, string $channel = null, int $increment = 1) |
||
536 | |||
537 | /** |
||
538 | * Decrement the subscribed count number. |
||
539 | * |
||
540 | * @param string|int $appId |
||
541 | * @param string|null $channel |
||
542 | * @param int $decrement |
||
543 | * @return PromiseInterface |
||
544 | */ |
||
545 | public function decrementSubscriptionsCount($appId, string $channel = null, int $increment = 1) |
||
549 | |||
550 | /** |
||
551 | * Add the connection to the sorted list. |
||
552 | * |
||
553 | * @param \Ratchet\ConnectionInterface $connection |
||
554 | * @param \DateTime|string|null $moment |
||
555 | * @return void |
||
556 | */ |
||
557 | public function addConnectionToSet(ConnectionInterface $connection, $moment = null) |
||
566 | |||
567 | /** |
||
568 | * Remove the connection from the sorted list. |
||
569 | * |
||
570 | * @param \Ratchet\ConnectionInterface $connection |
||
571 | * @return void |
||
572 | */ |
||
573 | public function removeConnectionFromSet(ConnectionInterface $connection) |
||
580 | |||
581 | /** |
||
582 | * Get the connections from the sorted list, with last |
||
583 | * connection between certain timestamps. |
||
584 | * |
||
585 | * @param int $start |
||
586 | * @param int $stop |
||
587 | * @param bool $strict |
||
588 | * @return PromiseInterface |
||
589 | */ |
||
590 | public function getConnectionsFromSet(int $start = 0, int $stop = 0, bool $strict = true) |
||
607 | |||
608 | /** |
||
609 | * Add a channel to the set list. |
||
610 | * |
||
611 | * @param string|int $appId |
||
612 | * @param string $channel |
||
613 | * @return PromiseInterface |
||
614 | */ |
||
615 | public function addChannelToSet($appId, string $channel) |
||
621 | |||
622 | /** |
||
623 | * Remove a channel from the set list. |
||
624 | * |
||
625 | * @param string|int $appId |
||
626 | * @param string $channel |
||
627 | * @return PromiseInterface |
||
628 | */ |
||
629 | public function removeChannelFromSet($appId, string $channel) |
||
635 | |||
636 | /** |
||
637 | * Set data for a topic. Might be used for the presence channels. |
||
638 | * |
||
639 | * @param string|int $appId |
||
640 | * @param string|null $channel |
||
641 | * @param string $key |
||
642 | * @param string $data |
||
643 | * @return PromiseInterface |
||
644 | */ |
||
645 | public function storeUserData($appId, string $channel = null, string $key, $data) |
||
651 | |||
652 | /** |
||
653 | * Remove data for a topic. Might be used for the presence channels. |
||
654 | * |
||
655 | * @param string|int $appId |
||
656 | * @param string|null $channel |
||
657 | * @param string $key |
||
658 | * @return PromiseInterface |
||
659 | */ |
||
660 | public function removeUserData($appId, string $channel = null, string $key) |
||
666 | |||
667 | /** |
||
668 | * Subscribe to the topic for the app, or app and channel. |
||
669 | * |
||
670 | * @param string|int $appId |
||
671 | * @param string|null $channel |
||
672 | * @return void |
||
673 | */ |
||
674 | public function subscribeToTopic($appId, string $channel = null) |
||
680 | |||
681 | /** |
||
682 | * Unsubscribe from the topic for the app, or app and channel. |
||
683 | * |
||
684 | * @param string|int $appId |
||
685 | * @param string|null $channel |
||
686 | * @return void |
||
687 | */ |
||
688 | public function unsubscribeFromTopic($appId, string $channel = null) |
||
694 | |||
695 | /** |
||
696 | * Add the Presence Channel's User's Socket ID to a list. |
||
697 | * |
||
698 | * @param string|int $appId |
||
699 | * @param string $channel |
||
700 | * @param stdClass $user |
||
701 | * @param string $socketId |
||
702 | * @return void |
||
703 | */ |
||
704 | protected function addUserSocket($appId, string $channel, stdClass $user, string $socketId) |
||
710 | |||
711 | /** |
||
712 | * Remove the Presence Channel's User's Socket ID from the list. |
||
713 | * |
||
714 | * @param string|int $appId |
||
715 | * @param string $channel |
||
716 | * @param stdClass $user |
||
717 | * @param string $socketId |
||
718 | * @return void |
||
719 | */ |
||
720 | protected function removeUserSocket($appId, string $channel, stdClass $user, string $socketId) |
||
726 | |||
727 | /** |
||
728 | * Get the Redis Keyspace name to handle subscriptions |
||
729 | * and other key-value sets. |
||
730 | * |
||
731 | * @param string|int|null $appId |
||
732 | * @param string|null $channel |
||
733 | * @return string |
||
734 | */ |
||
735 | public function getRedisKey($appId = null, string $channel = null, array $suffixes = []): string |
||
753 | |||
754 | /** |
||
755 | * Get a new RedisLock instance to avoid race conditions. |
||
756 | * |
||
757 | * @return \Illuminate\Cache\CacheLock |
||
758 | */ |
||
759 | protected function lock() |
||
763 | |||
764 | /** |
||
765 | * Create a fake connection for app that will mimick a connection |
||
766 | * by app ID and Socket ID to be able to be passed to the methods |
||
767 | * that accepts a connection class. |
||
768 | * |
||
769 | * @param string|int $appId |
||
770 | * @param string $socketId |
||
771 | * @return ConnectionInterface |
||
772 | */ |
||
773 | public function fakeConnectionForApp($appId, string $socketId) |
||
777 | } |
||
778 |
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.