Completed
Push — master ( 283203...274200 )
by lan tian
11s
created

AlidayuAgent::sendVoiceCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class AlidayuAgent
7
 *
8
 * @property string $sendUrl
9
 * @property string $appKey
10
 * @property string $secretKey
11
 * @property string $smsFreeSignName
12
 * @property string $calledShowNum
13
 */
14
class AlidayuAgent extends Agent implements TemplateSms, VoiceCode, TemplateVoice
15
{
16
    /**
17
     * Template SMS send process.
18
     *
19
     * @param string|array $to
20
     * @param int|string   $tempId
21
     * @param array        $tempData
22
     */
23
    public function sendTemplateSms($to, $tempId, array $tempData)
24
    {
25
        $params = [
26
            'method'             => 'alibaba.aliqin.fc.sms.num.send',
27
            'sms_type'           => 'normal',
28
            'sms_free_sign_name' => $this->smsFreeSignName,
29
            'sms_param'          => $this->getTempDataString($tempData),
30
            'rec_num'            => $to,
31
            'sms_template_code'  => $tempId,
32
        ];
33
        $this->request($params);
34
    }
35
36
    /**
37
     * Template voice send process.
38
     *
39
     * @param string|array $to
40
     * @param int|string   $tempId
41
     * @param array        $tempData
42
     */
43
    public function sendTemplateVoice($to, $tempId, array $tempData)
44
    {
45
        $params = [
46
            'called_num'        => $to,
47
            'called_show_num'   => $this->calledShowNum,
48
            'method'            => 'alibaba.aliqin.fc.tts.num.singlecall',
49
            'tts_code'          => $tempId,
50
            'tts_param'         => $this->getTempDataString($tempData),
51
        ];
52
        $this->request($params);
53
    }
54
55
    /**
56
     * Voice code send process.
57
     *
58
     * @param string|array $to
59
     * @param int|string   $code
60
     */
61
    public function sendVoiceCode($to, $code)
62
    {
63
        $params = [
64
            'called_num'        => $to,
65
            'called_show_num'   => $this->calledShowNum,
66
            'method'            => 'alibaba.aliqin.fc.voice.num.singlecall',
67
            'voice_code'        => $code,
68
        ];
69
        $this->request($params);
70
    }
71
72
    protected function request(array $params)
73
    {
74
        $params = $this->createParams($params);
75
        $result = $this->curlPost($this->sendUrl, $params);
76
        $this->setResult($result, $this->genResponseName($params['method']));
77
    }
78
79
    protected function createParams(array $params)
80
    {
81
        return array_merge([
82
            'app_key'            => $this->appKey,
83
            'v'                  => '2.0',
84
            'format'             => 'json',
85
            'sign_method'        => 'md5',
86
            'timestamp'          => date('Y-m-d H:i:s'),
87
        ], $params, [
88
            'sign'               => $this->genSign($params),
89
        ]);
90
    }
91
92
    protected function genSign($params)
93
    {
94
        ksort($params);
95
        $stringToBeSigned = $this->secretKey;
96
        foreach ($params as $k => $v) {
97
            if (is_string($v) && '@' !== substr($v, 0, 1)) {
98
                $stringToBeSigned .= "$k$v";
99
            }
100
        }
101
        unset($k, $v);
102
        $stringToBeSigned .= $this->secretKey;
103
104
        return strtoupper(md5($stringToBeSigned));
105
    }
106
107
    protected function setResult($result, $callbackName)
108
    {
109
        if ($result['request']) {
110
            $result = json_decode($result['response'], true);
111
            if (isset($result[$callbackName]['result'])) {
112
                $result = $result[$callbackName]['result'];
113
                $this->result(Agent::SUCCESS, (bool) $result['success']);
114
                $this->result(Agent::INFO, json_encode($result));
115
                $this->result(Agent::CODE, $result['err_code']);
116
            } elseif (isset($result['error_response'])) {
117
                $error = $result['error_response'];
118
                $this->result(Agent::INFO, json_encode($error));
119
                $this->result(Agent::CODE, $error['code']);
120
            }
121
        } else {
122
            $this->result(Agent::INFO, 'request failed');
123
        }
124
    }
125
126
    protected function genResponseName($method)
127
    {
128
        return str_replace('.', '_', $method) . '_response';
129
    }
130
131
    protected function getTempDataString(array $data)
132
    {
133
        $data = array_map(function ($value) {
134
            return (string) $value;
135
        }, $data);
136
137
        return json_encode($data);
138
    }
139
}
140