| Total Complexity | 71 |
| Total Lines | 666 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Support 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 Support, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Support |
||
| 27 | { |
||
| 28 | |||
| 29 | use HttpRequest; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Instance. |
||
| 33 | * |
||
| 34 | * @var Support |
||
| 35 | */ |
||
| 36 | private static $instance; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Config. |
||
| 40 | * |
||
| 41 | * @var Config |
||
| 42 | */ |
||
| 43 | public static $config; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Charset |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public static $fileCharset = 'UTF-8'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public static $respCharset = 'UTF-8'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var float |
||
| 59 | */ |
||
| 60 | public $connectTimeout = 6.0; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | public $baseUri = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var float |
||
| 69 | */ |
||
| 70 | public $timeout = 6.0; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @static getInstance |
||
| 74 | * |
||
| 75 | * @return Support |
||
| 76 | * |
||
| 77 | * @author liuml <[email protected]> |
||
| 78 | * @DateTime 2019-04-09 10:13 |
||
| 79 | */ |
||
| 80 | public static function getInstance() |
||
| 81 | { |
||
| 82 | if (php_sapi_name() === 'cli' || !(self::$instance instanceof self)) { |
||
| 83 | self::$instance = new self(); |
||
| 84 | self::$instance->setHttpOptions(); |
||
| 85 | } |
||
| 86 | |||
| 87 | return self::$instance; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * setHttpOptions |
||
| 92 | */ |
||
| 93 | protected function setHttpOptions() |
||
| 94 | { |
||
| 95 | if (self::$config->has('http') && is_array(self::$config->get('http'))) { |
||
| 96 | self::$config->offsetUnset('http.base_uri'); |
||
| 97 | $this->httpOptions = self::$config->get('http'); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @static createConfig |
||
| 103 | * |
||
| 104 | * @param $config |
||
| 105 | * |
||
| 106 | * @return Config |
||
| 107 | * |
||
| 108 | * @author liuml <[email protected]> |
||
| 109 | * @DateTime 2019-04-08 17:17 |
||
| 110 | */ |
||
| 111 | public static function createConfig($config) |
||
| 112 | { |
||
| 113 | self::$config = new Config($config); |
||
| 114 | |||
| 115 | return self::$config; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @static getConfig |
||
| 120 | * |
||
| 121 | * @param string $key |
||
| 122 | * @param string $default |
||
| 123 | * |
||
| 124 | * @return mixed |
||
| 125 | * |
||
| 126 | * @author liuml <[email protected]> |
||
| 127 | * @DateTime 2019-04-08 17:17 |
||
| 128 | */ |
||
| 129 | public static function getConfig($key = '', $default = '') |
||
| 130 | { |
||
| 131 | if ($key === '') { |
||
| 132 | return self::$config->get(); |
||
| 133 | } |
||
| 134 | |||
| 135 | if (self::$config->offsetExists($key)) { |
||
| 136 | return self::$config[$key]; |
||
| 137 | } |
||
| 138 | |||
| 139 | return $default; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @static generateSign |
||
| 144 | * |
||
| 145 | * @param array $params |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | * |
||
| 149 | * @throws Exceptions\InvalidArgumentException |
||
| 150 | * |
||
| 151 | * @author liuml <[email protected]> |
||
| 152 | * @DateTime 2019-04-09 17:24 |
||
| 153 | */ |
||
| 154 | public static function generateSign(array $params): string |
||
| 155 | { |
||
| 156 | $privateKey = self::getConfig('private_key'); |
||
| 157 | if (!is_string($privateKey)) { |
||
| 158 | throw new Exceptions\InvalidArgumentException('请检查 [ private_key ] 配置项的私钥文件格式或路径是否正确,只接受字符串类型值'); |
||
| 159 | } |
||
| 160 | $keyFromFile = Str::endsWith($privateKey, '.pem'); |
||
| 161 | if ($keyFromFile) { |
||
| 162 | $res = openssl_pkey_get_private('file://' . $privateKey); |
||
| 163 | } else { |
||
| 164 | $res = "-----BEGIN RSA PRIVATE KEY-----\n" . |
||
| 165 | wordwrap( |
||
| 166 | $privateKey, |
||
| 167 | 64, |
||
| 168 | "\n", |
||
| 169 | true |
||
| 170 | ) . "\n-----END RSA PRIVATE KEY-----"; |
||
| 171 | } |
||
| 172 | |||
| 173 | if (!$res) { |
||
| 174 | throw new Exceptions\InvalidArgumentException('支付宝RSA私钥错误。请检查 [ private_key ] 配置项的私钥文件格式或路径是否正确'); |
||
| 175 | } |
||
| 176 | |||
| 177 | $data = self::getSignContent($params); |
||
| 178 | if ("RSA2" == self::getConfig('sign_type', 'RSA2')) { |
||
| 179 | openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256); |
||
| 180 | } else { |
||
| 181 | openssl_sign($data, $sign, $res); |
||
| 182 | } |
||
| 183 | |||
| 184 | $sign = base64_encode($sign); |
||
| 185 | if ($keyFromFile) { |
||
| 186 | // 释放资源 |
||
| 187 | openssl_free_key($res); |
||
|
|
|||
| 188 | } |
||
| 189 | |||
| 190 | return $sign; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @static getSignContent |
||
| 195 | * |
||
| 196 | * @param $params |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | * |
||
| 200 | * @author liuml <[email protected]> |
||
| 201 | * @DateTime 2019-04-08 21:03 |
||
| 202 | */ |
||
| 203 | public static function getSignContent($params) |
||
| 204 | { |
||
| 205 | ksort($params); |
||
| 206 | |||
| 207 | $stringToBeSigned = ""; |
||
| 208 | |||
| 209 | foreach ($params as $k => $v) { |
||
| 210 | if ($v !== '' && !is_null($v) && $k != 'sign' && '@' != substr($v, 0, 1)) { |
||
| 211 | $v = self::characet($v, $params['charset'] ?? 'utf-8'); |
||
| 212 | |||
| 213 | $stringToBeSigned .= $k . '=' . $v . '&'; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | unset($k, $v); |
||
| 217 | |||
| 218 | return rtrim($stringToBeSigned, '&'); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @static getSignContentUrlencode |
||
| 223 | * |
||
| 224 | * @param $params |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | * |
||
| 228 | * @author liuml <[email protected]> |
||
| 229 | * @DateTime 2019-04-11 11:56 |
||
| 230 | */ |
||
| 231 | public static function getSignContentUrlencode($params) |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @static verifySign |
||
| 252 | * |
||
| 253 | * @param string $data |
||
| 254 | * @param string $sign |
||
| 255 | * @param null|string $sign_type |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | * |
||
| 259 | * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException |
||
| 260 | */ |
||
| 261 | public static function verifySign(string $data, string $sign, $sign_type = null): bool |
||
| 262 | { |
||
| 263 | $publicKey = self::getConfig('ali_public_key'); |
||
| 264 | if (!is_string($publicKey)) { |
||
| 265 | throw new Exceptions\InvalidArgumentException('请检查 [ ali_public_key ] 配置项的公钥文件格式或路径是否正确,只支持字符串类型参数'); |
||
| 266 | } |
||
| 267 | $keyFromFile = Str::endsWith($publicKey, '.pem'); |
||
| 268 | if ($keyFromFile) { |
||
| 269 | $res = openssl_pkey_get_public("file://" . $publicKey); |
||
| 270 | } else { |
||
| 271 | if ($publicKey) { |
||
| 272 | $res = "-----BEGIN PUBLIC KEY-----\n" . |
||
| 273 | wordwrap( |
||
| 274 | $publicKey, |
||
| 275 | 64, |
||
| 276 | "\n", |
||
| 277 | true |
||
| 278 | ) . "\n-----END PUBLIC KEY-----"; |
||
| 279 | } else { |
||
| 280 | $res = false; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | if (!$res) { |
||
| 285 | Events::dispatch( |
||
| 286 | SignFailed::NAME, |
||
| 287 | new SignFailed( |
||
| 288 | self::$config->get('event.driver'), |
||
|
1 ignored issue
–
show
|
|||
| 289 | self::$config->get('event.method'), |
||
|
1 ignored issue
–
show
|
|||
| 290 | [$data], |
||
| 291 | '支付宝RSA公钥错误。请检查 [ ali_public_key ] 配置项的公钥文件格式或路径是否正确' |
||
| 292 | ) |
||
| 293 | ); |
||
| 294 | throw new Exceptions\InvalidArgumentException('支付宝RSA公钥错误。请检查 [ ali_public_key ] 配置项的公钥文件格式或路径是否正确'); |
||
| 295 | } |
||
| 296 | $sign_type = $sign_type === null ? self::getConfig('sign_type', 'RSA2') : $sign_type; |
||
| 297 | // 调用openssl内置方法验签,返回bool值 |
||
| 298 | if ("RSA2" == $sign_type) { |
||
| 299 | $result = (openssl_verify( |
||
| 300 | $data, |
||
| 301 | base64_decode($sign), |
||
| 302 | $res, |
||
| 303 | OPENSSL_ALGO_SHA256 |
||
| 304 | ) === 1); |
||
| 305 | } else { |
||
| 306 | $result = (openssl_verify($data, base64_decode($sign), $res) === 1); |
||
| 307 | } |
||
| 308 | |||
| 309 | if ($keyFromFile) { |
||
| 310 | // 释放资源 |
||
| 311 | openssl_free_key($res); |
||
| 312 | } |
||
| 313 | |||
| 314 | return $result; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @static characet |
||
| 319 | * |
||
| 320 | * @param $data |
||
| 321 | * @param $targetCharset |
||
| 322 | * |
||
| 323 | * @return false|string|string[]|null |
||
| 324 | * |
||
| 325 | * @author liuml <[email protected]> |
||
| 326 | * @DateTime 2019-04-08 21:03 |
||
| 327 | */ |
||
| 328 | public static function characet($data, $targetCharset) |
||
| 329 | { |
||
| 330 | if (!empty($data)) { |
||
| 331 | $data = mb_convert_encoding( |
||
| 332 | $data, |
||
| 333 | $targetCharset, |
||
| 334 | self::$fileCharset |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | |||
| 338 | return $data; |
||
| 339 | } |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * @static requestApi |
||
| 344 | * |
||
| 345 | * @param $gatewayUrl |
||
| 346 | * @param array $data |
||
| 347 | * |
||
| 348 | * @return AccessData |
||
| 349 | * |
||
| 350 | * @throws Exceptions\BusinessException |
||
| 351 | * @throws Exceptions\InvalidArgumentException |
||
| 352 | * @throws SignException |
||
| 353 | * |
||
| 354 | * @author liuml <[email protected]> |
||
| 355 | * @DateTime 2019-04-11 10:38 |
||
| 356 | */ |
||
| 357 | public static function requestApi($gatewayUrl, array $data): AccessData |
||
| 358 | { |
||
| 359 | Events::dispatch( |
||
| 360 | ApiRequestStart::NAME, |
||
| 361 | new ApiRequestStart( |
||
| 362 | self::$config->get('event.driver'), |
||
|
1 ignored issue
–
show
|
|||
| 363 | self::$config->get('event.method'), |
||
| 364 | $gatewayUrl, |
||
| 365 | $data |
||
| 366 | ) |
||
| 367 | ); |
||
| 368 | $data = array_filter($data, function ($value) { |
||
| 369 | return ($value == '' || is_null($value)) ? false : true; |
||
| 370 | }); |
||
| 371 | // 请求支付宝网关 |
||
| 372 | $resp = self::getInstance()->post($gatewayUrl, $data); |
||
| 373 | |||
| 374 | self::$respCharset = mb_detect_encoding($resp, "UTF-8, GBK, GB2312"); |
||
| 375 | |||
| 376 | // 将返回结果转换本地文件编码 |
||
| 377 | $result = iconv(self::$respCharset, self::$fileCharset . "//IGNORE", $resp); |
||
| 378 | |||
| 379 | Events::dispatch( |
||
| 380 | ApiRequestEnd::NAME, |
||
| 381 | new ApiRequestEnd( |
||
| 382 | self::$config->get('event.driver'), |
||
|
1 ignored issue
–
show
|
|||
| 383 | self::$config->get('event.method'), |
||
|
1 ignored issue
–
show
|
|||
| 384 | $gatewayUrl, |
||
| 385 | $data, |
||
| 386 | $result |
||
| 387 | ) |
||
| 388 | ); |
||
| 389 | |||
| 390 | return self::processingApiResult($data, $result); |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @static processingApiResult |
||
| 395 | * |
||
| 396 | * @param $data |
||
| 397 | * @param $resp |
||
| 398 | * |
||
| 399 | * @return AccessData |
||
| 400 | * |
||
| 401 | * @throws Exceptions\BusinessException |
||
| 402 | * @throws Exceptions\InvalidArgumentException |
||
| 403 | * @throws SignException |
||
| 404 | * |
||
| 405 | * @author liuml <[email protected]> |
||
| 406 | * @DateTime 2019-04-11 10:38 |
||
| 407 | */ |
||
| 408 | protected static function processingApiResult($data, $resp): AccessData |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @static assemblyProgram |
||
| 485 | * |
||
| 486 | * @param $gatewayUrl |
||
| 487 | * @param array $data |
||
| 488 | * @param string $httpmethod |
||
| 489 | * |
||
| 490 | * @return Response |
||
| 491 | * |
||
| 492 | * @author liuml <[email protected]> |
||
| 493 | * @DateTime 2019-04-12 09:54 |
||
| 494 | */ |
||
| 495 | public static function assemblyProgram($gatewayUrl, array $data, $httpmethod = 'POST'): Response |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @static buildRequestForm |
||
| 513 | * |
||
| 514 | * @param $gatewayUrl |
||
| 515 | * @param $para_temp |
||
| 516 | * |
||
| 517 | * @return string |
||
| 518 | * |
||
| 519 | * @author liuml <[email protected]> |
||
| 520 | * @DateTime 2019-04-10 15:13 |
||
| 521 | */ |
||
| 522 | protected static function buildRequestForm($gatewayUrl, $para_temp) |
||
| 523 | { |
||
| 524 | $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='" |
||
| 525 | . $gatewayUrl . "' method='POST'>"; |
||
| 526 | |||
| 527 | foreach ($para_temp as $key => $val) { |
||
| 528 | if (!is_null($val)) { |
||
| 529 | //$val = $this->characet($val, $this->postCharset); |
||
| 530 | $val = str_replace("'", "'", $val); |
||
| 531 | //$val = str_replace("\"",""",$val); |
||
| 532 | $sHtml .= "<input type='hidden' name='" . $key . "' value='" |
||
| 533 | . $val . "'/>"; |
||
| 534 | } |
||
| 535 | } |
||
| 536 | |||
| 537 | //submit按钮控件请不要含有name属性 |
||
| 538 | $sHtml = $sHtml |
||
| 539 | . "<input type='submit' value='ok' style='display:none;'></form>"; |
||
| 540 | |||
| 541 | $sHtml = $sHtml |
||
| 542 | . "<script>document.forms['alipaysubmit'].submit();</script>"; |
||
| 543 | |||
| 544 | return $sHtml; |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @static executeApi |
||
| 549 | * |
||
| 550 | * @param $params |
||
| 551 | * @param $method |
||
| 552 | * |
||
| 553 | * @return AccessData |
||
| 554 | * |
||
| 555 | * @throws Exceptions\BusinessException |
||
| 556 | * @throws Exceptions\InvalidArgumentException |
||
| 557 | * @throws SignException |
||
| 558 | * |
||
| 559 | * @author liuml <[email protected]> |
||
| 560 | * @DateTime 2019-04-12 10:06 |
||
| 561 | */ |
||
| 562 | public static function executeApi($params, $method) |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * 页面提交执行方法 |
||
| 585 | * |
||
| 586 | * @param array $params 跳转类接口的request; $httpmethod 提交方式。两个值可选:post、get |
||
| 587 | * @param string $method 构建好的、签名后的最终跳转URL(GET)或String形式的form(POST) |
||
| 588 | * |
||
| 589 | * @return Response |
||
| 590 | * |
||
| 591 | * @throws Exceptions\InvalidArgumentException |
||
| 592 | * |
||
| 593 | * @author liuml <[email protected]> |
||
| 594 | * @DateTime 2019-04-12 09:59 |
||
| 595 | */ |
||
| 596 | public static function executePage($params, $method) |
||
| 597 | { |
||
| 598 | // 请求跳转类接口,返回字符串组装格式get url或post表单形式,默认POST形式 |
||
| 599 | $http_method = 'POST'; |
||
| 600 | if (isset($params['http_method'])) { |
||
| 601 | $http_method = isset($params['http_method']) |
||
| 602 | ? $params['http_method'] : 'POST'; |
||
| 603 | unset($params['http_method']); |
||
| 604 | } |
||
| 605 | // 获取公共参数 |
||
| 606 | $payload = self::$config->get('payload'); |
||
| 607 | // 设置方法 |
||
| 608 | $payload['method'] = $method; |
||
| 609 | // 设置业务参数 |
||
| 610 | $payload['biz_content'] = json_encode($params); |
||
| 611 | // 过滤空值 |
||
| 612 | $payload = array_filter($payload, function ($value) { |
||
|
1 ignored issue
–
show
|
|||
| 613 | return $value !== '' && !is_null($value); |
||
| 614 | }); |
||
| 615 | // 设置签名 |
||
| 616 | $payload['sign'] = self::generateSign($payload); |
||
| 617 | // 获取支付宝网关地址 |
||
| 618 | $base_uri = self::getConfig('base_uri'); |
||
| 619 | |||
| 620 | // 生成客户端需要的表单或者url字符串 |
||
| 621 | return self::assemblyProgram($base_uri, $payload, $http_method); |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * 生成用于调用收银台SDK的字符串 |
||
| 626 | * |
||
| 627 | * @param array $params SDK接口的请求参数对象 |
||
| 628 | * @param $method |
||
| 629 | * |
||
| 630 | * @return Response |
||
| 631 | * |
||
| 632 | * @throws \WannanBigPig\Supports\Exceptions\InvalidArgumentException |
||
| 633 | */ |
||
| 634 | public static function executeSdk($params, $method): Response |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @static notifyVerify |
||
| 659 | * |
||
| 660 | * @param mixed $data |
||
| 661 | * |
||
| 662 | * @return bool |
||
| 663 | * |
||
| 664 | * @throws Exceptions\InvalidArgumentException |
||
| 665 | */ |
||
| 666 | public static function notifyVerify($data = null) |
||
| 679 | ); |
||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @static getRequest |
||
| 684 | * |
||
| 685 | * @return array |
||
| 686 | */ |
||
| 687 | public static function getRequest() |
||
| 692 | } |
||
| 693 | } |
||
| 694 |