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 | public function __construct() |
||
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | public function getConnectionFactory() |
||
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | public function setConnectionFactory( |
||
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | public function getCredentials() |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | public function addServer(Credentials $credentials) |
||
124 | |||
125 | /** |
||
126 | * @inheritdoc |
||
127 | */ |
||
128 | public function getPriorityStrategy() |
||
132 | |||
133 | /** |
||
134 | * @inheritdoc |
||
135 | */ |
||
136 | public function setPriorityStrategy(NodePrioritizerInterface $priorityStrategy) |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | public function isConnected() |
||
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | public function connect() |
||
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | public function execute(CommandInterface $command) |
||
193 | |||
194 | /** |
||
195 | * @inheritdoc |
||
196 | */ |
||
197 | public function getCurrentNode() |
||
201 | |||
202 | /** |
||
203 | * Get a functional connection to any known node |
||
204 | * |
||
205 | * Disque suggests the first connection should be chosen randomly |
||
206 | * We go through the user-supplied credentials randomly and try to connect. |
||
207 | * |
||
208 | * @return Node A connected node |
||
209 | * |
||
210 | * @throws ConnectionException |
||
211 | */ |
||
212 | protected function findAvailableConnection() |
||
233 | |||
234 | /** |
||
235 | * Connect to the node given in the credentials |
||
236 | * |
||
237 | * @param Credentials $server |
||
238 | * |
||
239 | * @return Node A connected node |
||
240 | * |
||
241 | * @throws ConnectionException |
||
242 | * @throws AuthenticationException |
||
243 | */ |
||
244 | protected function getNodeConnection(Credentials $server) |
||
250 | |||
251 | /** |
||
252 | * Reset node counters that should be reset upon node switch |
||
253 | */ |
||
254 | protected function resetNodeCounters() |
||
260 | |||
261 | /** |
||
262 | * Hook into the command execution and do anything before it's executed |
||
263 | * |
||
264 | * Eg. start measuring node latency etc. |
||
265 | * |
||
266 | * @param CommandInterface $command |
||
267 | * |
||
268 | * @return CommandInterface $command |
||
269 | */ |
||
270 | protected function preprocessExecution(CommandInterface $command) |
||
274 | |||
275 | /** |
||
276 | * Postprocess the command execution, eg. update node stats |
||
277 | * |
||
278 | * @param CommandInterface $command |
||
279 | * @param mixed $response |
||
280 | * |
||
281 | * @return mixed |
||
282 | * @throws ConnectionException |
||
283 | */ |
||
284 | protected function postprocessExecution( |
||
295 | |||
296 | /** |
||
297 | * Update node counters indicating how many jobs the node has produced |
||
298 | * |
||
299 | * @param array $jobs Jobs |
||
300 | */ |
||
301 | protected function updateNodeStats(array $jobs) |
||
314 | |||
315 | /** |
||
316 | * Decide if we should switch to a better node |
||
317 | * |
||
318 | * @throws ConnectionException |
||
319 | */ |
||
320 | private function switchNodeIfNeeded() |
||
358 | |||
359 | /** |
||
360 | * Get a node ID based off a Job ID |
||
361 | * |
||
362 | * @param string $jobId Job ID |
||
363 | * @return string|null Node ID |
||
364 | */ |
||
365 | private function getNodeIdFromJobId($jobId) |
||
377 | |||
378 | /** |
||
379 | * Get the node prefix from the job ID |
||
380 | * |
||
381 | * @param string $jobId |
||
382 | * @return string Node prefix |
||
383 | */ |
||
384 | private function getNodePrefixFromJobId($jobId) |
||
394 | |||
395 | /** |
||
396 | * We should be connected |
||
397 | * |
||
398 | * @return void |
||
399 | * @throws ConnectionException |
||
400 | */ |
||
401 | protected function shouldBeConnected() |
||
412 | |||
413 | /** |
||
414 | * Create a new Node object |
||
415 | * |
||
416 | * @param Credentials $credentials |
||
417 | * |
||
418 | * @return Node An unconnected Node |
||
419 | */ |
||
420 | private function createNode(Credentials $credentials) |
||
428 | |||
429 | /** |
||
430 | * Switch to the given node and map the cluster from its HELLO |
||
431 | * |
||
432 | * @param Node $node |
||
433 | */ |
||
434 | private function switchToNode(Node $node) |
||
456 | |||
457 | /** |
||
458 | * Reveal the whole Disque cluster from a node HELLO response |
||
459 | * |
||
460 | * The HELLO response from a Disque node contains addresses of all other |
||
461 | * nodes in the cluster. We want to learn about them and save them, so that |
||
462 | * we can switch to them later, if needed. |
||
463 | * |
||
464 | * @param Node $node The current node |
||
465 | */ |
||
466 | private function revealClusterFromHello(Node $node) |
||
484 | |||
485 | /** |
||
486 | * Reveal a single node from a HELLO response, or use an existing node |
||
487 | * |
||
488 | * @param string $nodeId The node ID |
||
489 | * @param array $node Node information as returned by the HELLO command |
||
490 | * |
||
491 | * @return Node $node A node in the current cluster |
||
492 | */ |
||
493 | private function revealNodeFromHello($nodeId, array $node) |
||
525 | |||
526 | /** |
||
527 | * Check if the manager held a connection to Disque already |
||
528 | * |
||
529 | * @return bool |
||
530 | */ |
||
531 | private function wasAlreadyConnected() |
||
535 | |||
536 | /** |
||
537 | * Copy node stats from the old to the new node |
||
538 | * |
||
539 | * @param Node $oldNode |
||
540 | * @param Node $newNode |
||
541 | */ |
||
542 | private function copyNodeStats(Node $oldNode, Node $newNode) |
||
547 | } |