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

QcloudAgent::request()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
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
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
	
62
    protected function createTemplateParams(array $params)
63
    {
64
        $tel = new \stdClass();
65
        $tel->nationcode = '86';
66
        $tel->phone = $params['to'];
67
        $jsondata = new \stdClass();
68
        $jsondata->tel = $tel;
69
        $jsondata->type = '0';
70
        $jsondata->tpl_id = $params['tempId'];
71
        $jsondata->params = [$params['tempdata']['code'], (string) $params['tempdata']['minutes']];
72
        $jsondata->sig = md5($this->appkey . $params['to']);
73
        $jsondata->extend = '';     // 根据需要添加,一般保持默认
74
        $jsondata->ext = '';        // 根据需要添加,一般保持默认
75
        $params = json_encode($jsondata);
76
        return $params;
77
    }
78
79
    public function voiceVerify($to, $code, $tempId, array $data)
80
    {
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
    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...
105
    {
106
        //
107
    }
108
    protected function setResult($result)
109
    {
110
        if ($result['response']['result'] === '0') {
111
            $this->result(Agent::SUCCESS, true);
112
            $this->result(Agent::INFO, $result['response']['ext']);
113
            $this->result(Agent::CODE, 1111);
114
        } else {
115
            $this->result(Agent::SUCCESS, false);
116
            $this->result(Agent::INFO, $result['response']['errmsg']);
117
            $this->result(Agent::CODE, 0000);
118
        }
119
    }
120
    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...
121
    {
122
    }
123
124
    protected function getTempDataString(array $data)
125
    {
126
        $data = array_map(function ($value) {
127
            return (string) $value;
128
        }, $data);
129
130
        return json_encode($data);
131
    }
132
}
133