1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ParasiticAgent |
7
|
|
|
* 寄生代理器 |
8
|
|
|
* |
9
|
|
|
* @property \Closure $sendSms |
10
|
|
|
* @property \Closure $voiceVerify |
11
|
|
|
*/ |
12
|
|
|
class ParasiticAgent extends Agent |
13
|
|
|
{ |
14
|
|
|
protected $sendSmsRunning = false; |
15
|
|
|
|
16
|
|
|
protected $voiceVerifyRunning = false; |
17
|
|
|
|
18
|
|
View Code Duplication |
public function sendSms($to, $content, $tempId, array $data) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
if (!is_callable($this->sendSms)) { |
21
|
|
|
throw new PhpSmsException('Expected the higher-order scheme option `sendSms` to be a closure.'); |
22
|
|
|
} |
23
|
|
|
if ($this->sendSmsRunning) { |
24
|
|
|
throw new PhpSmsException('Do not call `$agent->sendSms()` in the closure.'); |
25
|
|
|
} |
26
|
|
|
$this->sendSmsRunning = true; |
27
|
|
|
try { |
28
|
|
|
call_user_func_array($this->sendSms, [$this, $to, $content, $tempId, $data]); |
29
|
|
|
$this->sendSmsRunning = false; |
30
|
|
|
} catch (\Exception $e) { |
31
|
|
|
$this->sendSmsRunning = false; |
32
|
|
|
|
33
|
|
|
throw $e; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function voiceVerify($to, $code, $tempId, array $data) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if (!is_callable($this->voiceVerify)) { |
40
|
|
|
throw new PhpSmsException('Expected the higher-order scheme option `voiceVerify` to be a closure.'); |
41
|
|
|
} |
42
|
|
|
if ($this->voiceVerifyRunning) { |
43
|
|
|
throw new PhpSmsException('Do not call `$agent->voiceVerify()` in the closure.'); |
44
|
|
|
} |
45
|
|
|
$this->voiceVerifyRunning = true; |
46
|
|
|
try { |
47
|
|
|
call_user_func_array($this->voiceVerify, [$this, $to, $code, $tempId, $data]); |
48
|
|
|
$this->voiceVerifyRunning = false; |
49
|
|
|
} catch (\Exception $e) { |
50
|
|
|
$this->voiceVerifyRunning = false; |
51
|
|
|
|
52
|
|
|
throw $e; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
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.