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