Completed
Push — master ( 13cabb...9cf391 )
by lan tian
6s
created

LuosimaoAgent::setResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 7
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Toplan\PhpSms;
4
5
/**
6
 * Class LuosimaoAgent
7
 *
8
 * @property string $apikey
9
 * @property string $voiceApikey
10
 */
11
class LuosimaoAgent extends Agent
12
{
13
    public function sendSms($to, $content, $tempId, array $data)
14
    {
15
        // check content signature,
16
        // Luosimao signature must be in the content finally
17
        if ($content && !preg_match('/】$/', $content)) {
18
            preg_match('/【([0-9a-zA-Z\W]+)】/', $content, $matches);
19
            if (isset($matches[0])) {
20
                $content = str_replace($matches[0], '', $content) . $matches[0];
21
            }
22
        }
23
        $this->sendContentSms($to, $content);
24
    }
25
26
    public function sendContentSms($to, $content)
27
    {
28
        $url = 'https://sms-api.luosimao.com/v1/send.json';
29
        $optData = [
30
            'mobile'  => $to,
31
            'message' => $content,
32
        ];
33
        $data = $this->LuosimaoCurl($url, $optData, $this->apikey);
34
        $this->setResult($data);
35
    }
36
37
    public function voiceVerify($to, $code, $tempId, array $data)
38
    {
39
        $url = 'https://voice-api.luosimao.com/v1/verify.json';
40
        $optData = [
41
            'mobile' => $to,
42
            'code'   => $code,
43
        ];
44
        $data = $this->LuosimaoCurl($url, $optData, $this->voiceApikey);
45
        $this->setResult($data);
46
    }
47
48
    protected function LuosimaoCurl($url, $optData, $apikey)
49
    {
50
        $ch = curl_init();
51
        curl_setopt($ch, CURLOPT_URL, "$url");
52
53
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
54
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
55
        curl_setopt($ch, CURLOPT_HEADER, false);
56
57
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
58
        curl_setopt($ch, CURLOPT_USERPWD, 'api:key-' . $apikey);
59
60
        curl_setopt($ch, CURLOPT_POST, true);
61
        curl_setopt($ch, CURLOPT_POSTFIELDS, $optData);
62
63
        $res = curl_exec($ch);
64
        curl_close($ch);
65
66
        return $res;
67
    }
68
69 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...
70
    {
71
        $this->result(Agent::INFO, $result);
72
        $result = json_decode($result, true);
73
        $this->result(Agent::SUCCESS, $result['error'] === 0);
74
        $this->result(Agent::CODE, $result['error']);
75
    }
76
77
    public function sendTemplateSms($to, $tempId, array $data)
78
    {
79
    }
80
}
81