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 |
||
10 | class Client |
||
11 | { |
||
12 | const RESPONSE_SHORT = 1; |
||
13 | const RESPONSE_FULL = 2; |
||
14 | |||
15 | protected $_host; |
||
16 | protected $_port; |
||
17 | protected $_protocol; |
||
18 | protected $_login; |
||
19 | protected $_password; |
||
20 | protected $_secretKey; |
||
21 | protected $_version = ''; |
||
22 | |||
23 | protected static $_isExecutionsLogEnabled = false; |
||
24 | protected static $_executionLog = []; |
||
25 | |||
26 | protected $_operatorsCache = []; |
||
27 | |||
28 | /** |
||
29 | * Create client |
||
30 | * |
||
31 | * @param string $host |
||
32 | * @param int $port |
||
33 | * @param string $protocol |
||
34 | */ |
||
35 | public function __construct($host, $port = 8443, $protocol = 'https') |
||
41 | |||
42 | /** |
||
43 | * Setup credentials for authentication |
||
44 | * |
||
45 | * @param string $login |
||
46 | * @param string $password |
||
47 | */ |
||
48 | public function setCredentials($login, $password) |
||
53 | |||
54 | /** |
||
55 | * Define secret key for alternative authentication |
||
56 | * |
||
57 | * @param string $secretKey |
||
58 | */ |
||
59 | public function setSecretKey($secretKey) |
||
63 | |||
64 | /** |
||
65 | * Set default version for requests |
||
66 | * |
||
67 | * @param string $version |
||
68 | */ |
||
69 | public function setVersion($version) |
||
73 | |||
74 | /** |
||
75 | * Retrieve host used for communication |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | public function getHost() |
||
83 | |||
84 | /** |
||
85 | * Retrieve port used for communication |
||
86 | * |
||
87 | * @return int |
||
88 | */ |
||
89 | public function getPort() |
||
93 | |||
94 | /** |
||
95 | * Retrieve name of the protocol (http or https) used for communication |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getProtocol() |
||
103 | |||
104 | /** |
||
105 | * Retrieve XML template for packet |
||
106 | * |
||
107 | * @param string|null $version |
||
108 | * @return SimpleXMLElement |
||
109 | */ |
||
110 | public function getPacket($version = null) |
||
117 | |||
118 | /** |
||
119 | * Perform API request |
||
120 | * |
||
121 | * @param string|array|SimpleXMLElement $request |
||
122 | * @param int $mode |
||
123 | * @return XmlResponse |
||
124 | */ |
||
125 | public function request($request, $mode = self::RESPONSE_SHORT) |
||
126 | { |
||
127 | if ($request instanceof SimpleXMLElement) { |
||
128 | $request = $request->asXml(); |
||
129 | } else { |
||
130 | $xml = $this->getPacket(); |
||
131 | |||
132 | if (is_array($request)) { |
||
133 | $request = $this->_arrayToXml($request, $xml)->asXML(); |
||
134 | } else if (preg_match('/^[a-z]/', $request)) { |
||
135 | $request = $this->_expandRequestShortSyntax($request, $xml); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if ('sdk' == $this->_protocol) { |
||
140 | $version = ('' == $this->_version) ? null : $this->_version; |
||
141 | $requestXml = new SimpleXMLElement((string)$request); |
||
142 | $xml = \pm_ApiRpc::getService($version)->call($requestXml->children()[0]->asXml(), $this->_login); |
||
143 | } else { |
||
144 | $xml = $this->_performHttpRequest($request); |
||
|
|||
145 | } |
||
146 | $this->_verifyResponse($xml); |
||
147 | |||
148 | return (self::RESPONSE_FULL == $mode) ? $xml : $xml->xpath('//result')[0]; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Perform HTTP request to end-point |
||
153 | * |
||
154 | * @param string $request |
||
155 | * @return XmlResponse |
||
156 | * @throws Client\Exception |
||
157 | */ |
||
158 | private function _performHttpRequest($request) |
||
159 | { |
||
160 | $curl = curl_init(); |
||
161 | |||
162 | curl_setopt($curl, CURLOPT_URL, "$this->_protocol://$this->_host:$this->_port/enterprise/control/agent.php"); |
||
163 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
164 | curl_setopt($curl, CURLOPT_POST, true); |
||
165 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
||
166 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); |
||
167 | curl_setopt($curl, CURLOPT_HTTPHEADER, $this->_getHeaders()); |
||
168 | curl_setopt($curl, CURLOPT_POSTFIELDS, $request); |
||
169 | |||
170 | $result = curl_exec($curl); |
||
171 | |||
172 | if (false === $result) { |
||
173 | throw new Client\Exception(curl_error($curl), curl_errno($curl)); |
||
174 | } |
||
175 | |||
176 | if (self::$_isExecutionsLogEnabled) { |
||
177 | self::$_executionLog[] = [ |
||
178 | 'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), |
||
179 | 'request' => $request, |
||
180 | 'response' => $result, |
||
181 | ]; |
||
182 | } |
||
183 | |||
184 | curl_close($curl); |
||
185 | |||
186 | $xml = new XmlResponse($result); |
||
187 | return $xml; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Perform multiple API requests using single HTTP request |
||
192 | * |
||
193 | * @param $requests |
||
194 | * @param int $mode |
||
195 | * @return array |
||
196 | * @throws Client\Exception |
||
197 | */ |
||
198 | public function multiRequest($requests, $mode = self::RESPONSE_SHORT) |
||
237 | |||
238 | /** |
||
239 | * Retrieve list of headers needed for request |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | protected function _getHeaders() |
||
259 | |||
260 | /** |
||
261 | * Enable or disable execution log |
||
262 | * |
||
263 | * @param bool $enable |
||
264 | */ |
||
265 | public static function enableExecutionLog($enable = true) |
||
269 | |||
270 | /** |
||
271 | * Retrieve execution log |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | public static function getExecutionLog() |
||
279 | |||
280 | /** |
||
281 | * Verify that response does not contain errors |
||
282 | * |
||
283 | * @param XmlResponse $xml |
||
284 | * @throws Exception |
||
285 | */ |
||
286 | protected function _verifyResponse($xml) |
||
298 | |||
299 | /** |
||
300 | * Expand short syntax (some.method.call) into full XML representation |
||
301 | * |
||
302 | * @param string $request |
||
303 | * @param SimpleXMLElement $xml |
||
304 | * @return string |
||
305 | */ |
||
306 | protected function _expandRequestShortSyntax($request, SimpleXMLElement $xml) |
||
318 | |||
319 | /** |
||
320 | * Convert array to XML representation |
||
321 | * |
||
322 | * @param array $array |
||
323 | * @param SimpleXMLElement $xml |
||
324 | * @return SimpleXMLElement |
||
325 | */ |
||
326 | protected function _arrayToXml(array $array, SimpleXMLElement $xml) |
||
338 | |||
339 | /** |
||
340 | * @param string $name |
||
341 | * @return \PleskX\Api\Operator |
||
342 | */ |
||
343 | protected function _getOperator($name) |
||
352 | |||
353 | /** |
||
354 | * @return Operator\Server |
||
355 | */ |
||
356 | public function server() |
||
360 | |||
361 | /** |
||
362 | * @return Operator\Customer |
||
363 | */ |
||
364 | public function customer() |
||
368 | |||
369 | /** |
||
370 | * @return Operator\Webspace |
||
371 | */ |
||
372 | public function webspace() |
||
376 | |||
377 | /** |
||
378 | * @return Operator\Subdomain |
||
379 | */ |
||
380 | public function subdomain() |
||
384 | |||
385 | /** |
||
386 | * @return Operator\Dns |
||
387 | */ |
||
388 | public function dns() |
||
392 | |||
393 | /** |
||
394 | * @return Operator\DatabaseServer |
||
395 | */ |
||
396 | public function databaseServer() |
||
400 | |||
401 | /** |
||
402 | * @return Operator\Mail |
||
403 | */ |
||
404 | public function mail() |
||
408 | |||
409 | /** |
||
410 | * @return Operator\Migration |
||
411 | */ |
||
412 | public function migration() |
||
416 | |||
417 | /** |
||
418 | * @return Operator\Certificate |
||
419 | */ |
||
420 | public function certificate() |
||
424 | |||
425 | /** |
||
426 | * @return Operator\SiteAlias |
||
427 | */ |
||
428 | public function siteAlias() |
||
432 | |||
433 | /** |
||
434 | * @return Operator\Ip |
||
435 | */ |
||
436 | public function ip() |
||
440 | |||
441 | /** |
||
442 | * @return Operator\EventLog |
||
443 | */ |
||
444 | public function eventLog() |
||
448 | |||
449 | /** |
||
450 | * @return Operator\SpamFilter |
||
451 | */ |
||
452 | public function spamFilter() |
||
456 | |||
457 | /** |
||
458 | * @return Operator\SecretKey |
||
459 | */ |
||
460 | public function secretKey() |
||
464 | |||
465 | /** |
||
466 | * @return Operator\Ui |
||
467 | */ |
||
468 | public function ui() |
||
472 | |||
473 | /** |
||
474 | * @return Operator\ServicePlan |
||
475 | */ |
||
476 | public function servicePlan() |
||
480 | |||
481 | /** |
||
482 | * @return Operator\WebUser |
||
483 | */ |
||
484 | public function webUser() |
||
488 | |||
489 | /** |
||
490 | * @return Operator\MailList |
||
491 | */ |
||
492 | public function mailList() |
||
496 | |||
497 | /** |
||
498 | * @return Operator\VirtualDirectory |
||
499 | */ |
||
500 | public function virtualDirectory() |
||
504 | |||
505 | /** |
||
506 | * @return Operator\Database |
||
507 | */ |
||
508 | public function database() |
||
512 | |||
513 | /** |
||
514 | * @return Operator\FtpUser |
||
515 | */ |
||
516 | public function ftpUser() |
||
520 | |||
521 | /** |
||
522 | * @return Operator\Session |
||
523 | */ |
||
524 | public function session() |
||
528 | |||
529 | /** |
||
530 | * @return Operator\Updater |
||
531 | */ |
||
532 | public function updater() |
||
536 | |||
537 | /** |
||
538 | * @return Operator\Locale |
||
539 | */ |
||
540 | public function locale() |
||
544 | |||
545 | /** |
||
546 | * @return Operator\LogRotation |
||
547 | */ |
||
548 | public function logRotation() |
||
552 | |||
553 | /** |
||
554 | * @return Operator\BackupManager |
||
555 | */ |
||
556 | public function backupManager() |
||
560 | |||
561 | /** |
||
562 | * @return Operator\Sso |
||
563 | */ |
||
564 | public function sso() |
||
568 | |||
569 | /** |
||
570 | * @return Operator\ProtectedDirectory |
||
571 | */ |
||
572 | public function protectedDirectory() |
||
576 | |||
577 | /** |
||
578 | * @return Operator\Reseller |
||
579 | */ |
||
580 | public function reseller() |
||
584 | |||
585 | /** |
||
586 | * @return Operator\ResellerPlan |
||
587 | */ |
||
588 | public function resellerPlan() |
||
592 | |||
593 | /** |
||
594 | * @return Operator\Aps |
||
595 | */ |
||
596 | public function aps() |
||
600 | |||
601 | /** |
||
602 | * @return Operator\ServicePlanAddon |
||
603 | */ |
||
604 | public function servicePlanAddon() |
||
608 | |||
609 | /** |
||
610 | * @return Operator\Site |
||
611 | */ |
||
612 | public function site() |
||
616 | |||
617 | /** |
||
618 | * @return Operator\User |
||
619 | */ |
||
620 | public function user() |
||
624 | |||
625 | /** |
||
626 | * @return Operator\Role |
||
627 | */ |
||
628 | public function role() |
||
632 | |||
633 | /** |
||
634 | * @return Operator\BusinessLogicUpgrade |
||
635 | */ |
||
636 | public function businessLogicUpgrade() |
||
640 | |||
641 | /** |
||
642 | * @return Operator\Webmail |
||
643 | */ |
||
644 | public function webmail() |
||
648 | |||
649 | /** |
||
650 | * @return Operator\PlanItem |
||
651 | */ |
||
652 | public function planItem() |
||
656 | |||
657 | /** |
||
658 | * @return Operator\Sitebuilder |
||
659 | */ |
||
660 | public function sitebuilder() |
||
664 | |||
665 | /** |
||
666 | * @return Operator\ServiceNode |
||
667 | */ |
||
668 | public function serviceNode() |
||
672 | |||
673 | /** |
||
674 | * @return Operator\IpBan |
||
675 | */ |
||
676 | public function ipBan() |
||
680 | |||
681 | /** |
||
682 | * @return Operator\WpInstance |
||
683 | */ |
||
684 | public function wpInstance() |
||
688 | |||
689 | } |
||
690 |