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 $channel |
||
| 272 | * @param stdClass $payload |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function broadcastAcrossServers($appId, string $channel, stdClass $payload) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Handle the user when it joined a presence channel. |
||
| 287 | * |
||
| 288 | * @param \Ratchet\ConnectionInterface $connection |
||
| 289 | * @param stdClass $user |
||
| 290 | * @param string $channel |
||
| 291 | * @param stdClass $payload |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Handle the user when it left a presence channel. |
||
| 307 | * |
||
| 308 | * @param \Ratchet\ConnectionInterface $connection |
||
| 309 | * @param stdClass $user |
||
| 310 | * @param string $channel |
||
| 311 | * @param stdClass $payload |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get the presence channel members. |
||
| 327 | * |
||
| 328 | * @param string|int $appId |
||
| 329 | * @param string $channel |
||
| 330 | * @return \React\Promise\PromiseInterface |
||
| 331 | */ |
||
| 332 | public function getChannelMembers($appId, string $channel): PromiseInterface |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get a member from a presence channel based on connection. |
||
| 348 | * |
||
| 349 | * @param \Ratchet\ConnectionInterface $connection |
||
| 350 | * @param string $channel |
||
| 351 | * @return \React\Promise\PromiseInterface |
||
| 352 | */ |
||
| 353 | public function getChannelMember(ConnectionInterface $connection, string $channel): PromiseInterface |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get the presence channels total members count. |
||
| 362 | * |
||
| 363 | * @param string|int $appId |
||
| 364 | * @param array $channelNames |
||
| 365 | * @return \React\Promise\PromiseInterface |
||
| 366 | */ |
||
| 367 | public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get the socket IDs for a presence channel member. |
||
| 385 | * |
||
| 386 | * @param string|int $userId |
||
| 387 | * @param string|int $appId |
||
| 388 | * @param string $channelName |
||
| 389 | * @return \React\Promise\PromiseInterface |
||
| 390 | */ |
||
| 391 | public function getMemberSockets($userId, $appId, $channelName): PromiseInterface |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Keep tracking the connections availability when they pong. |
||
| 400 | * |
||
| 401 | * @param \Ratchet\ConnectionInterface $connection |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | public function connectionPonged(ConnectionInterface $connection): bool |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Remove the obsolete connections that didn't ponged in a while. |
||
| 414 | * |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | public function removeObsoleteConnections(): bool |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Handle a message received from Redis on a specific channel. |
||
| 435 | * |
||
| 436 | * @param string $redisChannel |
||
| 437 | * @param string $payload |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | public function onMessage(string $redisChannel, string $payload) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Build the Redis connection URL from Laravel database config. |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | protected function getConnectionUri() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Get the Subscribe client instance. |
||
| 495 | * |
||
| 496 | * @return Client |
||
| 497 | */ |
||
| 498 | public function getSubscribeClient() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Get the Publish client instance. |
||
| 505 | * |
||
| 506 | * @return Client |
||
| 507 | */ |
||
| 508 | public function getPublishClient() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Get the unique identifier for the server. |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function getServerId() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Increment the subscribed count number. |
||
| 525 | * |
||
| 526 | * @param string|int $appId |
||
| 527 | * @param string|null $channel |
||
| 528 | * @param int $increment |
||
| 529 | * @return PromiseInterface |
||
| 530 | */ |
||
| 531 | public function incrementSubscriptionsCount($appId, string $channel = null, int $increment = 1) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Decrement the subscribed count number. |
||
| 540 | * |
||
| 541 | * @param string|int $appId |
||
| 542 | * @param string|null $channel |
||
| 543 | * @param int $decrement |
||
| 544 | * @return PromiseInterface |
||
| 545 | */ |
||
| 546 | public function decrementSubscriptionsCount($appId, string $channel = null, int $increment = 1) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Add the connection to the sorted list. |
||
| 553 | * |
||
| 554 | * @param \Ratchet\ConnectionInterface $connection |
||
| 555 | * @param \DateTime|string|null $moment |
||
| 556 | * @return void |
||
| 557 | */ |
||
| 558 | public function addConnectionToSet(ConnectionInterface $connection, $moment = null) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Remove the connection from the sorted list. |
||
| 570 | * |
||
| 571 | * @param \Ratchet\ConnectionInterface $connection |
||
| 572 | * @return void |
||
| 573 | */ |
||
| 574 | public function removeConnectionFromSet(ConnectionInterface $connection) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Get the connections from the sorted list, with last |
||
| 584 | * connection between certain timestamps. |
||
| 585 | * |
||
| 586 | * @param int $start |
||
| 587 | * @param int $stop |
||
| 588 | * @param bool $strict |
||
| 589 | * @return PromiseInterface |
||
| 590 | */ |
||
| 591 | public function getConnectionsFromSet(int $start = 0, int $stop = 0, bool $strict = true) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Add a channel to the set list. |
||
| 613 | * |
||
| 614 | * @param string|int $appId |
||
| 615 | * @param string $channel |
||
| 616 | * @return PromiseInterface |
||
| 617 | */ |
||
| 618 | 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) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Set data for a topic. Might be used for the presence channels. |
||
| 643 | * |
||
| 644 | * @param string|int $appId |
||
| 645 | * @param string|null $channel |
||
| 646 | * @param string $key |
||
| 647 | * @param string $data |
||
| 648 | * @return PromiseInterface |
||
| 649 | */ |
||
| 650 | public function storeUserData($appId, string $channel = null, string $key, $data) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Remove data for a topic. Might be used for the presence channels. |
||
| 659 | * |
||
| 660 | * @param string|int $appId |
||
| 661 | * @param string|null $channel |
||
| 662 | * @param string $key |
||
| 663 | * @return PromiseInterface |
||
| 664 | */ |
||
| 665 | public function removeUserData($appId, string $channel = null, string $key) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Subscribe to the topic for the app, or app and channel. |
||
| 674 | * |
||
| 675 | * @param string|int $appId |
||
| 676 | * @param string|null $channel |
||
| 677 | * @return void |
||
| 678 | */ |
||
| 679 | public function subscribeToTopic($appId, string $channel = null) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Unsubscribe from the topic for the app, or app and channel. |
||
| 688 | * |
||
| 689 | * @param string|int $appId |
||
| 690 | * @param string|null $channel |
||
| 691 | * @return void |
||
| 692 | */ |
||
| 693 | public function unsubscribeFromTopic($appId, string $channel = null) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Add the Presence Channel's User's Socket ID to a list. |
||
| 702 | * |
||
| 703 | * @param string|int $appId |
||
| 704 | * @param string $channel |
||
| 705 | * @param stdClass $user |
||
| 706 | * @param string $socketId |
||
| 707 | * @return void |
||
| 708 | */ |
||
| 709 | protected function addUserSocket($appId, string $channel, stdClass $user, string $socketId) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Remove the Presence Channel's User's Socket ID from the list. |
||
| 719 | * |
||
| 720 | * @param string|int $appId |
||
| 721 | * @param string $channel |
||
| 722 | * @param stdClass $user |
||
| 723 | * @param string $socketId |
||
| 724 | * @return void |
||
| 725 | */ |
||
| 726 | protected function removeUserSocket($appId, string $channel, stdClass $user, string $socketId) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Get the Redis Keyspace name to handle subscriptions |
||
| 736 | * and other key-value sets. |
||
| 737 | * |
||
| 738 | * @param string|int|null $appId |
||
| 739 | * @param string|null $channel |
||
| 740 | * @return string |
||
| 741 | */ |
||
| 742 | public function getRedisKey($appId = null, string $channel = null, array $suffixes = []): string |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Get a new RedisLock instance to avoid race conditions. |
||
| 763 | * |
||
| 764 | * @return \Illuminate\Cache\CacheLock |
||
| 765 | */ |
||
| 766 | protected function lock() |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Create a fake connection for app that will mimick a connection |
||
| 773 | * by app ID and Socket ID to be able to be passed to the methods |
||
| 774 | * that accepts a connection class. |
||
| 775 | * |
||
| 776 | * @param string|int $appId |
||
| 777 | * @param string $socketId |
||
| 778 | * @return ConnectionInterface |
||
| 779 | */ |
||
| 780 | public function fakeConnectionForApp($appId, string $socketId) |
||
| 784 | } |
||
| 785 |
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.