Completed
Pull Request — master (#73)
by
unknown
46:11 queued 21:11
created

SendCloudAgent::createParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
nc 1
cc 1
eloc 6
nop 1
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
        var_dump($result);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($result); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
71
        if ($result) {
72
            if (isset($result['result'])) {
73
                $this->result(Agent::SUCCESS, (bool) $result['result']);
74
                $this->result(Agent::INFO, $error['message']);
0 ignored issues
show
Bug introduced by
The variable $error does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
75
                $this->result(Agent::CODE, $result['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