Completed
Pull Request — master (#17)
by lan tian
03:35
created

ParasiticAgent   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 67.86 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 2
dl 38
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sendSms() 19 19 4
A sendContentSms() 0 4 1
A sendTemplateSms() 0 4 1
A voiceVerify() 19 19 4

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 ParasiticAgent
7
 * 寄生代理器
8
 *
9
 * @property string   $name
10
 * @property \Closure $sendSms
11
 * @property \Closure $voiceVerify
12
 */
13
class ParasiticAgent extends Agent
14
{
15
    protected $sendSmsRunning = false;
16
17
    protected $voiceVerifyRunning = false;
18
19 View Code Duplication
    public function sendSms($tempId, $to, array $tempData, $content)
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...
20
    {
21
        if (!is_callable($this->sendSms)) {
22
            $name = $this->name;
23
            throw new PhpSmsException("Please give parasitic agent [$name] a callable param named `sendSms` by enable config.");
24
        }
25
        if (!$this->sendSmsRunning) {
26
            $this->sendSmsRunning = true;
27
            try {
28
                call_user_func_array($this->sendSms, [$this, $tempId, $to, $tempData, $content]);
29
            } catch (\Exception $e) {
30
                $this->sendSmsRunning = false;
31
                throw $e;
32
            }
33
            $this->sendSmsRunning = false;
34
        } else {
35
            throw new PhpSmsException('Please do not use `$agent->sendSms()` in closure.');
36
        }
37
    }
38
39
    public function sendContentSms($to, $content)
40
    {
41
        throw new PhpSmsException('Parasitic agent does not support `sendContentSms` method.');
42
    }
43
44
    public function sendTemplateSms($tempId, $to, array $tempData)
45
    {
46
        throw new PhpSmsException('Parasitic agent does not support `sendTemplateSms` method.');
47
    }
48
49 View Code Duplication
    public function voiceVerify($to, $code)
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...
50
    {
51
        if (!is_callable($this->voiceVerify)) {
52
            $name = $this->name;
53
            throw new PhpSmsException("Please give parasitic agent [$name] a callable param named `voiceVerify` by enable config.");
54
        }
55
        if (!$this->voiceVerifyRunning) {
56
            $this->voiceVerifyRunning = true;
57
            try {
58
                call_user_func_array($this->voiceVerify, [$this, $to, $code]);
59
            } catch (\Exception $e) {
60
                $this->voiceVerifyRunning = false;
61
                throw $e;
62
            }
63
            $this->voiceVerifyRunning = false;
64
        } else {
65
            throw new PhpSmsException('Please do not use `$agent->voiceVerify()` in closure.');
66
        }
67
    }
68
}
69