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) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.