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) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Unsubscribe the connection from the channel. |
||
| 178 | * |
||
| 179 | * @param \Ratchet\ConnectionInterface $connection |
||
| 180 | * @param string $channelName |
||
| 181 | * @param stdClass $payload |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Subscribe the connection to a specific channel. |
||
| 213 | * |
||
| 214 | * @param string|int $appId |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | public function subscribeToApp($appId) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Unsubscribe the connection from the channel. |
||
| 226 | * |
||
| 227 | * @param string|int $appId |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | public function unsubscribeFromApp($appId) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get the connections count on the app |
||
| 239 | * for the current server instance. |
||
| 240 | * |
||
| 241 | * @param string|int $appId |
||
| 242 | * @param string|null $channelName |
||
| 243 | * @return \React\Promise\PromiseInterface |
||
| 244 | */ |
||
| 245 | public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get the connections count |
||
| 252 | * across multiple servers. |
||
| 253 | * |
||
| 254 | * @param string|int $appId |
||
| 255 | * @param string|null $channelName |
||
| 256 | * @return \React\Promise\PromiseInterface |
||
| 257 | */ |
||
| 258 | public function getGlobalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Broadcast the message across multiple servers. |
||
| 269 | * |
||
| 270 | * @param string|int $appId |
||
| 271 | * @param string|null $socketId |
||
| 272 | * @param string $channel |
||
| 273 | * @param stdClass $payload |
||
| 274 | * @param string|null $serverId |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Handle the user when it joined a presence channel. |
||
| 290 | * |
||
| 291 | * @param \Ratchet\ConnectionInterface $connection |
||
| 292 | * @param stdClass $user |
||
| 293 | * @param string $channel |
||
| 294 | * @param stdClass $payload |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Handle the user when it left a presence channel. |
||
| 310 | * |
||
| 311 | * @param \Ratchet\ConnectionInterface $connection |
||
| 312 | * @param stdClass $user |
||
| 313 | * @param string $channel |
||
| 314 | * @param stdClass $payload |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get the presence channel members. |
||
| 330 | * |
||
| 331 | * @param string|int $appId |
||
| 332 | * @param string $channel |
||
| 333 | * @return \React\Promise\PromiseInterface |
||
| 334 | */ |
||
| 335 | public function getChannelMembers($appId, string $channel): PromiseInterface |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get a member from a presence channel based on connection. |
||
| 351 | * |
||
| 352 | * @param \Ratchet\ConnectionInterface $connection |
||
| 353 | * @param string $channel |
||
| 354 | * @return \React\Promise\PromiseInterface |
||
| 355 | */ |
||
| 356 | public function getChannelMember(ConnectionInterface $connection, string $channel): PromiseInterface |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the presence channels total members count. |
||
| 365 | * |
||
| 366 | * @param string|int $appId |
||
| 367 | * @param array $channelNames |
||
| 368 | * @return \React\Promise\PromiseInterface |
||
| 369 | */ |
||
| 370 | public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get the socket IDs for a presence channel member. |
||
| 388 | * |
||
| 389 | * @param string|int $userId |
||
| 390 | * @param string|int $appId |
||
| 391 | * @param string $channelName |
||
| 392 | * @return \React\Promise\PromiseInterface |
||
| 393 | */ |
||
| 394 | public function getMemberSockets($userId, $appId, $channelName): PromiseInterface |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Keep tracking the connections availability when they pong. |
||
| 403 | * |
||
| 404 | * @param \Ratchet\ConnectionInterface $connection |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | public function connectionPonged(ConnectionInterface $connection): bool |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Remove the obsolete connections that didn't ponged in a while. |
||
| 417 | * |
||
| 418 | * @return bool |
||
| 419 | */ |
||
| 420 | public function removeObsoleteConnections(): bool |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Handle a message received from Redis on a specific channel. |
||
| 438 | * |
||
| 439 | * @param string $redisChannel |
||
| 440 | * @param string $payload |
||
| 441 | * @return void |
||
| 442 | */ |
||
| 443 | public function onMessage(string $redisChannel, string $payload) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Build the Redis connection URL from Laravel database config. |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | protected function getConnectionUri() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the Subscribe client instance. |
||
| 498 | * |
||
| 499 | * @return Client |
||
| 500 | */ |
||
| 501 | public function getSubscribeClient() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get the Publish client instance. |
||
| 508 | * |
||
| 509 | * @return Client |
||
| 510 | */ |
||
| 511 | public function getPublishClient() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get the unique identifier for the server. |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | public function getServerId() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Increment the subscribed count number. |
||
| 528 | * |
||
| 529 | * @param string|int $appId |
||
| 530 | * @param string|null $channel |
||
| 531 | * @param int $increment |
||
| 532 | * @return PromiseInterface |
||
| 533 | */ |
||
| 534 | public function incrementSubscriptionsCount($appId, string $channel = null, int $increment = 1) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Decrement the subscribed count number. |
||
| 543 | * |
||
| 544 | * @param string|int $appId |
||
| 545 | * @param string|null $channel |
||
| 546 | * @param int $decrement |
||
| 547 | * @return PromiseInterface |
||
| 548 | */ |
||
| 549 | public function decrementSubscriptionsCount($appId, string $channel = null, int $increment = 1) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Add the connection to the sorted list. |
||
| 556 | * |
||
| 557 | * @param \Ratchet\ConnectionInterface $connection |
||
| 558 | * @param \DateTime|string|null $moment |
||
| 559 | * @return void |
||
| 560 | */ |
||
| 561 | public function addConnectionToSet(ConnectionInterface $connection, $moment = null) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Remove the connection from the sorted list. |
||
| 573 | * |
||
| 574 | * @param \Ratchet\ConnectionInterface $connection |
||
| 575 | * @return void |
||
| 576 | */ |
||
| 577 | public function removeConnectionFromSet(ConnectionInterface $connection) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Get the connections from the sorted list, with last |
||
| 587 | * connection between certain timestamps. |
||
| 588 | * |
||
| 589 | * @param int $start |
||
| 590 | * @param int $stop |
||
| 591 | * @param bool $strict |
||
| 592 | * @return PromiseInterface |
||
| 593 | */ |
||
| 594 | public function getConnectionsFromSet(int $start = 0, int $stop = 0, bool $strict = true) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Add a channel to the set list. |
||
| 616 | * |
||
| 617 | * @param string|int $appId |
||
| 618 | * @param string $channel |
||
| 619 | * @return PromiseInterface |
||
| 620 | */ |
||
| 621 | public function addChannelToSet($appId, string $channel) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Remove a channel from the set list. |
||
| 631 | * |
||
| 632 | * @param string|int $appId |
||
| 633 | * @param string $channel |
||
| 634 | * @return PromiseInterface |
||
| 635 | */ |
||
| 636 | public function removeChannelFromSet($appId, string $channel) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Set data for a topic. Might be used for the presence channels. |
||
| 646 | * |
||
| 647 | * @param string|int $appId |
||
| 648 | * @param string|null $channel |
||
| 649 | * @param string $key |
||
| 650 | * @param string $data |
||
| 651 | * @return PromiseInterface |
||
| 652 | */ |
||
| 653 | public function storeUserData($appId, string $channel = null, string $key, $data) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Remove data for a topic. Might be used for the presence channels. |
||
| 662 | * |
||
| 663 | * @param string|int $appId |
||
| 664 | * @param string|null $channel |
||
| 665 | * @param string $key |
||
| 666 | * @return PromiseInterface |
||
| 667 | */ |
||
| 668 | public function removeUserData($appId, string $channel = null, string $key) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Subscribe to the topic for the app, or app and channel. |
||
| 677 | * |
||
| 678 | * @param string|int $appId |
||
| 679 | * @param string|null $channel |
||
| 680 | * @return void |
||
| 681 | */ |
||
| 682 | public function subscribeToTopic($appId, string $channel = null) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Unsubscribe from the topic for the app, or app and channel. |
||
| 691 | * |
||
| 692 | * @param string|int $appId |
||
| 693 | * @param string|null $channel |
||
| 694 | * @return void |
||
| 695 | */ |
||
| 696 | public function unsubscribeFromTopic($appId, string $channel = null) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Add the Presence Channel's User's Socket ID to a list. |
||
| 705 | * |
||
| 706 | * @param string|int $appId |
||
| 707 | * @param string $channel |
||
| 708 | * @param stdClass $user |
||
| 709 | * @param string $socketId |
||
| 710 | * @return void |
||
| 711 | */ |
||
| 712 | protected function addUserSocket($appId, string $channel, stdClass $user, string $socketId) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Remove the Presence Channel's User's Socket ID from the list. |
||
| 722 | * |
||
| 723 | * @param string|int $appId |
||
| 724 | * @param string $channel |
||
| 725 | * @param stdClass $user |
||
| 726 | * @param string $socketId |
||
| 727 | * @return void |
||
| 728 | */ |
||
| 729 | protected function removeUserSocket($appId, string $channel, stdClass $user, string $socketId) |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Get the Redis Keyspace name to handle subscriptions |
||
| 739 | * and other key-value sets. |
||
| 740 | * |
||
| 741 | * @param string|int|null $appId |
||
| 742 | * @param string|null $channel |
||
| 743 | * @return string |
||
| 744 | */ |
||
| 745 | public function getRedisKey($appId = null, string $channel = null, array $suffixes = []): string |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Get a new RedisLock instance to avoid race conditions. |
||
| 766 | * |
||
| 767 | * @return \Illuminate\Cache\CacheLock |
||
| 768 | */ |
||
| 769 | protected function lock() |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Create a fake connection for app that will mimick a connection |
||
| 776 | * by app ID and Socket ID to be able to be passed to the methods |
||
| 777 | * that accepts a connection class. |
||
| 778 | * |
||
| 779 | * @param string|int $appId |
||
| 780 | * @param string $socketId |
||
| 781 | * @return ConnectionInterface |
||
| 782 | */ |
||
| 783 | public function fakeConnectionForApp($appId, string $socketId) |
||
| 787 | } |
||
| 788 |
Adding a
@returnannotation 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.