Completed
Pull Request — master (#106)
by
unknown
02:38 queued 39s
created

AliyunSmsAgent   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 102
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A sendSms() 0 4 1
A sendTemplateSms() 0 11 1
A voiceVerify() 0 3 1
A request() 0 7 2
A setResult() 0 17 3
A getTempDataString() 0 8 1
A sendContentSms() 0 3 1
A createParams() 0 16 1
A computeSignature() 0 12 2
A percentEncode() 0 9 1
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class AliyunSmsAgent
7
 *
8
 * @property string $accessKeyId
9
 * @property string $accessKeySecret
10
 * @property string $signName
11
 */
12
class AliyunSmsAgent extends Agent
13
{
14
    public function sendSms($to, $content, $tempId, array $data)
15
    {
16
        $this->sendTemplateSms($to, $tempId, $data);
17
    }
18
19
    public function sendTemplateSms($to, $tempId, array $data)
20
    {
21
        $params = [
22
            'Action'            => 'SingleSendSms',
23
            'SignName'          => $this->signName,
24
            'ParamString'       => $this->getTempDataString($data),
25
            'RecNum'            => $to,
26
            'TemplateCode'      => $tempId,
27
        ];
28
        $this->request($params);
29
    }
30
31
    public function voiceVerify($to, $code, $tempId, array $data)
32
    {
33
    }
34
35
    protected function request(array $params)
36
    {
37
        $sendUrl = $this->sendUrl ?: 'https://sms.aliyuncs.com';
0 ignored issues
show
Documentation introduced by
The property sendUrl does not exist on object<Toplan\PhpSms\AliyunSmsAgent>. 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...
38
        $params = $this->createParams($params);
39
        $result = $this->curl($sendUrl, $params, true);
40
        $this->setResult($result);
41
    }
42
43
    protected function createParams(array $params)
44
    {
45
        date_default_timezone_set('GMT');
46
        $params = array_merge([
47
            'Format'             => 'JSON',
48
            'Version'            => '2016-09-27',
49
            'AccessKeyId'        => $this->accessKeyId,
50
            'SignatureMethod'    => 'HMAC-SHA1',
51
            'Timestamp'          => date('Y-m-d\TH:i:s\Z'),
52
            'SignatureVersion'   => '1.0',
53
            'SignatureNonce'     => uniqid(),
54
        ], $params);
55
        $params['Signature'] = $this->computeSignature($params);
56
57
        return $params;
58
    }
59
60
    private function computeSignature($parameters)
61
    {
62
        ksort($parameters);
63
        $canonicalizedQueryString = '';
64
        foreach ($parameters as $key => $value) {
65
            $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
66
        }
67
        $stringToSign = 'POST&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
68
        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret . '&', true));
69
70
        return $signature;
71
    }
72
73
    protected function percentEncode($str)
74
    {
75
        $res = urlencode($str);
76
        $res = preg_replace('/\+/', '%20', $res);
77
        $res = preg_replace('/\*/', '%2A', $res);
78
        $res = preg_replace('/%7E/', '~', $res);
79
80
        return $res;
81
    }
82
83
    protected function setResult($result)
84
    {
85
        if ($result['request']) {
86
            $result = json_decode($result['response'], true);
87
            //dump($result);
88
            if (isset($result['Message'])) {
89
                $this->result(Agent::INFO, json_encode($result));
90
                $this->result(Agent::CODE, $result['Code']);
91
            } else {
92
                $this->result(Agent::SUCCESS, true);
93
                $this->result(Agent::INFO, json_encode($result));
94
                $this->result(Agent::CODE, 0);
95
            }
96
        } else {
97
            $this->result(Agent::INFO, '请求失败');
98
        }
99
    }
100
101
    protected function getTempDataString(array $data)
102
    {
103
        $data = array_map(function ($value) {
104
            return (string) $value;
105
        }, $data);
106
107
        return json_encode($data);
108
    }
109
110
    public function sendContentSms($to, $content)
0 ignored issues
show
Unused Code introduced by
The parameter $to is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
    }
113
}
114