REST   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 256
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 26
c 0
b 0
f 0
lcom 1
cbo 0
dl 16
loc 256
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A setAccount() 0 5 1
A setAppId() 0 4 1
B curl_post() 0 28 4
C sendTemplateSMS() 11 48 7
B voiceVerify() 5 43 4
C accAuth() 0 47 7

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
0 ignored issues
show
Security Bug introduced by
It is not recommended to output anything before PHP's opening tag in non-template files.
Loading history...
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 1.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
<?php
3
/*
4
 *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
5
 *
6
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
7
 *  that can be found in the LICENSE file in the root of the web site.
8
 *
9
 *   http://www.yuntongxun.com
10
 *
11
 *  An additional intellectual property rights grant can be found
12
 *  in the file PATENTS.  All contributing project authors may
13
 *  be found in the AUTHORS file in the root of the source tree.
14
 */
15
16
class REST
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    private $AccountSid;
19
    private $AccountToken;
20
    private $AppId;
21
    private $ServerIP;
22
    private $ServerPort;
23
    private $SoftVersion;
24
    private $Batch;  //时间戳
25
    private $BodyType = 'xml'; //包体格式,可填值:json 、xml
26
27
    public function __construct($ServerIP, $ServerPort, $SoftVersion, $BodyType = 'xml')
28
    {
29
        $this->Batch = date('YmdHis');
30
        $this->ServerIP = $ServerIP;
31
        $this->ServerPort = $ServerPort;
32
        $this->SoftVersion = $SoftVersion;
33
        if (in_array($BodyType, ['xml', 'json'])) {
34
            $this->BodyType = $BodyType;
35
        }
36
    }
37
38
    /**
39
     * 设置主帐号
40
     *
41
     * @param string $AccountSid   主帐号
42
     * @param string $AccountToken 主帐号Token
43
     */
44
    public function setAccount($AccountSid, $AccountToken)
45
    {
46
        $this->AccountSid = $AccountSid;
47
        $this->AccountToken = $AccountToken;
48
    }
49
50
    /**
51
     * 设置应用ID
52
     *
53
     * @param string $AppId 应用ID
54
     */
55
    public function setAppId($AppId)
56
    {
57
        $this->AppId = $AppId;
58
    }
59
60
     /**
61
      * 发起HTTPS请求
62
      *
63
      * @param string $url
64
      * @param mixed $data
65
      * @param mixed $header
66
      * @param mixed $post
67
      *
68
      * @return mixed
69
      */
70
     public function curl_post($url, $data, $header, $post = 1)
71
     {
72
         //初始化curl
73
         $ch = curl_init();
74
         //参数设置
75
         $res = curl_setopt($ch, CURLOPT_URL, $url);
0 ignored issues
show
Unused Code introduced by
$res 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...
76
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
77
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
78
         curl_setopt($ch, CURLOPT_HEADER, 0);
79
         curl_setopt($ch, CURLOPT_POST, $post);
80
         if ($post) {
81
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
82
         }
83
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
84
         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
85
         $result = curl_exec($ch);
86
         //连接失败
87
         if ($result === false) {
88
             if ($this->BodyType === 'json') {
89
                 $result = '{"statusCode":"172001","statusMsg":"网络错误"}';
90
             } else {
91
                 $result = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>';
92
             }
93
         }
94
         curl_close($ch);
95
96
         return $result;
97
     }
98
99
    /**
100
     * 发送模板短信
101
     *
102
     * @param string $to
103
     *                       短信接收彿手机号码集合,用英文逗号分开
104
     * @param array  $datas
105
     *                       内容数据
106
     * @param mixed  $tempId
107
     *                       模板Id
108
     *
109
     * @return mixed
110
     */
111
    public function sendTemplateSMS($to, $datas, $tempId)
112
    {
113
        //主帐号鉴权信息验证,对必选参数进行判空。
114
        $auth = $this->accAuth();
115
        if ($auth !== true) {
116
            return $auth;
117
        }
118
        // 拼接请求包体
119
        if ($this->BodyType === 'json') {
120
            $data = '';
121 View Code Duplication
            for ($i = 0; $i < count($datas); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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...
122
                $data = $data . "'" . $datas[$i] . "',";
123
            }
124
            $body = "{'to':'$to','templateId':'$tempId','appId':'$this->AppId','datas':[" . $data . ']}';
125
        } else {
126
            $data = '';
127 View Code Duplication
            for ($i = 0; $i < count($datas); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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...
128
                $data = $data . '<data>' . $datas[$i] . '</data>';
129
            }
130
            $body = "<TemplateSMS>
131
                    <to>$to</to> 
132
                    <appId>$this->AppId</appId>
133
                    <templateId>$tempId</templateId>
134
                    <datas>" . $data . '</datas>
135
                  </TemplateSMS>';
136
        }
137
        // 大写的sig参数
138
        $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
139
        // 生成请求URL
140
        $url = "https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig";
141
        // 生成授权:主帐户Id + 英文冒号 + 时间戳。
142
        $authen = base64_encode($this->AccountSid . ':' . $this->Batch);
143
        // 生成包头
144
        $header = array("Accept:application/$this->BodyType", "Content-Type:application/$this->BodyType;charset=utf-8", "Authorization:$authen");
145
        // 发送请求
146
        $result = $this->curl_post($url, $body, $header);
147 View Code Duplication
        if ($this->BodyType === 'json') {//JSON格式
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...
148
           $datas = json_decode($result);
149
        } else { //xml格式
150
           $datas = simplexml_load_string(trim($result, " \t\n\r"));
151
        }
152
        // 重新装填数据
153
        if (isset($datas->templateSMS)) {
154
            $datas->TemplateSMS = $datas->templateSMS;
155
        }
156
157
        return $datas;
158
    }
159
160
    /**
161
     * 语音验证码
162
     *
163
     * @param mixed $verifyCode     验证码内容,为数字和英文字母,不区分大小写,长度4-8位
164
     * @param mixed $playTimes      播放次数,1-3次
165
     * @param mixed $to             接收号码
166
     * @param mixed $displayNum     显示的主叫号码
167
     * @param mixed $respUrl        语音验证码状态通知回调地址,云通讯平台将向该Url地址发送呼叫结果通知
168
     * @param mixed $lang           语言类型
169
     * @param mixed $userData       第三方私有数据
170
     * @param mixed $welcomePrompt  欢迎提示音,在播放验证码语音前播放此内容(语音文件格式为wav)
171
     * @param mixed $playVerifyCode 语音验证码的内容全部播放此节点下的全部语音文件
172
     *
173
     * @return mixed
174
     */
175
    public function voiceVerify($verifyCode, $playTimes, $to, $displayNum = null, $respUrl = null, $lang = 'zh', $userData = null, $welcomePrompt = null, $playVerifyCode = null)
176
    {
177
        //主帐号鉴权信息验证,对必选参数进行判空。
178
        $auth = $this->accAuth();
179
        if ($auth !== true) {
180
            return $auth;
181
        }
182
        // 拼接请求包体
183
        if ($this->BodyType === 'json') {
184
            $body = "{'appId':'$this->AppId','verifyCode':'$verifyCode','playTimes':'$playTimes','to':'$to','respUrl':'$respUrl','displayNum':'$displayNum',
185
           'lang':'$lang','userData':'$userData','welcomePrompt':'$welcomePrompt','playVerifyCode':'$playVerifyCode'}";
186
        } else {
187
            $body = "<VoiceVerify>
188
                    <appId>$this->AppId</appId>
189
                    <verifyCode>$verifyCode</verifyCode>
190
                    <playTimes>$playTimes</playTimes>
191
                    <to>$to</to>
192
                    <respUrl>$respUrl</respUrl>
193
                    <displayNum>$displayNum</displayNum>
194
                    <lang>$lang</lang>
195
                    <userData>$userData</userData>
196
					<welcomePrompt>$welcomePrompt</welcomePrompt>
197
					<playVerifyCode>$playVerifyCode</playVerifyCode>
198
                  </VoiceVerify>";
199
        }
200
        // 大写的sig参数
201
        $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
202
        // 生成请求URL
203
        $url = "https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/Calls/VoiceVerify?sig=$sig";
204
        // 生成授权:主帐户Id + 英文冒号 + 时间戳。
205
        $authen = base64_encode($this->AccountSid . ':' . $this->Batch);
206
        // 生成包头
207
        $header = array("Accept:application/$this->BodyType", "Content-Type:application/$this->BodyType;charset=utf-8", "Authorization:$authen");
208
        // 发送请求
209
        $result = $this->curl_post($url, $body, $header);
210 View Code Duplication
        if ($this->BodyType === 'json') {//JSON格式
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...
211
            $datas = json_decode($result);
212
        } else { //xml格式
213
            $datas = simplexml_load_string(trim($result, " \t\n\r"));
214
        }
215
216
        return $datas;
217
    }
218
219
   /**
220
    * 主帐号鉴权
221
    *
222
    * @return mixed
223
    */
224
   public function accAuth()
225
   {
226
       if ($this->ServerIP === '') {
227
           $data = new stdClass();
228
           $data->statusCode = '172004';
229
           $data->statusMsg = 'IP为空';
230
231
           return $data;
232
       }
233
       if ($this->ServerPort <= 0) {
234
           $data = new stdClass();
235
           $data->statusCode = '172005';
236
           $data->statusMsg = '端口错误(小于等于0)';
237
238
           return $data;
239
       }
240
       if ($this->SoftVersion === '') {
241
           $data = new stdClass();
242
           $data->statusCode = '172013';
243
           $data->statusMsg = '版本号为空';
244
245
           return $data;
246
       }
247
       if ($this->AccountSid === '') {
248
           $data = new stdClass();
249
           $data->statusCode = '172006';
250
           $data->statusMsg = '主帐号为空';
251
252
           return $data;
253
       }
254
       if ($this->AccountToken === '') {
255
           $data = new stdClass();
256
           $data->statusCode = '172007';
257
           $data->statusMsg = '主帐号令牌为空';
258
259
           return $data;
260
       }
261
       if ($this->AppId === '') {
262
           $data = new stdClass();
263
           $data->statusCode = '172012';
264
           $data->statusMsg = '应用ID为空';
265
266
           return $data;
267
       }
268
269
       return true;
270
   }
271
}
272