Conditions | 13 |
Paths | 129 |
Total Lines | 93 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
27 | public function execute($request) |
||
28 | { |
||
29 | if($this->checkRequest) { |
||
30 | try { |
||
31 | $request->check(); |
||
32 | } catch (Exception $e) { |
||
33 | $result->code = $e->getCode(); |
||
|
|||
34 | $result->message = $e->getMessage(); |
||
35 | return $result; |
||
36 | } |
||
37 | } |
||
38 | //获取业务参数 |
||
39 | $apiParams = $request->getApiParas(); |
||
40 | //组装系统参数 |
||
41 | $apiParams["AccessKeyId"] = $this->accessKeyId; |
||
42 | $apiParams["Format"] = $this->format;// |
||
43 | $apiParams["SignatureMethod"] = $this->signatureMethod; |
||
44 | $apiParams["SignatureVersion"] = $this->signatureVersion; |
||
45 | $apiParams["SignatureNonce"] = uniqid(); |
||
46 | date_default_timezone_set("GMT"); |
||
47 | $apiParams["TimeStamp"] = date($this->dateTimeFormat); |
||
48 | $apiParams["partner_id"] = $this->sdkVersion; |
||
49 | |||
50 | $apiNameArray = split("\.", $request->getApiMethodName()); |
||
51 | $apiParams["Action"] = $apiNameArray[3]; |
||
52 | $apiParams["Version"] = $apiNameArray[4]; |
||
53 | //签名 |
||
54 | $apiParams["Signature"] = $this->computeSignature($apiParams, $this->accessKeySecret); |
||
55 | |||
56 | //系统参数放入GET请求串 |
||
57 | $requestUrl = rtrim($this->serverUrl,"/") . "/?"; |
||
58 | foreach ($apiParams as $apiParamKey => $apiParamValue) |
||
59 | { |
||
60 | $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&"; |
||
61 | } |
||
62 | $requestUrl = substr($requestUrl, 0, -1); |
||
63 | //发起HTTP请求 |
||
64 | try |
||
65 | { |
||
66 | $resp = $this->curl($requestUrl, null); |
||
67 | } |
||
68 | catch (Exception $e) |
||
69 | { |
||
70 | $this->logCommunicationError($apiParams["Action"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
||
71 | if ("json" == $this->format) |
||
72 | { |
||
73 | return json_decode($e->getMessage()); |
||
74 | } |
||
75 | else if("xml" == $this->format) |
||
76 | { |
||
77 | return @simplexml_load_string($e->getMessage()); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | //解析API返回结果 |
||
82 | $respWellFormed = false; |
||
83 | if ("json" == $this->format) |
||
84 | { |
||
85 | $respObject = json_decode($resp); |
||
86 | if (null !== $respObject) |
||
87 | { |
||
88 | $respWellFormed = true; |
||
89 | } |
||
90 | } |
||
91 | else if("xml" == $this->format) |
||
92 | { |
||
93 | $respObject = @simplexml_load_string($resp); |
||
94 | if (false !== $respObject) |
||
95 | { |
||
96 | $respWellFormed = true; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
||
101 | if (false === $respWellFormed) |
||
102 | { |
||
103 | $this->logCommunicationError($apiParams["Action"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
||
104 | $result->code = 0; |
||
105 | $result->message = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
||
106 | return $result; |
||
107 | } |
||
108 | |||
109 | //如果TOP返回了错误码,记录到业务错误日志中 |
||
110 | if (isset($respObject->code)) |
||
111 | { |
||
112 | $logger = new LtLogger; |
||
113 | $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
||
114 | $logger->log(array( |
||
115 | date("Y-m-d H:i:s"), |
||
116 | $resp |
||
117 | )); |
||
118 | } |
||
119 | return $respObject; |
||
120 | } |
||
256 |