Total Complexity | 61 |
Total Lines | 381 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like QimenCloudClient 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.
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 QimenCloudClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
2 | class QimenCloudClient |
||
3 | { |
||
4 | public $appkey; |
||
5 | |||
6 | public $secretKey; |
||
7 | |||
8 | public $targetAppkey = ""; |
||
9 | |||
10 | public $gatewayUrl = null; |
||
11 | |||
12 | public $format = "xml"; |
||
13 | |||
14 | public $connectTimeout; |
||
15 | |||
16 | public $readTimeout; |
||
17 | |||
18 | /** 是否打开入参check**/ |
||
19 | public $checkRequest = true; |
||
20 | |||
21 | protected $signMethod = "md5"; |
||
22 | |||
23 | protected $apiVersion = "2.0"; |
||
24 | |||
25 | protected $sdkVersion = "top-sdk-php-20151012"; |
||
26 | |||
27 | public function getAppkey() |
||
28 | { |
||
29 | return $this->appkey; |
||
30 | } |
||
31 | |||
32 | public function __construct($appkey = "",$secretKey = ""){ |
||
33 | $this->appkey = $appkey; |
||
34 | $this->secretKey = $secretKey ; |
||
35 | } |
||
36 | |||
37 | protected function generateSign($params) |
||
38 | { |
||
39 | ksort($params); |
||
40 | |||
41 | $stringToBeSigned = $this->secretKey; |
||
42 | foreach ($params as $k => $v) |
||
43 | { |
||
44 | if(!is_array($v) && "@" != substr($v, 0, 1)) |
||
45 | { |
||
46 | $stringToBeSigned .= "$k$v"; |
||
47 | } |
||
48 | } |
||
49 | unset($k, $v); |
||
50 | $stringToBeSigned .= $this->secretKey; |
||
51 | |||
52 | return strtoupper(md5($stringToBeSigned)); |
||
53 | } |
||
54 | |||
55 | public function curl($url, $postFields = null) |
||
56 | { |
||
57 | $ch = curl_init(); |
||
58 | curl_setopt($ch, CURLOPT_URL, $url); |
||
59 | curl_setopt($ch, CURLOPT_FAILONERROR, false); |
||
60 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
61 | if ($this->readTimeout) { |
||
62 | curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout); |
||
63 | } |
||
64 | if ($this->connectTimeout) { |
||
65 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout); |
||
66 | } |
||
67 | curl_setopt ( $ch, CURLOPT_USERAGENT, "top-sdk-php" ); |
||
68 | //https 请求 |
||
69 | if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { |
||
70 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||
71 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
||
72 | } |
||
73 | |||
74 | if (is_array($postFields) && 0 < count($postFields)) |
||
75 | { |
||
76 | $postBodyString = ""; |
||
77 | $postMultipart = false; |
||
78 | foreach ($postFields as $k => $v) |
||
79 | { |
||
80 | if("@" != substr($v, 0, 1))//判断是不是文件上传 |
||
81 | { |
||
82 | $postBodyString .= "$k=" . urlencode($v) . "&"; |
||
83 | } |
||
84 | else//文件上传用multipart/form-data,否则用www-form-urlencoded |
||
85 | { |
||
86 | $postMultipart = true; |
||
87 | if(class_exists('\CURLFile')){ |
||
88 | $postFields[$k] = new \CURLFile(substr($v, 1)); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | unset($k, $v); |
||
93 | curl_setopt($ch, CURLOPT_POST, true); |
||
94 | if ($postMultipart) |
||
95 | { |
||
96 | if (class_exists('\CURLFile')) { |
||
97 | curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
||
|
|||
98 | } else { |
||
99 | if (defined('CURLOPT_SAFE_UPLOAD')) { |
||
100 | curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); |
||
101 | } |
||
102 | } |
||
103 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); |
||
104 | } |
||
105 | else |
||
106 | { |
||
107 | $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8"); |
||
108 | curl_setopt($ch,CURLOPT_HTTPHEADER,$header); |
||
109 | curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); |
||
110 | } |
||
111 | } |
||
112 | $reponse = curl_exec($ch); |
||
113 | |||
114 | if (curl_errno($ch)) |
||
115 | { |
||
116 | throw new Exception(curl_error($ch),0); |
||
117 | } |
||
118 | else |
||
119 | { |
||
120 | $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
121 | if (200 !== $httpStatusCode) |
||
122 | { |
||
123 | throw new Exception($reponse,$httpStatusCode); |
||
124 | } |
||
125 | } |
||
126 | curl_close($ch); |
||
127 | return $reponse; |
||
128 | } |
||
129 | public function curl_with_memory_file($url, $postFields = null, $fileFields = null) |
||
200 | } |
||
201 | |||
202 | protected function logCommunicationError($apiName, $requestUrl, $errorCode, $responseTxt) |
||
203 | { |
||
204 | $localIp = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : "CLI"; |
||
205 | $logger = new TopLogger; |
||
206 | $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_comm_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
||
207 | $logger->conf["separator"] = "^_^"; |
||
208 | $logData = array( |
||
209 | date("Y-m-d H:i:s"), |
||
210 | $apiName, |
||
211 | $this->appkey, |
||
212 | $localIp, |
||
213 | PHP_OS, |
||
214 | $this->sdkVersion, |
||
215 | $requestUrl, |
||
216 | $errorCode, |
||
217 | str_replace("\n","",$responseTxt) |
||
218 | ); |
||
219 | $logger->log($logData); |
||
220 | } |
||
221 | |||
222 | public function execute($request, $session = null,$bestUrl = null) |
||
347 | } |
||
348 | |||
349 | public function exec($paramsArray) |
||
378 | } |
||
379 | |||
380 | private function getClusterTag() |
||
383 | } |
||
384 | } |
||
385 |