YunTongXunAgent::singlesSend()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 2
1
<?php
2
3
namespace Laravelsms\Sms\Agents;
4
5
use Illuminate\Support\Arr;
6
use Laravelsms\Sms\Contracts\Sms;
7
8
class YunTongXunAgent extends Sms
9
{
10
    protected $config = [];
11
12
    private $accountSid;
13
    private $appId;
14
15
    private $host = 'https://app.cloopen.com:8883';
16
    private $singleSendUrl = '/2013-12-26/Accounts/{accountSid}/SMS/TemplateSMS?sig={SigParameter}';
17
18
    private $sigParameter;
19
    private $header;
20
21
    public function __construct($config)
22
    {
23
        $this->config = $config;
24
        $this->transformConfig();
25
    }
26
27
    protected function transformConfig()
28
    {
29
        $postDateTime = date("YmdHis");
30
31
        $this->setTemplateId(Arr::pull($this->config, 'templateId'));
32
33
        $credentials = Arr::pull($this->config, 'credentials');
34
        $this->accountSid = Arr::pull($credentials, 'accountSid');
35
        $accountToken = Arr::pull($credentials, 'accountToken');
36
        $this->appId = Arr::pull($credentials, 'appId');
37
        $this->sigParameter = strtoupper(md5($this->accountSid . $accountToken . $postDateTime));
38
39
        $authorization = base64_encode($this->accountSid . ":" . $postDateTime);
40
        $this->header = array(
41
            'Content-Type:application/json;charset=utf-8',
42
            'Accept:application/json',
43
            'Authorization:' . $authorization
44
        );
45
    }
46
47
    public function singlesSend($mobile, $send = true)
48
    {
49
        $url = $this->host . $this->singleSendUrl;
50
        $url = str_replace('{accountSid}', $this->accountSid, $url);
51
        $url = str_replace('{SigParameter}', $this->sigParameter, $url);
52
53
        $data = '';
54
        while (list(, $value) = each($this->templateVar)) {
55
            $data .= $value . ",";
56
        }
57
        $data = rtrim($data, ',');
58
59
        $postData = "{'to':'$mobile','templateId':'$this->templateId','appId':'$this->appId','datas':[" . $data . "]}";
60
61
        if ($send) {
62
            return $this->curl($url, $postData);
0 ignored issues
show
Documentation introduced by
$postData 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...
63
        }
64
65
        return $postData;
66
    }
67
68
    /**
69
     * @param $url
70
     * @param array $postData
71
     * @return array $result
72
     * @return int $result[].code 返回0则成功,返回其它则错误
73
     * @return string $result[].msg 返回消息
74
     * @return mixed $result[].verifyCode 验证码
75
     */
76 View Code Duplication
    protected function curl($url, $postData)
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...
77
    {
78
        $ch = curl_init();
79
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
80
        curl_setopt($ch, CURLOPT_URL, $url);
81
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
82
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
83
        curl_setopt($ch, CURLOPT_HEADER, 0);
84
        curl_setopt($ch, CURLOPT_POST, true);
85
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
86
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
87
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
88
        $httpResponse = $this->httpResponse($ch);
89
        $result = $this->transformerResponse($httpResponse);
90
        curl_close($ch);
91
92
        return $result;
93
    }
94
95 View Code Duplication
    protected function transformerResponse($httpResponse)
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...
96
    {
97
        if (empty($httpResponse['error'])) {
98
            $response = json_decode($httpResponse['jsonData'], true);
99
            if ($response['statusCode'] == '000000') {
100
                $result = ['code' => 0, 'msg' => '发送成功', 'verifyCode' => $this->verifyCode];
101
            } else {
102
                $result = ['code' => $response['statusCode'], 'msg' => 'YunTongXun:' . $response['statusCode']];
103
            }
104
            unset($response);
105
        } else {
106
            $result = ['code' => time(), 'msg' => $httpResponse['error']];
107
        }
108
109
        return $result;
110
    }
111
}
112