Completed
Push — master ( daa55e...283203 )
by lan tian
9s
created

SendCloudAgent::sendContentSms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
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 implements TemplateSms
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 View Code Duplication
    protected function setResult($result)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        if ($result['request']) {
71
            $this->result(Agent::INFO, $result['response']);
72
            $result = json_decode($result['response'], true);
73
            if (isset($result['result'])) {
74
                $this->result(Agent::SUCCESS, (bool) $result['result']);
75
                $this->result(Agent::CODE, $result['statusCode']);
76
            }
77
        } else {
78
            $this->result(Agent::INFO, 'request failed');
79
        }
80
    }
81
82
    protected function getTempDataString(array $data)
83
    {
84
        return json_encode(array_map('strval', $data));
85
    }
86
}
87