Completed
Pull Request — master (#80)
by
unknown
02:49 queued 18s
created

QcloudAgent::genSign()   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 1
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
    public function sendSms($to, $content, $tempId, array $data)
15
    {
16
        $this->sendTemplateSms($to, $tempId, $data);
17
    }
18
19
    public function sendContentSms($to, $content)
20
    {
21
        $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...
22
        $params['content'] = $content;
23
        $this->request($params);
24
    }
25
	
26
    public function sendTemplateSms($to, $tempId, array $data)
27
    {
28
        $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...
29
        $params['tempId'] = (int) $tempId;
30
        $params['tempdata'] = $data;
31
        $this->request($params);
32
    }
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
	
47
    protected function createContentParams(array $params)
48
    {
49
        $tel = new \stdClass();
50
        $tel->nationcode = '86';
51
        $tel->phone = $params['to'];
52
        $jsondata = new \stdClass();
53
        $jsondata->tel = $tel;
54
        $jsondata->type = '0';
55
        $jsondata->msg = $params['content'];
56
        $jsondata->sig = md5($this->appkey . $params['to']);
57
        $jsondata->extend = '';     // 根据需要添加,一般保持默认
58
        $jsondata->ext = '';        // 根据需要添加,一般保持默认
59
        $params = json_encode($jsondata);
60
        return $params;
61
    }
62
	
63
    protected function createTemplateParams(array $params)
64
    {
65
        $tel = new \stdClass();
66
        $tel->nationcode = '86';
67
        $tel->phone = $params['to'];
68
        $jsondata = new \stdClass();
69
        $jsondata->tel = $tel;
70
        $jsondata->type = '0';
71
        $jsondata->tpl_id = $params['tempId'];
72
        $jsondata->params = [$params['tempdata']['code'], (string) $params['tempdata']['minutes']];
73
        $jsondata->sig = md5($this->appkey . $params['to']);
74
        $jsondata->extend = '';     // 根据需要添加,一般保持默认
75
        $jsondata->ext = '';        // 根据需要添加,一般保持默认
76
        $params = json_encode($jsondata);
77
        return $params;
78
    }
79
80
    public function voiceVerify($to, $code, $tempId, array $data)
81
    {
82
    }
83
84
    protected function QcloudCurl($url, $optData)
85
    {
86
        $ch = curl_init();
87
        curl_setopt($ch, CURLOPT_URL, $url);
88
        curl_setopt($ch, CURLOPT_HEADER, 0);
89
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
90
        curl_setopt($ch, CURLOPT_POST, 1);
91
        curl_setopt($ch, CURLOPT_POSTFIELDS, $optData);
92
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
93
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
94
        $response = curl_exec($ch);
95
        if ($response === false) {
96
            $request = false;
97
            $response = curl_getinfo($ch);
98
        }
99
        curl_close($ch);
100
        $response = json_decode($response, true);
101
	    
102
        return compact('request', 'response');
103
    }
104
105
    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...
106
    {
107
        //
108
    }
109
110
    protected function setResult($result)
111
    {
112
        if ($result['response']['result'] === '0') {
113
            $this->result(Agent::SUCCESS, true);
114
            $this->result(Agent::INFO, $result['response']['ext']);
115
            $this->result(Agent::CODE, 1111);
116
        } else {
117
            $this->result(Agent::SUCCESS, false);
118
            $this->result(Agent::INFO, $result['response']['errmsg']);
119
            $this->result(Agent::CODE, 0000);
120
        }
121
    }
122
123
    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...
124
    {
125
    }
126
127
    protected function getTempDataString(array $data)
128
    {
129
        $data = array_map(function ($value) {
130
            return (string) $value;
131
        }, $data);
132
133
        return json_encode($data);
134
    }
135
}
136