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

LuosimaoAgent   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 9
c 4
b 1
f 1
lcom 1
cbo 1
dl 7
loc 70
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A sendSms() 0 12 4
A sendContentSms() 0 10 1
A voiceVerify() 0 10 1
A LuosimaoCurl() 0 20 1
A setResult() 7 7 1
A sendTemplateSms() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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