AlidayuAgent   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 129
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A sendTemplateSms() 0 12 1
A sendTemplateVoice() 0 11 1
A sendVoiceCode() 0 10 1
A request() 0 8 1
A createParams() 0 13 1
A genSign() 0 14 4
A setResult() 0 18 4
A genResponseName() 0 4 1
A getTempDataString() 0 8 1
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, [], [
76
            CURLOPT_POSTFIELDS => http_build_query($params),
77
        ]);
78
        $this->setResult($result, $this->genResponseName($params['method']));
79
    }
80
81
    protected function createParams(array $params)
82
    {
83
        $params = array_merge([
84
            'app_key'            => $this->appKey,
85
            'v'                  => '2.0',
86
            'format'             => 'json',
87
            'sign_method'        => 'md5',
88
            'timestamp'          => date('Y-m-d H:i:s'),
89
        ], $params);
90
        $params['sign'] = $this->genSign($params);
91
92
        return $this->params($params);
93
    }
94
95
    protected function genSign($params)
96
    {
97
        ksort($params);
98
        $stringToBeSigned = $this->secretKey;
99
        foreach ($params as $k => $v) {
100
            if (is_string($v) && '@' !== substr($v, 0, 1)) {
101
                $stringToBeSigned .= "$k$v";
102
            }
103
        }
104
        unset($k, $v);
105
        $stringToBeSigned .= $this->secretKey;
106
107
        return strtoupper(md5($stringToBeSigned));
108
    }
109
110
    protected function setResult($result, $callbackName)
111
    {
112
        if ($result['request']) {
113
            $result = json_decode($result['response'], true);
114
            if (isset($result[$callbackName]['result'])) {
115
                $result = $result[$callbackName]['result'];
116
                $this->result(Agent::SUCCESS, (bool) $result['success']);
117
                $this->result(Agent::INFO, json_encode($result));
118
                $this->result(Agent::CODE, $result['err_code']);
119
            } elseif (isset($result['error_response'])) {
120
                $error = $result['error_response'];
121
                $this->result(Agent::INFO, json_encode($error));
122
                $this->result(Agent::CODE, $error['code']);
123
            }
124
        } else {
125
            $this->result(Agent::INFO, 'request failed');
126
        }
127
    }
128
129
    protected function genResponseName($method)
130
    {
131
        return str_replace('.', '_', $method) . '_response';
132
    }
133
134
    protected function getTempDataString(array $data)
135
    {
136
        $data = array_map(function ($value) {
137
            return (string) $value;
138
        }, $data);
139
140
        return json_encode($data);
141
    }
142
}
143