Completed
Pull Request — master (#107)
by
unknown
02:32
created

QcloudAgent::sendTemplateSms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 3
1
<?php
2
3
namespace Toplan\PhpSms;
4
/**
5
 * Class SendCloudAgent
6
 *
7
 * @property string $smsUser
8
 * @property string $smsKey
9
 */
10
class QcloudAgent extends Agent implements TemplateSms
11
{
12
    protected $sendUrl = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms';
13
    protected $random;
14
    protected $content;
15
    public function sendSms($to, $content, $tempId, array $data)
16
    {
17
        $this->content = $content;
18
        $this->sendTemplateSms($to, $tempId, $data);
19
    }
20
21
    public function sendTemplateSms($to, $tempId, array $data)
22
    {
23
        $params = [
24
            'type'    => 0,//0:普通短信;1:营销短信
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25
            'msg'    => $this->content,//$this->getTempDataString($data)
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
            'tel'   => ["nationcode"=> "86","mobile"=>$to],
27
            'time' => time(),
28
            'extend' => "",
29
            'ext' => "",
30
        ];
31
        $this->random = $this->getRandom();
32
        $sendUrl =  $this->sendUrl.'?'.'sdkappid='.$this->appId.'&random='.$this->random;
0 ignored issues
show
Documentation introduced by
The property appId does not exist on object<Toplan\PhpSms\QcloudAgent>. 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...
33
        $this->request($sendUrl, $params);
34
    }
35
36
    public function voiceVerify($to, $code, $tempId, array $data)
37
    {
38
        $params = [
39
            'phone' => $to,
40
            'code'  => $code,
41
        ];
42
        $this->request('https://yun.tim.qq.com/v5/tlssmssvr/sendVoice', $params);
43
    }
44
45
    protected function request($sendUrl, array $params)
46
    {
47
        $params = $this->createParams($params);
48
        $result = $this->curl($sendUrl, json_encode($params), true);
0 ignored issues
show
Documentation introduced by
json_encode($params) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
        $this->setResult($result);
50
    }
51
52
    protected function createParams(array $params)
53
    {
54
        $params['sig'] = $this->genSign($params);
55
56
        return $params;
57
    }
58
59
    protected function genSign($params)
60
    {
61
        $phone = $params['tel']["mobile"];
62
        $signature = "appkey=".$this->appKey."&random=".$this->random."&time=".$params['time']."&mobile=".$phone;
0 ignored issues
show
Documentation introduced by
The property appKey does not exist on object<Toplan\PhpSms\QcloudAgent>. 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...
63
        return hash("sha256",$signature, FALSE);
64
    }
65
66 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...
67
    {
68
        if ($result['request']) {
69
            $this->result(Agent::INFO, $result['response']);
70
            $result = json_decode($result['response'], true);
71
            if (isset($result['result'])) {
72
                $this->result(Agent::SUCCESS, (bool) ($result['result'] == 0));
73
                $this->result(Agent::CODE, $result['result']);
74
            }
75
        } else {
76
            $this->result(Agent::INFO, 'request failed');
77
        }
78
    }
79
80
    protected function getTempDataString(array $data)
81
    {
82
        $data = array_map(function ($value) {
83
            return (string) $value;
84
        }, $data);
85
86
        return json_encode($data);
87
    }
88
89
    protected function getRandom() {
90
        return rand(100000, 999999);
91
    }
92
}
93