Completed
Push — master ( daa55e...283203 )
by lan tian
9s
created

JuHeAgent::sendContentSms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class JuHeAgent
7
 *
8
 * @property string $key
9
 * @property string $times
10
 */
11
class JuHeAgent extends Agent implements TemplateSms
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
        $sendUrl = 'http://v.juhe.cn/sms/send';
21
        $tplValue = '';
22
        foreach ($data as $key => $value) {
23
            if (preg_match('/[#&=]/', $value)) {
24
                $value = urlencode($value);
25
            }
26
            $split = !$tplValue ? '' : '&';
27
            $tplValue .= "$split#$key#=$value";
28
        }
29
        $tplValue = !$tplValue ?: urlencode($tplValue);
30
        $smsConf = [
31
            'key'       => $this->key,
32
            'mobile'    => $to,
33
            'tpl_id'    => $tempId,
34
            'tpl_value' => $tplValue,
35
            'dtype'     => 'json',
36
        ];
37
        $result = $this->curl($sendUrl, $smsConf);
38
        $this->setResult($result);
39
    }
40
41
    public function voiceVerify($to, $code, $tempId, array $data)
42
    {
43
        $url = 'http://op.juhe.cn/yuntongxun/voice';
44
        $params = [
45
            'valicode'  => $code,
46
            'to'        => $to,
47
            'playtimes' => $this->times ?: 3,
48
            'key'       => $this->key,
49
            'dtype'     => 'json',
50
        ];
51
        $result = $this->curl($url, $params);
52
        $this->setResult($result);
53
    }
54
55 View Code Duplication
    protected function setResult($result)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        if ($result['request']) {
58
            $this->result(Agent::INFO, $result['response']);
59
            $result = json_decode($result['response'], true);
60
            $this->result(Agent::SUCCESS, $result['error_code'] === 0);
61
            $this->result(Agent::CODE, $result['error_code']);
62
        } else {
63
            $this->result(Agent::INFO, 'request failed');
64
        }
65
    }
66
}
67