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 |
||
| 14 | class RedisChannelManager extends LocalChannelManager |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The running loop. |
||
| 18 | * |
||
| 19 | * @var LoopInterface |
||
| 20 | */ |
||
| 21 | protected $loop; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The unique server identifier. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $serverId; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The pub client. |
||
| 32 | * |
||
| 33 | * @var Client |
||
| 34 | */ |
||
| 35 | protected $publishClient; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The sub client. |
||
| 39 | * |
||
| 40 | * @var Client |
||
| 41 | */ |
||
| 42 | protected $subscribeClient; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Create a new channel manager instance. |
||
| 46 | * |
||
| 47 | * @param LoopInterface $loop |
||
| 48 | * @param string|null $factoryClass |
||
| 49 | * @return void |
||
|
|
|||
| 50 | */ |
||
| 51 | public function __construct(LoopInterface $loop, $factoryClass = null) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get all channels for a specific app |
||
| 72 | * for the current instance. |
||
| 73 | * |
||
| 74 | * @param string|int $appId |
||
| 75 | * @return \React\Promise\PromiseInterface[array] |
||
| 76 | */ |
||
| 77 | public function getLocalChannels($appId): PromiseInterface |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get all channels for a specific app |
||
| 84 | * across multiple servers. |
||
| 85 | * |
||
| 86 | * @param string|int $appId |
||
| 87 | * @return \React\Promise\PromiseInterface[array] |
||
| 88 | */ |
||
| 89 | public function getGlobalChannels($appId): PromiseInterface |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Remove connection from all channels. |
||
| 98 | * |
||
| 99 | * @param \Ratchet\ConnectionInterface $connection |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function unsubscribeFromAllChannels(ConnectionInterface $connection) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Subscribe the connection to a specific channel. |
||
| 118 | * |
||
| 119 | * @param \Ratchet\ConnectionInterface $connection |
||
| 120 | * @param string $channelName |
||
| 121 | * @param stdClass $payload |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Unsubscribe the connection from the channel. |
||
| 147 | * |
||
| 148 | * @param \Ratchet\ConnectionInterface $connection |
||
| 149 | * @param string $channelName |
||
| 150 | * @param stdClass $payload |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Subscribe the connection to a specific channel. |
||
| 188 | * |
||
| 189 | * @param string|int $appId |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | public function subscribeToApp($appId) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Unsubscribe the connection from the channel. |
||
| 201 | * |
||
| 202 | * @param string|int $appId |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | public function unsubscribeFromApp($appId) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get the connections count on the app |
||
| 214 | * for the current server instance. |
||
| 215 | * |
||
| 216 | * @param string|int $appId |
||
| 217 | * @param string|null $channelName |
||
| 218 | * @return \React\Promise\PromiseInterface |
||
| 219 | */ |
||
| 220 | public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the connections count |
||
| 227 | * across multiple servers. |
||
| 228 | * |
||
| 229 | * @param string|int $appId |
||
| 230 | * @param string|null $channelName |
||
| 231 | * @return \React\Promise\PromiseInterface |
||
| 232 | */ |
||
| 233 | public function getGlobalConnectionsCount($appId, string $channelName = null): PromiseInterface |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Broadcast the message across multiple servers. |
||
| 244 | * |
||
| 245 | * @param string|int $appId |
||
| 246 | * @param string $channel |
||
| 247 | * @param stdClass $payload |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function broadcastAcrossServers($appId, string $channel, stdClass $payload) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Handle the user when it joined a presence channel. |
||
| 262 | * |
||
| 263 | * @param \Ratchet\ConnectionInterface $connection |
||
| 264 | * @param stdClass $user |
||
| 265 | * @param string $channel |
||
| 266 | * @param stdClass $payload |
||
| 267 | * @return void |
||
| 268 | */ |
||
| 269 | public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Handle the user when it left a presence channel. |
||
| 278 | * |
||
| 279 | * @param \Ratchet\ConnectionInterface $connection |
||
| 280 | * @param stdClass $user |
||
| 281 | * @param string $channel |
||
| 282 | * @param stdClass $payload |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get the presence channel members. |
||
| 294 | * |
||
| 295 | * @param string|int $appId |
||
| 296 | * @param string $channel |
||
| 297 | * @return \React\Promise\PromiseInterface |
||
| 298 | */ |
||
| 299 | public function getChannelMembers($appId, string $channel): PromiseInterface |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get a member from a presence channel based on connection. |
||
| 318 | * |
||
| 319 | * @param \Ratchet\ConnectionInterface $connection |
||
| 320 | * @param string $channel |
||
| 321 | * @return \React\Promise\PromiseInterface |
||
| 322 | */ |
||
| 323 | public function getChannelMember(ConnectionInterface $connection, string $channel): PromiseInterface |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the presence channels total members count. |
||
| 332 | * |
||
| 333 | * @param string|int $appId |
||
| 334 | * @param array $channelNames |
||
| 335 | * @return \React\Promise\PromiseInterface |
||
| 336 | */ |
||
| 337 | public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Handle a message received from Redis on a specific channel. |
||
| 356 | * |
||
| 357 | * @param string $redisChannel |
||
| 358 | * @param string $payload |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | public function onMessage(string $redisChannel, string $payload) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Build the Redis connection URL from Laravel database config. |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | protected function getConnectionUri() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get the Subscribe client instance. |
||
| 416 | * |
||
| 417 | * @return Client |
||
| 418 | */ |
||
| 419 | public function getSubscribeClient() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get the Publish client instance. |
||
| 426 | * |
||
| 427 | * @return Client |
||
| 428 | */ |
||
| 429 | public function getPublishClient() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get the unique identifier for the server. |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | public function getServerId() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Increment the subscribed count number. |
||
| 446 | * |
||
| 447 | * @param string|int $appId |
||
| 448 | * @param string|null $channel |
||
| 449 | * @param int $increment |
||
| 450 | * @return PromiseInterface |
||
| 451 | */ |
||
| 452 | public function incrementSubscriptionsCount($appId, string $channel = null, int $increment = 1) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Set data for a topic. Might be used for the presence channels. |
||
| 461 | * |
||
| 462 | * @param string|int $appId |
||
| 463 | * @param string|null $channel |
||
| 464 | * @param string $key |
||
| 465 | * @param mixed $data |
||
| 466 | * @return PromiseInterface |
||
| 467 | */ |
||
| 468 | public function storeUserData($appId, string $channel = null, string $key, $data) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Remove data for a topic. Might be used for the presence channels. |
||
| 477 | * |
||
| 478 | * @param string|int $appId |
||
| 479 | * @param string|null $channel |
||
| 480 | * @param string $key |
||
| 481 | * @return PromiseInterface |
||
| 482 | */ |
||
| 483 | public function removeUserData($appId, string $channel = null, string $key) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Subscribe to the topic for the app, or app and channel. |
||
| 492 | * |
||
| 493 | * @param string|int $appId |
||
| 494 | * @param string|null $channel |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | public function subscribeToTopic($appId, string $channel = null) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Unsubscribe from the topic for the app, or app and channel. |
||
| 506 | * |
||
| 507 | * @param string|int $appId |
||
| 508 | * @param string|null $channel |
||
| 509 | * @return void |
||
| 510 | */ |
||
| 511 | public function unsubscribeFromTopic($appId, string $channel = null) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get the Redis Keyspace name to handle subscriptions |
||
| 520 | * and other key-value sets. |
||
| 521 | * |
||
| 522 | * @param mixed $appId |
||
| 523 | * @param string|null $channel |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | public function getRedisKey($appId, string $channel = null, array $suffixes = []): string |
||
| 544 | } |
||
| 545 |
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.