LuosimaoAgent   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 23.4 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 11
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setResult() 11 11 2
B sendContentSms() 0 17 5
A sendVoiceCode() 0 11 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 implements ContentSms, VoiceCode
12
{
13
    protected static $smsUrl = 'http://sms-api.luosimao.com/v1/send.json';
14
    protected static $voiceCodeUrl = 'http://voice-api.luosimao.com/v1/verify.json';
15
16
    public function sendContentSms($to, $content)
17
    {
18
        // 签名必须在最后面
19
        if ($content && preg_match('/(【[\\s\\S]*】)/', $content, $matches)) {
20
            if (isset($matches[0]) && strlen($matches[0])) {
21
                $content = str_replace($matches[0], '', $content) . $matches[0];
22
            }
23
        }
24
        $result = $this->curlPost(self::$smsUrl, [
25
            'mobile'  => $to,
26
            'message' => $content,
27
        ], [
28
            CURLOPT_HTTPAUTH    => CURLAUTH_BASIC,
29
            CURLOPT_USERPWD     => "api:key-{$this->apikey}",
30
        ]);
31
        $this->setResult($result);
32
    }
33
34
    public function sendVoiceCode($to, $code)
35
    {
36
        $result = $this->curlPost(self::$voiceCodeUrl, [
37
            'mobile' => $to,
38
            'code'   => $code,
39
        ], [
40
            CURLOPT_HTTPAUTH    => CURLAUTH_BASIC,
41
            CURLOPT_USERPWD     => "api:key-{$this->voiceApikey}",
42
        ]);
43
        $this->setResult($result);
44
    }
45
46 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...
47
    {
48
        if ($result['request']) {
49
            $this->result(Agent::INFO, $result['response']);
50
            $result = json_decode($result['response'], true);
51
            $this->result(Agent::SUCCESS, $result['error'] === 0);
52
            $this->result(Agent::CODE, $result['error']);
53
        } else {
54
            $this->result(Agent::INFO, 'request failed');
55
        }
56
    }
57
}
58