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