Completed
Pull Request — master (#80)
by
unknown
03:27
created

QcloudAgent::voiceVerify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

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 1
nc 1
nop 4
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class AlidayuAgent
7
 *
8
 * @property string $appid
9
 * @property string $appkey
10
 * @property string $sendUrl
11
 */
12
class QcloudAgent extends Agent
13
{
14
    public function sendSms($to, $content, $tempId, array $data)
15
    {
16
//        $this->sendContentSms($to, $content);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
17
	    $this->sendTemplateSms($to, $tempId ,$data);
18
    }
19
    public function sendContentSms($to, $content)
20
    {
21
        $params['to']=$to;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
22
        $params['content']=$content;
23
        $this->request($params);
24
    }
25
26
    public function sendTemplateSms($to, $tempId, array $data)
27
    {
28
	    $params['to']=$to;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
29
	    $params['tempId']=(int)$tempId;
30
	    $params['tempdata']=$data;
31
	    $this->request($params);
32
    }
33
34
    protected function request(array $params)
35
    {
36
        $randNum = rand(100000, 999999);
37
        $sendUrl = $this->sendUrl."?sdkappid=".$this->appid."&random=".$randNum;
38
	    if(isset($params['content'])){
39
		    $params = $this->createContentParams($params);
40
	    }elseif (isset($params['tempId'])){
41
		    $params = $this->createTemplateParams($params);
42
	    }
43
        $result = $this->QcloudCurl($sendUrl, $params);
44
//	    dd($result);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
45
        $this->setResult($result);
46
    }
47
	protected function createContentParams(array $params)
48
	{
49
		$tel = new \stdClass();
50
//        $telarr=explode('-',$params['to']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
51
		$tel->nationcode = '86';
52
		$tel->phone = $params['to'];
53
		$jsondata = new \stdClass();
54
		$jsondata->tel = $tel;
55
		$jsondata->type = "0";
56
		$jsondata->msg = $params['content'];
57
		$jsondata->sig = md5($this->appkey.$params['to']);
58
		$jsondata->extend = "";     // 根据需要添加,一般保持默认
59
		$jsondata->ext = "";        // 根据需要添加,一般保持默认
60
		$params =json_encode($jsondata);
61
		return $params;
62
	}
63
	protected function createTemplateParams(array $params)
64
	{
65
		$tel = new \stdClass();
66
//        $telarr=explode('-',$params['to']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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
		$tel->nationcode = '86';
68
		$tel->phone = $params['to'];
69
		$jsondata = new \stdClass();
70
		$jsondata->tel = $tel;
71
		$jsondata->type = '0';
72
		$jsondata->tpl_id=$params['tempId'];
73
		$jsondata->params=[$params['tempdata']['code'],(string)$params['tempdata']['minutes']];
74
		$jsondata->sig = md5($this->appkey.$params['to']);
75
		$jsondata->extend = "";     // 根据需要添加,一般保持默认
76
		$jsondata->ext = "";        // 根据需要添加,一般保持默认
77
		$params =json_encode($jsondata);
78
		return $params;
79
	}
80
	public function voiceVerify($to, $code, $tempId, array $data)
81
	{
82
83
	}
84
    protected function QcloudCurl($url, $optData)
85
    {
86
//	    dd($url,$optData);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
87
        $ch = curl_init();
88
        curl_setopt($ch, CURLOPT_URL, $url);
89
        curl_setopt($ch, CURLOPT_HEADER, 0);
90
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
91
        curl_setopt($ch, CURLOPT_POST, 1);
92
        curl_setopt($ch, CURLOPT_POSTFIELDS, $optData);
93
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
94
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
95
	    $response = curl_exec($ch);
96
	    if ($response === false) {
97
		    $request = false;
98
		    $response = curl_getinfo($ch);
99
	    }
100
	    curl_close($ch);
101
	     $response=json_decode($response,true);
102
	    return compact('request', 'response');
103
    }
104
    protected function genSign($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params 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...
105
    {
106
107
    }
108
    protected function setResult($result)
109
    {
110
        if ($result['response']['result']=='0') {
111
            $this->result(Agent::SUCCESS, true);
112
            $this->result(Agent::INFO, $result['response']['ext']);
113
            $this->result(Agent::CODE, 1111);
114
        } else {
115
            $this->result(Agent::SUCCESS, false);
116
            $this->result(Agent::INFO, $result['response']['errmsg']);
117
            $this->result(Agent::CODE, 0000);
118
        }
119
    }
120
    protected function genResponseName($method)
0 ignored issues
show
Unused Code introduced by
The parameter $method 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...
121
    {
122
    }
123
124
    protected function getTempDataString(array $data)
125
    {
126
        $data = array_map(function ($value) {
127
            return (string) $value;
128
        }, $data);
129
130
        return json_encode($data);
131
    }
132
133
134
}
135