Completed
Push — master ( 13cabb...9cf391 )
by lan tian
6s
created

YunTongXunAgent::rest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
use REST;
6
7
/**
8
 * Class YunTongXunAgent
9
 *
10
 * @property string $serverIP
11
 * @property string $serverPort
12
 * @property string $softVersion
13
 * @property string $accountSid
14
 * @property string $accountToken
15
 * @property string $appId
16
 * @property int    $playTimes
17
 * @property string $voiceLang
18
 * @property string $displayNum
19
 */
20
class YunTongXunAgent extends Agent
21
{
22
    public function sendSms($to, $content, $tempId, array $data)
23
    {
24
        $this->sendTemplateSms($to, $tempId, $data);
25
    }
26
27
    public function sendTemplateSms($to, $tempId, array $data)
28
    {
29
        $data = array_values($data);
30
        $result = $this->rest()->sendTemplateSMS($to, $data, $tempId);
31
        $this->setResult($result);
32
    }
33
34
    public function voiceVerify($to, $code, $tempId, array $data)
35
    {
36
        $playTimes = intval($this->playTimes ?: 3);
37
        $displayNum = $this->displayNum ?: null;
38
        $lang = $this->voiceLang ?: 'zh';
39
        $result = $this->rest()->voiceVerify($code, $playTimes, $to, $displayNum, null, $lang);
40
        $this->setResult($result);
41
    }
42
43
    protected function rest()
44
    {
45
        $rest = new REST($this->serverIP, $this->serverPort, $this->softVersion, 'json');
46
        $rest->setAccount($this->accountSid, $this->accountToken);
47
        $rest->setAppId($this->appId);
48
49
        return $rest;
50
    }
51
52
    protected function setResult($result)
53
    {
54
        if (!$result) {
55
            return;
56
        }
57
        $code = $info = (string) $result->statusCode;
58
        $success = $code === '000000';
59
        if (!$success) {
60
            $info = (string) $result->statusMsg;
61
        } else {
62
            if (isset($result->TemplateSMS)) {
63
                $info = 'smsSid:' . $result->TemplateSMS->smsMessageSid;
64
            } elseif (isset($result->VoiceVerify)) {
65
                $info = 'callSid:' . $result->VoiceVerify->callSid;
66
            }
67
        }
68
        $this->result(Agent::SUCCESS, $success);
69
        $this->result(Agent::CODE, $code);
70
        $this->result(Agent::INFO, $info);
71
    }
72
73
    public function sendContentSms($to, $content)
74
    {
75
    }
76
}
77