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) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Add a channel to the set list. |
||
| 614 | * |
||
| 615 | * @param string|int $appId |
||
| 616 | * @param string $channel |
||
| 617 | * @return PromiseInterface |
||
| 618 | */ |
||
| 619 | public function addChannelToSet($appId, string $channel) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Remove a channel from the set list. |
||
| 628 | * |
||
| 629 | * @param string|int $appId |
||
| 630 | * @param string $channel |
||
| 631 | * @return PromiseInterface |
||
| 632 | */ |
||
| 633 | public function removeChannelFromSet($appId, string $channel) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Set data for a topic. Might be used for the presence channels. |
||
| 642 | * |
||
| 643 | * @param string|int $appId |
||
| 644 | * @param string|null $channel |
||
| 645 | * @param string $key |
||
| 646 | * @param string $data |
||
| 647 | * @return PromiseInterface |
||
| 648 | */ |
||
| 649 | public function storeUserData($appId, string $channel = null, string $key, $data) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Remove data for a topic. Might be used for the presence channels. |
||
| 658 | * |
||
| 659 | * @param string|int $appId |
||
| 660 | * @param string|null $channel |
||
| 661 | * @param string $key |
||
| 662 | * @return PromiseInterface |
||
| 663 | */ |
||
| 664 | public function removeUserData($appId, string $channel = null, string $key) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Subscribe to the topic for the app, or app and channel. |
||
| 673 | * |
||
| 674 | * @param string|int $appId |
||
| 675 | * @param string|null $channel |
||
| 676 | * @return void |
||
| 677 | */ |
||
| 678 | public function subscribeToTopic($appId, string $channel = null) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Unsubscribe from the topic for the app, or app and channel. |
||
| 687 | * |
||
| 688 | * @param string|int $appId |
||
| 689 | * @param string|null $channel |
||
| 690 | * @return void |
||
| 691 | */ |
||
| 692 | public function unsubscribeFromTopic($appId, string $channel = null) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Add the Presence Channel's User's Socket ID to a list. |
||
| 701 | * |
||
| 702 | * @param string|int $appId |
||
| 703 | * @param string $channel |
||
| 704 | * @param stdClass $user |
||
| 705 | * @param string $socketId |
||
| 706 | * @return void |
||
| 707 | */ |
||
| 708 | protected function addUserSocket($appId, string $channel, stdClass $user, string $socketId) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Remove the Presence Channel's User's Socket ID from the list. |
||
| 717 | * |
||
| 718 | * @param string|int $appId |
||
| 719 | * @param string $channel |
||
| 720 | * @param stdClass $user |
||
| 721 | * @param string $socketId |
||
| 722 | * @return void |
||
| 723 | */ |
||
| 724 | protected function removeUserSocket($appId, string $channel, stdClass $user, string $socketId) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Get the Redis Keyspace name to handle subscriptions |
||
| 733 | * and other key-value sets. |
||
| 734 | * |
||
| 735 | * @param string|int|null $appId |
||
| 736 | * @param string|null $channel |
||
| 737 | * @return string |
||
| 738 | */ |
||
| 739 | public function getRedisKey($appId = null, string $channel = null, array $suffixes = []): string |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Get a new RedisLock instance to avoid race conditions. |
||
| 760 | * |
||
| 761 | * @return \Illuminate\Cache\CacheLock |
||
| 762 | */ |
||
| 763 | protected function lock() |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Create a fake connection for app that will mimick a connection |
||
| 770 | * by app ID and Socket ID to be able to be passed to the methods |
||
| 771 | * that accepts a connection class. |
||
| 772 | * |
||
| 773 | * @param string|int $appId |
||
| 774 | * @param string $socketId |
||
| 775 | * @return ConnectionInterface |
||
| 776 | */ |
||
| 777 | public function fakeConnectionForApp($appId, string $socketId) |
||
| 781 | } |
||
| 782 |
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.