Completed
Push — master ( f1f896...6080f5 )
by lan tian
10s
created

SendCloudAgent::sendTemplateSms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
nc 1
cc 1
eloc 7
nop 3
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class SendCloudAgent
7
 *
8
 * @property string $smsUser
9
 * @property string $smsKey
10
 */
11
class SendCloudAgent extends Agent
12
{
13
    public function sendSms($to, $content, $tempId, array $data)
14
    {
15
        $this->sendTemplateSms($to, $tempId, $data);
16
    }
17
18
    public function sendTemplateSms($to, $tempId, array $data)
19
    {
20
        $params = [
21
            'msgType'    => 0,
22
            'vars'       => $this->getTempDataString($data),
23
            'phone'      => $to,
24
            'templateId' => $tempId,
25
        ];
26
        $this->request('http://sendcloud.sohu.com/smsapi/send', $params);
27
    }
28
29
    public function voiceVerify($to, $code, $tempId, array $data)
30
    {
31
        $params = [
32
            'phone' => $to,
33
            'code'  => $code,
34
        ];
35
        $this->request('http://sendcloud.sohu.com/smsapi/sendVoice', $params);
36
    }
37
38
    protected function request($sendUrl, array $params)
39
    {
40
        $params = $this->createParams($params);
41
        $result = $this->curl($sendUrl, $params, true);
42
        $this->setResult($result);
43
    }
44
45
    protected function createParams(array $params)
46
    {
47
        $params = array_merge([
48
            'smsUser' => $this->smsUser,
49
            ], $params);
50
        $params['signature'] = $this->genSign($params);
51
52
        return $params;
53
    }
54
55
    protected function genSign($params)
56
    {
57
        ksort($params);
58
        $stringToBeSigned = '';
59
        foreach ($params as $k => $v) {
60
            $stringToBeSigned .= $k . '=' . $v . '&';
61
        }
62
        $stringToBeSigned = trim($stringToBeSigned, '&');
63
        $stringToBeSigned = $this->smsKey . '&' . $stringToBeSigned . '&' . $this->smsKey;
64
65
        return md5($stringToBeSigned);
66
    }
67
68
    protected function setResult($result)
69
    {
70
        if ($result) {
71
            $response = json_decode($result['response'], true);
72
            if (isset($response['result'])) {
73
                $this->result(Agent::SUCCESS, (bool) $response['result']);
74
                $this->result(Agent::INFO, $response['message']);
75
                $this->result(Agent::CODE, $response['statusCode']);
76
            }
77
        } else {
78
            $this->result(Agent::INFO, '请求失败');
79
        }
80
    }
81
82
    protected function getTempDataString(array $data)
83
    {
84
        return json_encode(array_map('strval', $data));
85
    }
86
87
    public function sendContentSms($to, $content)
88
    {
89
    }
90
}
91