Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Manager implements ManagerInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Servers we can connect to initially, without knowing the cluster |
||
| 29 | * |
||
| 30 | * After connecting to one, the server returns a list of other nodes |
||
| 31 | * in the cluster so we can connect to them automatically, unless |
||
| 32 | * the discovered nodes are secured with a password. |
||
| 33 | * |
||
| 34 | * 'serverAddress' => Credentials |
||
| 35 | * |
||
| 36 | * @var Credentials[] |
||
| 37 | */ |
||
| 38 | protected $credentials = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * A strategy to prioritize nodes and find the best one to switch to |
||
| 42 | * |
||
| 43 | * The default strategy is the ConservativeJobCountPrioritizer. It |
||
| 44 | * prioritizes nodes by their job count, but prefers the current node |
||
| 45 | * in order to avoid switching until there is a clearly better node. |
||
| 46 | * |
||
| 47 | * @var NodePrioritizerInterface |
||
| 48 | */ |
||
| 49 | protected $priorityStrategy; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * List of nodes, ie Disque instances available in the cluster |
||
| 53 | * |
||
| 54 | * 'nodeId' => Node |
||
| 55 | * |
||
| 56 | * @var Node[] |
||
| 57 | */ |
||
| 58 | protected $nodes = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Node prefixes and their corresponding node ID |
||
| 62 | * |
||
| 63 | * Node prefix consists of the first 8 bytes from the node ID. Because job |
||
| 64 | * IDs contain the node prefix, it can be used to identify on which node |
||
| 65 | * a job lives. |
||
| 66 | * |
||
| 67 | * 'nodePrefix' => 'nodeId' |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $nodePrefixes = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The ID of the node we are currently connected to |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $nodeId; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var ConnectionFactoryInterface |
||
| 82 | */ |
||
| 83 | private $connectionFactory; |
||
| 84 | |||
| 85 | 39 | public function __construct() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @inheritdoc |
||
| 93 | */ |
||
| 94 | 2 | public function getConnectionFactory() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @inheritdoc |
||
| 101 | */ |
||
| 102 | 15 | public function setConnectionFactory( |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @inheritdoc |
||
| 110 | */ |
||
| 111 | 5 | public function getCredentials() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @inheritdoc |
||
| 118 | */ |
||
| 119 | 17 | public function addServer(Credentials $credentials) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @inheritdoc |
||
| 127 | */ |
||
| 128 | 1 | public function getPriorityStrategy() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @inheritdoc |
||
| 135 | */ |
||
| 136 | 5 | public function setPriorityStrategy(NodePrioritizerInterface $priorityStrategy) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | 7 | public function isConnected() |
|
| 145 | { |
||
| 146 | return ( |
||
| 147 | 7 | isset($this->nodeId) && |
|
| 148 | 5 | $this->nodes[$this->nodeId]->getConnection()->isConnected() |
|
| 149 | 7 | ); |
|
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @inheritdoc |
||
| 154 | */ |
||
| 155 | 16 | public function connect() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @inheritdoc |
||
| 164 | */ |
||
| 165 | 7 | public function execute(CommandInterface $command) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @inheritdoc |
||
| 179 | */ |
||
| 180 | 4 | public function getCurrentNode() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Get a functional connection to any known node |
||
| 187 | * |
||
| 188 | * Disque suggests the first connection should be chosen randomly |
||
| 189 | * We go through the user-supplied credentials randomly and try to connect. |
||
| 190 | * |
||
| 191 | * @return Node A connected node |
||
| 192 | * |
||
| 193 | * @throws ConnectionException |
||
| 194 | */ |
||
| 195 | 16 | protected function findAvailableConnection() |
|
| 196 | { |
||
| 197 | 16 | $servers = $this->credentials; |
|
| 198 | 16 | shuffle($servers); |
|
| 199 | 16 | foreach ($servers as $server) { |
|
| 200 | try { |
||
| 201 | 14 | $node = $this->getNodeConnection($server); |
|
| 202 | 14 | } catch (ConnectionException $e) { |
|
| 203 | 6 | continue; |
|
| 204 | } |
||
| 205 | |||
| 206 | 9 | if ($node->getConnection()->isConnected()) { |
|
| 207 | 9 | return $node; |
|
| 208 | } |
||
| 209 | 7 | } |
|
| 210 | |||
| 211 | 7 | throw new ConnectionException('No servers available'); |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Connect to the node given in the credentials |
||
| 216 | * |
||
| 217 | * @param Credentials $server |
||
| 218 | * |
||
| 219 | * @return Node A connected node |
||
| 220 | * |
||
| 221 | * @throws ConnectionException |
||
| 222 | * @throws AuthenticationException |
||
| 223 | */ |
||
| 224 | 14 | protected function getNodeConnection(Credentials $server) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Reset node counters that should be reset upon node switch |
||
| 233 | */ |
||
| 234 | 9 | protected function resetNodeCounters() |
|
| 235 | { |
||
| 236 | 9 | foreach($this->nodes as $node) { |
|
| 237 | 1 | $node->resetJobCount(); |
|
| 238 | 9 | } |
|
| 239 | 9 | } |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Hook into the command execution and do anything before it's executed |
||
| 243 | * |
||
| 244 | * Eg. start measuring node latency etc. |
||
| 245 | * |
||
| 246 | * @param CommandInterface $command |
||
| 247 | * |
||
| 248 | * @return CommandInterface $command |
||
| 249 | */ |
||
| 250 | 5 | protected function preprocessExecution(CommandInterface $command) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Postprocess the command execution, eg. update node stats |
||
| 257 | * |
||
| 258 | * @param CommandInterface $command |
||
| 259 | * @param mixed $response |
||
| 260 | * |
||
| 261 | * @return mixed |
||
| 262 | * @throws ConnectionException |
||
| 263 | */ |
||
| 264 | 5 | protected function postprocessExecution( |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Update node counters indicating how many jobs the node has produced |
||
| 278 | * |
||
| 279 | * @param array $jobs Jobs |
||
| 280 | */ |
||
| 281 | 4 | protected function updateNodeStats(array $jobs) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Decide if we should switch to a better node |
||
| 297 | * |
||
| 298 | * @throws ConnectionException |
||
| 299 | */ |
||
| 300 | 6 | private function switchNodeIfNeeded() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Get a node ID based off a Job ID |
||
| 338 | * |
||
| 339 | * @param string $jobId Job ID |
||
| 340 | * @return string|null Node ID |
||
| 341 | */ |
||
| 342 | 4 | private function getNodeIdFromJobId($jobId) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Get the node prefix from the job ID |
||
| 357 | * |
||
| 358 | * @param string $jobId |
||
| 359 | * @return string Node prefix |
||
| 360 | */ |
||
| 361 | 4 | private function getNodePrefixFromJobId($jobId) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * We should be connected |
||
| 374 | * |
||
| 375 | * @return void |
||
| 376 | * @throws ConnectionException |
||
| 377 | */ |
||
| 378 | 7 | private function shouldBeConnected() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Create a new Node object |
||
| 392 | * |
||
| 393 | * @param Credentials $credentials |
||
| 394 | * |
||
| 395 | * @return Node An unconnected Node |
||
| 396 | */ |
||
| 397 | 14 | private function createNode(Credentials $credentials) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Switch to the given node and map the cluster from its HELLO |
||
| 408 | * |
||
| 409 | * @param Node $node |
||
| 410 | */ |
||
| 411 | 9 | private function switchToNode(Node $node) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Reveal the whole Disque cluster from a node HELLO response |
||
| 427 | * |
||
| 428 | * The HELLO response from a Disque node contains addresses of all other |
||
| 429 | * nodes in the cluster. We want to learn about them and save them, so that |
||
| 430 | * we can switch to them later, if needed. |
||
| 431 | * |
||
| 432 | * @param Node $node The current node |
||
| 433 | */ |
||
| 434 | 9 | private function revealClusterFromHello(Node $node) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Reveal a single node from a HELLO response, or use an existing node |
||
| 455 | * |
||
| 456 | * @param string $nodeId The node ID |
||
| 457 | * @param array $node Node information as returned by the HELLO command |
||
| 458 | * |
||
| 459 | * @return Node $node A node in the current cluster |
||
| 460 | */ |
||
| 461 | 9 | private function revealNodeFromHello($nodeId, array $node) |
|
| 493 | } |
||
| 494 |