| Conditions | 19 |
| Paths | 1585 |
| Total Lines | 120 |
| Code Lines | 69 |
| 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 |
||
| 220 | public function execute($request, $session = null,$bestUrl = null) |
||
| 221 | { |
||
| 222 | $result = new ResultSet(); |
||
| 223 | if($this->checkRequest) { |
||
| 224 | try { |
||
| 225 | $request->check(); |
||
| 226 | } catch (Exception $e) { |
||
| 227 | |||
| 228 | $result->code = $e->getCode(); |
||
| 229 | $result->msg = $e->getMessage(); |
||
| 230 | return $result; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | //组装系统参数 |
||
| 234 | $sysParams["app_key"] = $this->appkey; |
||
| 235 | $sysParams["v"] = $this->apiVersion; |
||
| 236 | $sysParams["format"] = $this->format; |
||
| 237 | $sysParams["sign_method"] = $this->signMethod; |
||
| 238 | $sysParams["method"] = $request->getApiMethodName(); |
||
| 239 | $sysParams["timestamp"] = date("Y-m-d H:i:s"); |
||
| 240 | if (null != $session) |
||
| 241 | { |
||
| 242 | $sysParams["session"] = $session; |
||
| 243 | } |
||
| 244 | $apiParams = array(); |
||
| 245 | //获取业务参数 |
||
| 246 | $apiParams = $request->getApiParas(); |
||
| 247 | |||
| 248 | |||
| 249 | //系统参数放入GET请求串 |
||
| 250 | if($bestUrl){ |
||
| 251 | $requestUrl = $bestUrl."?"; |
||
| 252 | $sysParams["partner_id"] = $this->getClusterTag(); |
||
| 253 | }else{ |
||
| 254 | $requestUrl = $this->gatewayUrl."?"; |
||
| 255 | $sysParams["partner_id"] = $this->sdkVersion; |
||
| 256 | } |
||
| 257 | //签名 |
||
| 258 | $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams)); |
||
| 259 | |||
| 260 | foreach ($sysParams as $sysParamKey => $sysParamValue) |
||
| 261 | { |
||
| 262 | // if(strcmp($sysParamKey,"timestamp") != 0) |
||
| 263 | $requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&"; |
||
| 264 | } |
||
| 265 | |||
| 266 | $fileFields = array(); |
||
| 267 | foreach ($apiParams as $key => $value) { |
||
| 268 | if(is_array($value) && array_key_exists('type',$value) && array_key_exists('content',$value) ){ |
||
| 269 | $value['name'] = $key; |
||
| 270 | $fileFields[$key] = $value; |
||
| 271 | unset($apiParams[$key]); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | // $requestUrl .= "timestamp=" . urlencode($sysParams["timestamp"]) . "&"; |
||
| 276 | $requestUrl = substr($requestUrl, 0, -1); |
||
| 277 | |||
| 278 | //发起HTTP请求 |
||
| 279 | try |
||
| 280 | { |
||
| 281 | if(count($fileFields) > 0){ |
||
| 282 | $resp = $this->curl_with_memory_file($requestUrl, $apiParams, $fileFields); |
||
| 283 | }else{ |
||
| 284 | $resp = $this->curl($requestUrl, $apiParams); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | catch (Exception $e) |
||
| 288 | { |
||
| 289 | $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_ERROR_" . $e->getCode(),$e->getMessage()); |
||
| 290 | $result->code = $e->getCode(); |
||
| 291 | $result->msg = $e->getMessage(); |
||
| 292 | return $result; |
||
| 293 | } |
||
| 294 | |||
| 295 | unset($apiParams); |
||
| 296 | unset($fileFields); |
||
| 297 | //解析TOP返回结果 |
||
| 298 | $respWellFormed = false; |
||
| 299 | if ("json" == $this->format) |
||
| 300 | { |
||
| 301 | $respObject = json_decode($resp); |
||
| 302 | if (null !== $respObject) |
||
| 303 | { |
||
| 304 | $respWellFormed = true; |
||
| 305 | foreach ($respObject as $propKey => $propValue) |
||
| 306 | { |
||
| 307 | $respObject = $propValue; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | else if("xml" == $this->format) |
||
| 312 | { |
||
| 313 | $respObject = @simplexml_load_string($resp); |
||
| 314 | if (false !== $respObject) |
||
| 315 | { |
||
| 316 | $respWellFormed = true; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | //返回的HTTP文本不是标准JSON或者XML,记下错误日志 |
||
| 321 | if (false === $respWellFormed) |
||
| 322 | { |
||
| 323 | $this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp); |
||
| 324 | $result->code = 0; |
||
| 325 | $result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"; |
||
| 326 | return $result; |
||
| 327 | } |
||
| 328 | |||
| 329 | //如果TOP返回了错误码,记录到业务错误日志中 |
||
| 330 | if (isset($respObject->code)) |
||
| 331 | { |
||
| 332 | $logger = new TopLogger; |
||
| 333 | $logger->conf["log_file"] = rtrim(TOP_SDK_WORK_DIR, '\\/') . '/' . "logs/top_biz_err_" . $this->appkey . "_" . date("Y-m-d") . ".log"; |
||
| 334 | $logger->log(array( |
||
| 335 | date("Y-m-d H:i:s"), |
||
| 336 | $resp |
||
| 337 | )); |
||
| 338 | } |
||
| 339 | return $respObject; |
||
| 340 | } |
||
| 378 |