Completed
Pull Request — master (#88)
by lan tian
03:55
created

SmsBaoAgent   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 12.16 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 9
loc 74
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sendSms() 0 4 1
A sendContentSms() 0 10 1
A sendTemplateSms() 0 3 1
A voiceVerify() 9 9 1
A setResult() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class SmsBaoAgent
7
 *
8
 * @property string $smsUser
9
 * @property string $smsPassword
10
 */
11
class SmsBaoAgent extends Agent
12
{
13
    protected $resultArr = [
14
        '0'  => '发送成功',
15
        '-1' => '参数不全',
16
        '-2' => '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!',
17
        '30' => '密码错误',
18
        '40' => '账号不存在',
19
        '41' => '余额不足',
20
        '42' => '帐户已过期',
21
        '43' => 'IP地址限制',
22
        '50' => '内容含有敏感词',
23
        '51' => '手机号码不正确',
24
    ];
25
26
    public function sendSms($to, $content, $tempId, array $data)
27
    {
28
        $this->sendContentSms($to, $content);
29
    }
30
31
    /**
32
     * Content SMS send process.
33
     *
34
     * @param $to
35
     * @param $content
36
     */
37
    public function sendContentSms($to, $content)
38
    {
39
        $url = 'http://api.smsbao.com/sms';
40
        $username = $this->smsUser;
41
        $password = md5($this->smsPassword);
42
        $content = urlencode($content);
43
        $postString = "u=$username&p=$password&m=$to&c=$content";
44
        $response = $this->sockPost($url, $postString);
45
        $this->setResult($response);
46
    }
47
48
    /**
49
     * Template SMS send process.
50
     *
51
     * @param       $to
52
     * @param       $tempId
53
     * @param array $tempData
54
     */
55
    public function sendTemplateSms($to, $tempId, array $tempData)
56
    {
57
    }
58
59
    /**
60
     * Voice verify send process.
61
     *
62
     * @param       $to
63
     * @param       $code
64
     * @param       $tempId
65
     * @param array $tempData
66
     */
67 View Code Duplication
    public function voiceVerify($to, $code, $tempId, array $tempData)
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...
68
    {
69
        $url = 'http://api.smsbao.com/voice';
70
        $username = $this->smsUser;
71
        $password = md5($this->smsPassword);
72
        $postString = "u=$username&p=$password&m=$to&c=$code";
73
        $response = $this->sockPost($url, $postString);
74
        $this->setResult($response);
75
    }
76
77
    protected function setResult($result)
78
    {
79
        $msg = array_key_exists($result, $this->resultArr) ? $this->resultArr[$result] : '未知错误';
80
        $this->result(Agent::INFO, json_encode(['code' => $result, 'msg' => $msg]));
81
        $this->result(Agent::SUCCESS, $result === '0');
82
        $this->result(Agent::CODE, $result);
83
    }
84
}
85