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

YunTongXunAgent   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 14
Bugs 3 Features 2
Metric Value
wmc 13
c 14
b 3
f 2
lcom 1
cbo 2
dl 0
loc 57
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A sendSms() 0 4 1
A sendTemplateSms() 0 6 1
A voiceVerify() 0 8 4
A rest() 0 8 1
B setResult() 0 20 5
A sendContentSms() 0 3 1
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