Completed
Pull Request — master (#86)
by
unknown
05:01
created

SmsbaoAgent::sendSms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class SmsbaoAgent
7
 *
8
 * @package Toplan\PhpSms
9
 */
10
class SmsbaoAgent extends Agent
11
{
12
    protected $resultArr = [
13
        "0" => "短信发送成功",
14
        "-1" => "参数不全",
15
        "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
16
        "30" => "密码错误",
17
        "40" => "账号不存在",
18
        "41" => "余额不足",
19
        "42" => "帐户已过期",
20
        "43" => "IP地址限制",
21
        "50" => "内容含有敏感词"
22
    ];
23
24
    public function sendSms($to, $content, $tempId, array $data)
25
    {
26
        $this->sendContentSms($to, $content);
27
    }
28
29
    /**
30
     * Content SMS send process.
31
     *
32
     * @param $to
33
     * @param $content
34
     */
35 View Code Duplication
    public function sendContentSms($to, $content)
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...
36
    {
37
        $url = 'http://api.smsbao.com/sms';
38
        $username = $this->smsUser;
0 ignored issues
show
Bug introduced by
The property smsUser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
        $password = md5($this->smsPassword);
0 ignored issues
show
Documentation introduced by
The property smsPassword does not exist on object<Toplan\PhpSms\SmsbaoAgent>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
40
        $content = urlencode($content);
41
        $postString = "u=$username&p=$password&m=$to&c=$content";
42
        $response = $this->sockPost($url, $postString);
43
        $this->setResult($response);
44
    }
45
46
    /**
47
     * Template SMS send process.
48
     *
49
     * @param       $to
50
     * @param       $tempId
51
     * @param array $tempData
52
     */
53
    public function sendTemplateSms($to, $tempId, array $tempData)
54
    {
55
    }
56
57
    /**
58
     * Voice verify send process.
59
     *
60
     * @param       $to
61
     * @param       $code
62
     * @param       $tempId
63
     * @param array $tempData
64
     */
65 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...
66
    {
67
        $url = 'http://api.smsbao.com/voice';
68
        $username = $this->smsUser;
69
        $password = md5($this->smsPassword);
0 ignored issues
show
Documentation introduced by
The property smsPassword does not exist on object<Toplan\PhpSms\SmsbaoAgent>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
        $postString = "u=$username&p=$password&m=$to&c=$code";
71
        $response = $this->sockPost($url, $postString);
72
        $response = is_array($response) ? $response['response'] : $response;
73
        $this->setResult($response);
74
    }
75
76
    protected function setResult($result)
77
    {
78
        $msg = key_exists($result, $this->resultArr) ? $this->resultArr[$result] : "未知错误";
79
        $this->result(Agent::INFO, json_encode(['code' => $result, 'msg' => $msg]));
80
        $this->result(Agent::SUCCESS, $result === '0');
81
        $this->result(Agent::CODE, $result);
82
    }
83
}