Completed
Push — master ( c52910...66a65c )
by lyu
02:53 queued 47s
created

AopClient   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 152
Duplicated Lines 41.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 63
loc 152
ccs 0
cts 87
cp 0
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
C requestPrepare() 31 75 10
B responseHandle() 32 57 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: kaylv <[email protected]>
5
 * Date: 2019/10/16
6
 * Time: 11:05
7
 */
8
9
namespace Kaylyu\Alipay\F2fpay\Kernel;
10
11
use Kaylyu\Alipay\F2fpay\Base\Aop\AopClient as BaseAopClient;
12
use Kaylyu\Alipay\Kernel\Exceptions\Exception;
13
14
class AopClient extends BaseAopClient
15
{
16
    /**
17
     * 准备请求参数
18
     * @param $request
19
     * @param null $authToken
20
     * @param null $appInfoAuthtoken
21
     * @author kaylv <[email protected]>
22
     * @return array
23
     * @throws Exception
24
     */
25
    public function requestPrepare($request, $authToken = null, $appInfoAuthtoken = null)
26
    {
27
28
        $this->setupCharsets($request);
29
30
        //		//  如果两者编码不一致,会出现签名验签或者乱码
31 View Code Duplication
        if (strcasecmp($this->fileCharset, $this->postCharset)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
33
            // writeLog("本地文件字符集编码与表单提交编码不一致,请务必设置成一样,属性名分别为postCharset!");
34
            throw new Exception("文件编码:[" . $this->fileCharset . "] 与表单提交编码:[" . $this->postCharset . "]两者不一致!");
35
        }
36
37
        $iv = null;
0 ignored issues
show
Unused Code introduced by
$iv is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
39
        if (!$this->checkEmpty($request->getApiVersion())) {
40
            $iv = $request->getApiVersion();
41
        } else {
42
            $iv = $this->apiVersion;
43
        }
44
45
        //组装系统参数
46
        $sysParams["app_id"] = $this->appId;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$sysParams was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sysParams = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
47
        $sysParams["version"] = $iv;
48
        $sysParams["format"] = $this->format;
49
        $sysParams["sign_type"] = $this->signType;
50
        $sysParams["method"] = $request->getApiMethodName();
51
        $sysParams["timestamp"] = date("Y-m-d H:i:s");
52
        $sysParams["auth_token"] = $authToken;
53
        $sysParams["alipay_sdk"] = $this->alipaySdkVersion;
54
        $sysParams["terminal_type"] = $request->getTerminalType();
55
        $sysParams["terminal_info"] = $request->getTerminalInfo();
56
        $sysParams["prod_code"] = $request->getProdCode();
57
        $sysParams["notify_url"] = $request->getNotifyUrl();
58
        $sysParams["charset"] = $this->postCharset;
59
        $sysParams["app_auth_token"] = $appInfoAuthtoken;
60
61
        //获取业务参数
62
        $apiParams = $request->getApiParas();
63
64 View Code Duplication
        if (method_exists($request, "getNeedEncrypt") && $request->getNeedEncrypt()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
66
            $sysParams["encrypt_type"] = $this->encryptType;
67
68
            if ($this->checkEmpty($apiParams['biz_content'])) {
69
70
                throw new Exception(" api request Fail! The reason : encrypt request is not supperted!");
71
            }
72
73
            if ($this->checkEmpty($this->encryptKey) || $this->checkEmpty($this->encryptType)) {
74
75
                throw new Exception(" encryptType and encryptKey must not null! ");
76
            }
77
78
            if ("AES" != $this->encryptType) {
79
80
                throw new Exception("加密类型只支持AES");
81
            }
82
83
            // 执行加密
84
            $enCryptContent = \Kaylyu\Alipay\F2fpay\Base\Aop\encrypt($apiParams['biz_content'], $this->encryptKey);
85
            $apiParams['biz_content'] = $enCryptContent;
86
        }
87
        //签名
88
        $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams), $this->signType);
89
90
91
        //系统参数放入GET请求串
92
        $requestUrl = $this->gatewayUrl . "?";
93 View Code Duplication
        foreach ($sysParams as $sysParamKey => $sysParamValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
            $requestUrl .= "$sysParamKey=" . urlencode($this->characet($sysParamValue, $this->postCharset)) . "&";
95
        }
96
        $requestUrl = substr($requestUrl, 0, -1);
97
98
        return [$requestUrl, $apiParams];
99
    }
100
101
    /**
102
     * 处理响应
103
     * @param $request
104
     * @param $response
105
     * @author kaylv <[email protected]>
106
     * @return bool|mixed|\SimpleXMLElement
107
     */
108
    public function responseHandle($request, $response)
109
    {
110
        //解析AOP返回结果
111
        $respWellFormed = false;
112
113
        // 将返回结果转换本地文件编码
114
        $r = iconv($this->postCharset, $this->fileCharset . "//IGNORE", $response);
115
116
        //存放数据
117
        $signData = null;
118
        $respObject = null;
119
120 View Code Duplication
        if ("json" == $this->format) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
122
            $respObject = json_decode($r);
123
            if (null !== $respObject) {
124
                $respWellFormed = true;
125
                $signData = $this->parserJSONSignData($request, $response, $respObject);
126
            }
127
        } else {
128
            if ("xml" == $this->format) {
129
130
                $respObject = @ simplexml_load_string($response);
131
                if (false !== $respObject) {
132
                    $respWellFormed = true;
133
134
                    $signData = $this->parserXMLSignData($request, $response);
135
                }
136
            }
137
        }
138
139
        //返回的HTTP文本不是标准JSON或者XML,记下错误日志
140
        if (false === $respWellFormed) {
141
            return false;
142
        }
143
144
        // 验签
145
        $this->checkResponseSign($request, $signData, $response, $respObject);
146
147
        // 解密
148 View Code Duplication
        if (method_exists($request, "getNeedEncrypt") && $request->getNeedEncrypt()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
            if ("json" == $this->format) {
150
                $resp = $this->encryptJSONSignSource($request, $response);
151
152
                // 将返回结果转换本地文件编码
153
                $r = iconv($this->postCharset, $this->fileCharset . "//IGNORE", $resp);
154
                $respObject = json_decode($r);
155
            } else {
156
                $resp = $this->encryptXMLSignSource($request, $response);
157
158
                $r = iconv($this->postCharset, $this->fileCharset . "//IGNORE", $resp);
159
                $respObject = @ simplexml_load_string($r);
160
            }
161
        }
162
163
        return $respObject;
164
    }
165
}