Completed
Pull Request — master (#80)
by
unknown
04:13
created

QcloudAgent   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A sendContentSms() 0 6 1
A sendTemplateSms() 0 7 1
A createContentParams() 0 16 1
A request() 0 12 3
A sendSms() 0 4 1
A createTemplateParams() 0 17 1
A voiceVerify() 0 3 1
A QcloudCurl() 0 20 2
A genSign() 0 4 1
A setResult() 0 12 2
A genResponseName() 0 3 1
A getTempDataString() 0 8 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
61
        return $params;
62
    }
63
64
    protected function createTemplateParams(array $params)
65
    {
66
        $tel = new \stdClass();
67
        $tel->nationcode = '86';
68
        $tel->phone = $params['to'];
69
        $jsondata = new \stdClass();
70
        $jsondata->tel = $tel;
71
        $jsondata->type = '0';
72
        $jsondata->tpl_id = $params['tempId'];
73
        $jsondata->params = [$params['tempdata']['code'], (string) $params['tempdata']['minutes']];
74
        $jsondata->sig = md5($this->appkey . $params['to']);
75
        $jsondata->extend = '';     // 根据需要添加,一般保持默认
76
        $jsondata->ext = '';        // 根据需要添加,一般保持默认
77
        $params = json_encode($jsondata);
78
79
        return $params;
80
    }
81
82
    public function voiceVerify($to, $code, $tempId, array $data)
83
    {
84
    }
85
86
    protected function QcloudCurl($url, $optData)
87
    {
88
        $ch = curl_init();
89
        curl_setopt($ch, CURLOPT_URL, $url);
90
        curl_setopt($ch, CURLOPT_HEADER, 0);
91
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
92
        curl_setopt($ch, CURLOPT_POST, 1);
93
        curl_setopt($ch, CURLOPT_POSTFIELDS, $optData);
94
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
95
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
96
        $response = curl_exec($ch);
97
        if ($response === false) {
98
            $request = false;
99
            $response = curl_getinfo($ch);
100
        }
101
        curl_close($ch);
102
        $response = json_decode($response, true);
103
104
        return compact('request', 'response');
105
    }
106
107
    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...
108
    {
109
        //
110
    }
111
112
    protected function setResult($result)
113
    {
114
        if ($result['response']['result'] === '0') {
115
            $this->result(Agent::SUCCESS, true);
116
            $this->result(Agent::INFO, $result['response']['ext']);
117
            $this->result(Agent::CODE, 1111);
118
        } else {
119
            $this->result(Agent::SUCCESS, false);
120
            $this->result(Agent::INFO, $result['response']['errmsg']);
121
            $this->result(Agent::CODE, 0000);
122
        }
123
    }
124
125
    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...
126
    {
127
    }
128
129
    protected function getTempDataString(array $data)
130
    {
131
        $data = array_map(function ($value) {
132
            return (string) $value;
133
        }, $data);
134
135
        return json_encode($data);
136
    }
137
}
138