Complex classes like CurlSoapClient 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 CurlSoapClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class CurlSoapClient extends SoapClient |
||
19 | { |
||
20 | protected $curl = null; ///< cURL handle |
||
21 | protected $redirect_max; ///< max redirect counts |
||
22 | protected $curl_timeout; ///< cURL request time-out seconds |
||
23 | private $redirect_count = 0; |
||
24 | |||
25 | 13 | public function __construct($wsdl, array $options) |
|
26 | { |
||
27 | 13 | parent::__construct($wsdl, $options); |
|
28 | 13 | $this->redirect_max = 5; |
|
29 | 13 | if (isset($options['redirect_max'])) { |
|
30 | 1 | $this->redirect_max = (int)$options['redirect_max']; |
|
31 | } |
||
32 | 13 | $this->curl_timeout = 30; |
|
33 | 13 | if (isset($options['curl_timeout'])) { |
|
34 | 1 | $this->curl_timeout = (int)$options['curl_timeout']; |
|
35 | } |
||
36 | 13 | $this->curl = curl_init(); |
|
37 | 13 | $this->_cookies = array(); |
|
38 | 13 | } |
|
39 | |||
40 | 13 | public function __destruct() |
|
41 | { |
||
42 | 13 | if (isset($this->curl)) { |
|
43 | 13 | curl_close($this->curl); |
|
44 | } |
||
45 | 13 | } |
|
46 | |||
47 | 1 | public function ___curlSetOpt($option, $value) |
|
48 | { |
||
49 | 1 | curl_setopt($this->curl, $option, $value); |
|
50 | 1 | } |
|
51 | |||
52 | 1 | public function __getCookies() |
|
53 | { |
||
54 | 1 | return $this->_cookies; |
|
55 | } |
||
56 | |||
57 | 1 | public function __setCookie($name, $value = null) |
|
58 | { |
||
59 | 1 | if (!isset($value)) { |
|
60 | 1 | unset($this->_cookies[$name]); |
|
61 | 1 | return; |
|
62 | } |
||
63 | 1 | $this->_cookies[$name] = (array)$value; |
|
64 | 1 | } |
|
65 | |||
66 | /** |
||
67 | * Execute SOAP requests. |
||
68 | * |
||
69 | * @param string $request SOAP request |
||
70 | * @param string $location SOAP address |
||
71 | * @param string $action SOAP action |
||
72 | * @param int $version SOAP version |
||
73 | * @param int $one_way |
||
74 | * @throws \Exception |
||
75 | * @throws \SoapFault |
||
76 | * @return string|object (string) SOAP response / (object) SoapFault object |
||
77 | */ |
||
78 | 13 | public function __doRequest($request, $location, $action, $version, $one_way = 0) |
|
79 | { |
||
80 | 13 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
|
81 | 13 | curl_setopt($this->curl, CURLOPT_HEADER, true); |
|
82 | 13 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $request); |
|
83 | 13 | if (isset($this->trace) && $this->trace) { |
|
84 | 3 | curl_setopt($this->curl, CURLINFO_HEADER_OUT, true); |
|
85 | } |
||
86 | |||
87 | 13 | $this->___configHeader($action, $version); |
|
88 | 13 | $this->___configCompression(); |
|
89 | 13 | $this->___configTimeout(); |
|
90 | 13 | if (!$this->___isEmptyExtProperty('_user_agent')) { |
|
91 | 1 | curl_setopt($this->curl, CURLOPT_USERAGENT, $this->_user_agent); |
|
92 | } |
||
93 | 13 | $this->___configHttpAuthentication(); |
|
94 | 13 | $this->___configProxy(); |
|
95 | 13 | if (!$this->___isEmptyExtProperty('_ssl_method')) { |
|
96 | 1 | switch ($this->_ssl_method) { |
|
97 | 1 | case SOAP_SSL_METHOD_SSLv2: |
|
98 | curl_setopt($this->curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv2); |
||
99 | break; |
||
100 | 1 | case SOAP_SSL_METHOD_SSLv3: |
|
101 | curl_setopt($this->curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3); |
||
102 | break; |
||
103 | default: |
||
104 | 1 | curl_setopt($this->curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT); |
|
105 | 1 | break; |
|
106 | } |
||
107 | } |
||
108 | |||
109 | try { |
||
110 | 13 | $response = $this->___curlCall($location); |
|
111 | 7 | } catch (SoapFault $fault) { |
|
112 | 7 | if (isset($this->_exceptions) && empty($this->_exceptions)) { |
|
113 | // if exceptions option is false, return SoapFault object |
||
114 | 1 | return $fault; |
|
115 | } |
||
116 | 6 | throw $fault; |
|
117 | } |
||
118 | |||
119 | 6 | if ($one_way) { |
|
120 | return ''; |
||
121 | } |
||
122 | |||
123 | 6 | return $response; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * set CURLOPT_HTTPHEADER. |
||
128 | * |
||
129 | * @param string $action SOAP action |
||
130 | * @param int $version SOAP version |
||
131 | */ |
||
132 | 13 | private function ___configHeader($action, $version) |
|
148 | |||
149 | /** |
||
150 | * set CURLOPT_ENCODING. |
||
151 | */ |
||
152 | 13 | private function ___configCompression() |
|
165 | |||
166 | /** |
||
167 | * set CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT. |
||
168 | */ |
||
169 | 13 | private function ___configTimeout() |
|
170 | { |
||
178 | |||
179 | /** |
||
180 | * set CURLOPT_USERPWD and CURLOPT_HTTPAUTH. |
||
181 | */ |
||
182 | 13 | private function ___configHttpAuthentication() |
|
194 | |||
195 | /** |
||
196 | * set proxy options. |
||
197 | */ |
||
198 | 13 | private function ___configProxy() |
|
218 | |||
219 | /** |
||
220 | * Request cURL. |
||
221 | * |
||
222 | * @param string $location SOAP address |
||
223 | * @param string $location |
||
224 | * @throws \SoapFault |
||
225 | * @return mixed response body |
||
226 | */ |
||
227 | 13 | private function ___curlCall($location) |
|
287 | |||
288 | /** |
||
289 | * check body is XML or not |
||
290 | * |
||
291 | * @param string $response_body server response body |
||
292 | * @return boolean |
||
293 | */ |
||
294 | 4 | private function ___isErrorResponse($response_body) { |
|
313 | |||
314 | |||
315 | /** |
||
316 | * SoapClient property util |
||
317 | * |
||
318 | * @param string $property property name |
||
319 | * @return boolean |
||
320 | */ |
||
321 | 13 | private function ___isEmptyExtProperty($property) |
|
332 | } |
||
333 |