Completed
Pull Request — master (#80)
by
unknown
02:43
created

QcloudAgent::voiceVerify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 4
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class AlidayuAgent
7
 *
8
 * @property string $appid
9
 * @property string $appkey
10
 * @property string $sendUrl
11
 */
12
class QcloudAgent extends Agent
13
{
14
15
    public function sendSms($to, $content, $tempId, array $data)
16
    {
17
        $this->sendTemplateSms($to, $tempId, $data);
18
    }
19
20
21
    public function sendContentSms($to, $content)
22
    {
23
        $params['to']=$to;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = 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...
24
        $params['content']=$content;
25
        $this->request($params);
26
    }
27
    public function sendTemplateSms($to, $tempId, array $data)
28
    {
29
        $params['to'] = $to;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = 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...
30
        $params['tempId']=(int)$tempId;
31
        $params['tempdata']=$data;
32
        $this->request($params);
33
    }
34
    protected function request(array $params)
35
    {
36
        $randNum = rand(100000, 999999);
37
        $sendUrl = $this->sendUrl."?sdkappid=".$this->appid."&random=".$randNum;
38
        if (isset($params['content'])) {
39
            $params = $this->createContentParams($params);
40
        } elseif (isset($params['tempId'])) {
41
            $params = $this->createTemplateParams($params);
42
        }
43
        $result = $this->QcloudCurl($sendUrl, $params);
44
        $this->setResult($result);
45
    }
46
    protected function createContentParams(array $params)
47
    {
48
        $tel = new \stdClass();
49
        $tel->nationcode = '86';
50
        $tel->phone = $params['to'];
51
        $jsondata = new \stdClass();
52
        $jsondata->tel = $tel;
53
        $jsondata->type = "0";
54
        $jsondata->msg = $params['content'];
55
        $jsondata->sig = md5($this->appkey.$params['to']);
56
        $jsondata->extend = "";     // 根据需要添加,一般保持默认
57
        $jsondata->ext = "";        // 根据需要添加,一般保持默认
58
        $params =json_encode($jsondata);
59
        return $params;
60
    }
61
	protected function createTemplateParams(array $params)
62
    {
63
        $tel = new \stdClass();
64
        $tel->nationcode = '86';
65
        $tel->phone = $params['to'];
66
        $jsondata = new \stdClass();
67
        $jsondata->tel = $tel;
68
        $jsondata->type = '0';
69
        $jsondata->tpl_id=$params['tempId'];
70
        $jsondata->params=[$params['tempdata']['code'],(string)$params['tempdata']['minutes']];
71
        $jsondata->sig = md5($this->appkey.$params['to']);
72
        $jsondata->extend = "";     // 根据需要添加,一般保持默认
73
        $jsondata->ext = "";        // 根据需要添加,一般保持默认
74
        $params =json_encode($jsondata);
75
        return $params;
76
    }
77
78
    public function voiceVerify($to, $code, $tempId, array $data)
79
    {
80
81
    }
82
    protected function QcloudCurl($url, $optData)
83
    {
84
        $ch = curl_init();
85
        curl_setopt($ch, CURLOPT_URL, $url);
86
        curl_setopt($ch, CURLOPT_HEADER, 0);
87
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
88
        curl_setopt($ch, CURLOPT_POST, 1);
89
        curl_setopt($ch, CURLOPT_POSTFIELDS, $optData);
90
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
91
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
92
        $response = curl_exec($ch);
93
        if ($response === false) {
94
            $request = false;
95
            $response = curl_getinfo($ch);
96
        }
97
        curl_close($ch);
98
         $response=json_decode($response, true);
99
        return compact('request', 'response');
100
    }
101
    protected function genSign($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        //
104
    }
105
    protected function setResult($result)
106
    {
107
        if ($result['response']['result']=='0') {
108
            $this->result(Agent::SUCCESS, true);
109
            $this->result(Agent::INFO, $result['response']['ext']);
110
            $this->result(Agent::CODE, 1111);
111
        } else {
112
            $this->result(Agent::SUCCESS, false);
113
            $this->result(Agent::INFO, $result['response']['errmsg']);
114
            $this->result(Agent::CODE, 0000);
115
        }
116
    }
117
    protected function genResponseName($method)
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    {
119
    }
120
121
    protected function getTempDataString(array $data)
122
    {
123
        $data = array_map(function ($value) {
124
            return (string) $value;
125
        }, $data);
126
127
        return json_encode($data);
128
    }
129
130
131
}
132