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 | use SocketTrait; |
||
19 | |||
20 | /** |
||
21 | * Socket resource |
||
22 | * |
||
23 | * @var resource|null |
||
24 | */ |
||
25 | private $_socket; |
||
26 | |||
27 | /** |
||
28 | * Code of error |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | private $_socket_err_num; |
||
33 | |||
34 | /** |
||
35 | * Description of socket error |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $_socket_err_str; |
||
40 | |||
41 | /** |
||
42 | * Configuration of connection |
||
43 | * |
||
44 | * @var \RouterOS\Config |
||
45 | */ |
||
46 | private $_config; |
||
47 | |||
48 | /** |
||
49 | * Client constructor. |
||
50 | * |
||
51 | * @param array|\RouterOS\Config $config |
||
52 | * @throws \RouterOS\Exceptions\ClientException |
||
53 | * @throws \RouterOS\Exceptions\ConfigException |
||
54 | * @throws \RouterOS\Exceptions\QueryException |
||
55 | */ |
||
56 | 13 | public function __construct($config) |
|
57 | { |
||
58 | // If array then need create object |
||
59 | 13 | if (\is_array($config)) { |
|
60 | 3 | $config = new Config($config); |
|
61 | } |
||
62 | |||
63 | // Check for important keys |
||
64 | 13 | if (true !== $key = ArrayHelper::checkIfKeysNotExist(['host', 'user', 'pass'], $config->getParameters())) { |
|
65 | 1 | throw new ConfigException("One or few parameters '$key' of Config is not set or empty"); |
|
66 | } |
||
67 | |||
68 | // Save config if everything is okay |
||
69 | 12 | $this->setConfig($config); |
|
70 | |||
71 | // Throw error if cannot to connect |
||
72 | 12 | if (false === $this->connect()) { |
|
73 | 1 | throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); |
|
74 | } |
||
75 | 10 | } |
|
76 | |||
77 | /** |
||
78 | * Get some parameter from config |
||
79 | * |
||
80 | * @param string $parameter Name of required parameter |
||
81 | * @return mixed |
||
82 | * @throws \RouterOS\Exceptions\ConfigException |
||
83 | */ |
||
84 | 12 | private function config(string $parameter) |
|
88 | |||
89 | /** |
||
90 | * Return socket resource if is exist |
||
91 | * |
||
92 | * @return \RouterOS\Config |
||
93 | * @since 0.6 |
||
94 | */ |
||
95 | 1 | public function getConfig(): Config |
|
96 | { |
||
97 | 1 | return $this->_config; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Set configuration of client |
||
102 | * |
||
103 | * @param \RouterOS\Config $config |
||
104 | * @since 0.7 |
||
105 | */ |
||
106 | 12 | public function setConfig(Config $config) |
|
110 | |||
111 | /** |
||
112 | * Encode given length in RouterOS format |
||
113 | * |
||
114 | * @param string $string |
||
115 | * @return string Encoded length |
||
116 | * @throws \RouterOS\Exceptions\ClientException |
||
117 | */ |
||
118 | 11 | private function encodeLength(string $string): string |
|
155 | |||
156 | /** |
||
157 | * Read length of line |
||
158 | * |
||
159 | * @param int $byte |
||
160 | * @return int |
||
161 | */ |
||
162 | 11 | private function getLength(int $byte): int |
|
190 | |||
191 | /** |
||
192 | * Send write query to RouterOS (with or without tag) |
||
193 | * |
||
194 | * @param string|array|\RouterOS\Query $query |
||
195 | * @return \RouterOS\Client |
||
196 | * @throws \RouterOS\Exceptions\ClientException |
||
197 | * @throws \RouterOS\Exceptions\QueryException |
||
198 | */ |
||
199 | 11 | public function write($query): Client |
|
200 | { |
||
201 | 11 | if (\is_string($query)) { |
|
202 | 2 | $query = new Query($query); |
|
203 | 11 | } elseif (\is_array($query)) { |
|
204 | 1 | $endpoint = array_shift($query); |
|
205 | 1 | $query = new Query($endpoint, $query); |
|
206 | } |
||
207 | |||
208 | 11 | if (!$query instanceof Query) { |
|
209 | 1 | throw new QueryException('Parameters cannot be processed'); |
|
210 | } |
||
211 | |||
212 | // Send commands via loop to router |
||
213 | 11 | foreach ($query->getQuery() as $command) { |
|
214 | 11 | $command = trim($command); |
|
215 | 11 | fwrite($this->_socket, $this->encodeLength($command) . $command); |
|
216 | } |
||
217 | |||
218 | // Write zero-terminator |
||
219 | 11 | fwrite($this->_socket, \chr(0)); |
|
220 | |||
221 | 11 | return $this; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * Read answer from server after query was executed |
||
226 | * |
||
227 | * @param bool $parse |
||
228 | * @return array |
||
229 | */ |
||
230 | 11 | public function read(bool $parse = true): array |
|
263 | |||
264 | /** |
||
265 | * Alias for ->write() method |
||
266 | * |
||
267 | * @param string|array|\RouterOS\Query $query |
||
268 | * @return \RouterOS\Client |
||
269 | * @throws \RouterOS\Exceptions\ClientException |
||
270 | * @throws \RouterOS\Exceptions\QueryException |
||
271 | */ |
||
272 | 1 | public function w($query): Client |
|
273 | { |
||
274 | 1 | return $this->write($query); |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Alias for ->read() method |
||
279 | * |
||
280 | * @param bool $parse |
||
281 | * @return array |
||
282 | * @since 0.7 |
||
283 | */ |
||
284 | 1 | public function r(bool $parse = true): array |
|
285 | { |
||
286 | 1 | return $this->read($parse); |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * Alias for ->write()->read() combination of methods |
||
291 | * |
||
292 | * @param string|array|\RouterOS\Query $query |
||
293 | * @param bool $parse |
||
294 | * @return array |
||
295 | * @throws \RouterOS\Exceptions\ClientException |
||
296 | * @throws \RouterOS\Exceptions\QueryException |
||
297 | * @since 0.6 |
||
298 | */ |
||
299 | 4 | public function wr($query, bool $parse = true): array |
|
300 | { |
||
301 | 4 | return $this->write($query)->read($parse); |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * Parse response from Router OS |
||
306 | * |
||
307 | * @param array $response Response data |
||
308 | * @return array Array with parsed data |
||
309 | */ |
||
310 | 3 | private function parseResponse(array $response): array |
|
311 | { |
||
312 | 3 | $result = []; |
|
313 | 3 | $i = -1; |
|
314 | 3 | $lines = \count($response); |
|
315 | 3 | foreach ($response as $key => $value) { |
|
316 | 3 | switch ($value) { |
|
317 | 3 | case '!re': |
|
318 | 1 | $i++; |
|
319 | 1 | break; |
|
320 | 3 | case '!fatal': |
|
321 | 1 | $result = $response; |
|
322 | 1 | break 2; |
|
323 | 2 | case '!trap': |
|
324 | 2 | case '!done': |
|
325 | // Check for =ret=, .tag and any other following messages |
||
326 | 2 | for ($j = $key + 1; $j <= $lines; $j++) { |
|
327 | // If we have lines after current one |
||
328 | 2 | if (isset($response[$j])) { |
|
329 | 1 | $this->pregResponse($response[$j], $matches); |
|
330 | 1 | if (!empty($matches)) { |
|
331 | 1 | $result['after'][$matches[1][0]] = $matches[2][0]; |
|
332 | } |
||
333 | } |
||
334 | } |
||
335 | 2 | break 2; |
|
336 | default: |
||
337 | 1 | $this->pregResponse($value, $matches); |
|
338 | 1 | if (!empty($matches)) { |
|
339 | 1 | $result[$i][$matches[1][0]] = $matches[2][0]; |
|
340 | } |
||
341 | 1 | break; |
|
342 | } |
||
343 | } |
||
344 | 3 | return $result; |
|
345 | } |
||
346 | |||
347 | /** |
||
348 | * Parse result from RouterOS by regular expression |
||
349 | * |
||
350 | * @param string $value |
||
351 | * @param array $matches |
||
352 | */ |
||
353 | 2 | private function pregResponse(string $value, &$matches) |
|
357 | |||
358 | /** |
||
359 | * Authorization logic |
||
360 | * |
||
361 | * @return bool |
||
362 | * @throws \RouterOS\Exceptions\ClientException |
||
363 | * @throws \RouterOS\Exceptions\ConfigException |
||
364 | * @throws \RouterOS\Exceptions\QueryException |
||
365 | */ |
||
366 | 11 | private function login(): bool |
|
391 | |||
392 | /** |
||
393 | * Connect to socket server |
||
394 | * |
||
395 | * @return bool |
||
396 | * @throws \RouterOS\Exceptions\ClientException |
||
397 | * @throws \RouterOS\Exceptions\ConfigException |
||
398 | * @throws \RouterOS\Exceptions\QueryException |
||
399 | */ |
||
400 | 12 | private function connect(): bool |
|
431 | |||
432 | } |
||
433 |