Completed
Pull Request — master (#85)
by
unknown
04:25
created

AliyuncsAgent::getTempDataString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
use Carbon\Carbon;
6
use Toplan\PhpSms\Agent;
7
8
/**
9
 * Class ChangZhuoAgent
10
 *
11
 * @property string $account
12
 */
13
class AliyuncsAgent extends Agent
14
{
15
    public function sendSms($to, $content, $tempData, array $data)
16
    {
17
        $this->sendTemplateSms($to, $tempData, $data);
18
    }
19
20
    public function sendTemplateSms($to, $tempData, array $data)
21
    {
22
        $params = [
23
            // 公共参数
24
            'Format' => $this->format,
0 ignored issues
show
Documentation introduced by
The property format does not exist on object<Toplan\PhpSms\AliyuncsAgent>. 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...
25
            'Version' => $this->version,
0 ignored issues
show
Documentation introduced by
The property version does not exist on object<Toplan\PhpSms\AliyuncsAgent>. 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...
26
            'AccessKeyId' => $this->accessKeyID,
0 ignored issues
show
Documentation introduced by
The property accessKeyID does not exist on object<Toplan\PhpSms\AliyuncsAgent>. 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...
27
            'SignatureVersion' => $this->signatureVersion,
0 ignored issues
show
Documentation introduced by
The property signatureVersion does not exist on object<Toplan\PhpSms\AliyuncsAgent>. 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...
28
            'SignatureMethod' => $this->signatureMethod,
0 ignored issues
show
Documentation introduced by
The property signatureMethod does not exist on object<Toplan\PhpSms\AliyuncsAgent>. 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...
29
            'SignatureNonce'=> uniqid(),
30
            'Timestamp' => Carbon::now('UTC')->format('Y-m-d\TH:i:s\Z'),
31
            'RegionId' => 'cn-hangzhou',
32
            // 接口参数
33
            'Action' => 'SingleSendSms',
34
            'SignName' => $tempData['signName'],
35
            'ParamString' => $this->getTempDataString($data),
36
            'RecNum' => $this->recNum($to),
37
            'TemplateCode' => $tempData['templateCode'],
38
        ];
39
40
        $url = 'https://sms.aliyuncs.com/';
41
        $params['Signature'] = $this->computeSignature($params, $this->accessKeySecret);
0 ignored issues
show
Documentation introduced by
The property accessKeySecret does not exist on object<Toplan\PhpSms\AliyuncsAgent>. 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...
42
        $params = http_build_query($params);
43
        $response = $this->sockPost($url, $params);
0 ignored issues
show
Documentation introduced by
$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...
44
        $this->setResult($response);
45
    }
46
47
    protected function getTempDataString(array $data)
48
    {
49
        $data = array_map(function ($value) {
50
            return (string) $value;
51
        }, $data);
52
53
        return json_encode($data);
54
    }
55
56
    public function sendContentSms($to, $content)
57
    {
58
    }
59
60
    public function voiceVerify($to, $code, $tempId, array $data)
61
    {
62
    }
63
64
    public function percentEncode($str)
65
    {
66
        // 使用urlencode编码后,将"+","*","%7E"做替换即满足ECS API规定的编码规范
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
67
        $res = urlencode($str);
68
        $res = preg_replace('/\+/', '%20', $res);
69
        $res = preg_replace('/\*/', '%2A', $res);
70
        $res = preg_replace('/%7E/', '~', $res);
71
        return $res;
72
    }
73
74
    public function computeSignature($parameters, $accessKeySecret)
75
    {
76
        // 将参数Key按字典顺序排序
77
        ksort($parameters);
78
        // 生成规范化请求字符串
79
        $canonicalizedQueryString = '';
80
        foreach ($parameters as $key => $value) {
81
            $canonicalizedQueryString .= '&' . $this->percentEncode($key)
82
            . '=' . $this->percentEncode($value);
83
        }
84
        // 生成用于计算签名的字符串 stringToSign
85
        $stringToSign = 'POST&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
86
        // 计算签名,注意accessKeySecret后面要加上字符'&'
87
        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));
88
        return $signature;
89
    }
90
91
    public function recNum($data)
92
    {
93
        if (is_array($data)) {
94
            $data = implode(',', $data);
95
        }
96
        return $data;
97
    }
98
99
    protected function setResult($result)
100
    {
101
        $result =json_decode($result, true);
102
        if (isset($result['Message'])) {
103
            $this->result(Agent::INFO, '请求失败');
104
        } else {
105
            $this->result(Agent::INFO, $result);
106
            $this->result(Agent::SUCCESS, (bool) $result);
107
            $this->result(Agent::CODE, 0);
108
        }
109
    }
110
}
111