Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Client implements Interfaces\ClientInterface |
||
17 | { |
||
18 | /** |
||
19 | * Socket resource |
||
20 | * @var resource|null |
||
21 | */ |
||
22 | private $_socket; |
||
23 | |||
24 | /** |
||
25 | * Code of error |
||
26 | * @var int |
||
27 | */ |
||
28 | private $_socket_err_num; |
||
29 | |||
30 | /** |
||
31 | * Description of socket error |
||
32 | * @var string |
||
33 | */ |
||
34 | private $_socket_err_str; |
||
35 | |||
36 | /** |
||
37 | * Configuration of connection |
||
38 | * @var ConfigInterface |
||
39 | */ |
||
40 | private $_config; |
||
41 | |||
42 | /** |
||
43 | * Client constructor. |
||
44 | * |
||
45 | * @param ConfigInterface $config |
||
46 | * @throws ConfigException |
||
47 | * @throws ClientException |
||
48 | */ |
||
49 | 5 | public function __construct(ConfigInterface $config) |
|
50 | { |
||
51 | // Check for important keys |
||
52 | 5 | $this->exceptionIfKeyNotExist(['host', 'user', 'pass'], $config); |
|
53 | |||
54 | // Save config if everything is okay |
||
55 | 5 | $this->_config = $config; |
|
56 | |||
57 | // Throw error if cannot to connect |
||
58 | 5 | if (false === $this->connect()) { |
|
59 | throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); |
||
60 | } |
||
61 | 4 | } |
|
62 | |||
63 | /** |
||
64 | * Check for important keys |
||
65 | * |
||
66 | * @param array $keys |
||
67 | * @param ConfigInterface $config |
||
68 | * @throws ConfigException |
||
69 | */ |
||
70 | 5 | private function exceptionIfKeyNotExist(array $keys, ConfigInterface $config) |
|
71 | { |
||
72 | 5 | $parameters = $config->getParameters(); |
|
73 | 5 | foreach ($keys as $key) { |
|
74 | 5 | if (!array_key_exists($key, $parameters) && isset($parameters[$key])) { |
|
75 | 5 | throw new ConfigException("Parameter '$key' of Config is not set or empty"); |
|
76 | } |
||
77 | } |
||
78 | 5 | } |
|
79 | |||
80 | /** |
||
81 | * Get some parameter from config |
||
82 | * |
||
83 | * @param string $parameter |
||
84 | * @return mixed |
||
85 | */ |
||
86 | 5 | private function config(string $parameter) |
|
90 | |||
91 | /** |
||
92 | * Encode given length in RouterOS format |
||
93 | * |
||
94 | * @param string $string |
||
95 | * @return string Encoded length |
||
96 | * @throws ClientException |
||
97 | */ |
||
98 | 4 | private function encodeLength(string $string): string |
|
135 | |||
136 | /** |
||
137 | * Read length of line |
||
138 | * |
||
139 | * @param int $byte |
||
140 | * @return int |
||
141 | */ |
||
142 | 4 | private function getLength(int $byte): int |
|
174 | |||
175 | /** |
||
176 | * Send write query to RouterOS (with or without tag) |
||
177 | * |
||
178 | * @param QueryInterface $query |
||
179 | * @return ClientInterface |
||
180 | * @throws ClientException |
||
181 | */ |
||
182 | 4 | public function write(QueryInterface $query): ClientInterface |
|
195 | |||
196 | /** |
||
197 | * Read answer from server after query was executed |
||
198 | * |
||
199 | * @param bool $parse |
||
200 | * @return array |
||
201 | */ |
||
202 | 4 | public function read(bool $parse = true): array |
|
238 | |||
239 | /** |
||
240 | * Parse response from Router OS |
||
241 | * |
||
242 | * @param array $response Response data |
||
243 | * @return array Array with parsed data |
||
244 | */ |
||
245 | 1 | private function parseResponse(array $response): array |
|
246 | { |
||
247 | 1 | $result = []; |
|
248 | 1 | $i = -1; |
|
249 | 1 | $lines = \count($response); |
|
250 | 1 | foreach ($response as $key => $value) { |
|
251 | switch ($value) { |
||
252 | 1 | case '!re': |
|
253 | $i++; |
||
254 | break; |
||
255 | 1 | case '!fatal': |
|
256 | 1 | case '!trap': |
|
257 | 1 | case '!done': |
|
258 | // Check for =ret=, .tag and any other following messages |
||
259 | 1 | for ($j = $key + 1; $j <= $lines; $j++) { |
|
260 | // If we have lines after current one |
||
261 | 1 | if (isset($response[$j])) { |
|
262 | 1 | $this->pregResponse($response[$j], $matches); |
|
263 | 1 | if (!empty($matches)) { |
|
264 | 1 | $result['after'][$matches[1][0]] = $matches[2][0]; |
|
265 | } |
||
266 | } |
||
267 | } |
||
268 | 1 | break 2; |
|
269 | default: |
||
270 | $this->pregResponse($value, $matches); |
||
271 | if (!empty($matches)) { |
||
272 | $result[$i][$matches[1][0]] = $matches[2][0]; |
||
273 | } |
||
274 | break; |
||
275 | } |
||
276 | } |
||
277 | 1 | return $result; |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * Parse result from RouterOS by regular expression |
||
282 | * |
||
283 | * @param string $value |
||
284 | * @param array $matches |
||
285 | */ |
||
286 | 1 | private function pregResponse(string $value, &$matches) |
|
290 | |||
291 | /** |
||
292 | * Authorization logic |
||
293 | * |
||
294 | * @return bool |
||
295 | * @throws ClientException |
||
296 | */ |
||
297 | 4 | private function login(): bool |
|
323 | |||
324 | /** |
||
325 | * Connect to socket server |
||
326 | * |
||
327 | * @return bool |
||
328 | * @throws ClientException |
||
329 | */ |
||
330 | 5 | private function connect(): bool |
|
331 | { |
||
332 | // By default we not connected |
||
333 | 5 | $connected = false; |
|
334 | |||
335 | // Few attempts in loop |
||
336 | 5 | for ($attempt = 1; $attempt <= $this->config('attempts'); $attempt++) { |
|
337 | |||
338 | // Initiate socket session |
||
339 | 5 | $this->openSocket(); |
|
340 | |||
341 | // If socket is active |
||
342 | 4 | if ($this->getSocket()) { |
|
343 | |||
344 | // If we logged in then exit from loop |
||
345 | 4 | if (true === $this->login()) { |
|
346 | 4 | $connected = true; |
|
347 | 4 | break; |
|
348 | } |
||
349 | |||
350 | // Else close socket and start from begin |
||
351 | $this->closeSocket(); |
||
352 | } |
||
353 | |||
354 | // Sleep some time between tries |
||
355 | sleep($this->config('delay')); |
||
356 | } |
||
357 | |||
358 | // Return status of connection |
||
359 | 4 | return $connected; |
|
360 | } |
||
361 | |||
362 | /** |
||
363 | * Save socket resource to static variable |
||
364 | * |
||
365 | * @param resource $socket |
||
366 | */ |
||
367 | 4 | private function setSocket($socket) |
|
371 | |||
372 | /** |
||
373 | * Return socket resource if is exist |
||
374 | * |
||
375 | * @return resource |
||
376 | */ |
||
377 | 4 | public function getSocket() |
|
381 | |||
382 | /** |
||
383 | * Initiate socket session |
||
384 | * |
||
385 | * @throws ClientException |
||
386 | */ |
||
387 | 5 | private function openSocket() |
|
419 | |||
420 | /** |
||
421 | * Close socket session |
||
422 | * |
||
423 | * @return bool |
||
424 | */ |
||
425 | private function closeSocket(): bool |
||
429 | } |
||
430 |