1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DucCnzj\Ip; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use DucCnzj\Ip\Imp\IpImp; |
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use DucCnzj\Ip\Imp\RequestHandlerImp; |
9
|
|
|
use DucCnzj\Ip\Exceptions\BreakLoopException; |
10
|
|
|
use DucCnzj\Ip\Exceptions\ServerErrorException; |
11
|
|
|
|
12
|
|
|
class RequestHandler implements RequestHandlerImp |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ClientInterface|null |
16
|
|
|
*/ |
17
|
|
|
protected $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $errors = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $tryTimes = 3; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return ClientInterface |
31
|
|
|
* |
32
|
|
|
* @author duc <[email protected]> |
33
|
|
|
*/ |
34
|
3 |
|
public function getClient(): ClientInterface |
35
|
|
|
{ |
36
|
3 |
|
return is_null($this->client) |
37
|
3 |
|
? $this->client = new Client() |
38
|
3 |
|
: $this->client; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
2 |
|
public function getErrors(): array |
45
|
|
|
{ |
46
|
2 |
|
return $this->errors; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $providers |
51
|
|
|
* @param string $ip |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
* @throws ServerErrorException |
55
|
|
|
* |
56
|
|
|
* @author duc <[email protected]> |
57
|
|
|
*/ |
58
|
6 |
|
public function send(array $providers, string $ip) |
59
|
|
|
{ |
60
|
6 |
|
foreach ($providers as $name => $provider) { |
61
|
6 |
|
for ($time = 1; $time <= $this->getTryTimes(); $time++) { |
62
|
|
|
try { |
63
|
|
|
/** @var IpImp $provider */ |
64
|
6 |
|
return array_merge($provider->send($this->getClient(), $ip), [ |
65
|
2 |
|
'provider' => $name, |
66
|
2 |
|
'success' => 1, |
67
|
|
|
]); |
68
|
6 |
|
} catch (ServerErrorException $e) { |
69
|
5 |
|
$this->logError($name, $e); |
70
|
|
|
|
71
|
5 |
|
continue; |
72
|
2 |
|
} catch (BreakLoopException $e) { |
73
|
1 |
|
$this->logError($name, $e); |
74
|
|
|
|
75
|
1 |
|
continue 2; |
76
|
1 |
|
} catch (\Exception $e) { |
77
|
1 |
|
$this->logError($name, $e); |
78
|
|
|
|
79
|
1 |
|
break 2; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
5 |
|
throw new ServerErrorException(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
8 |
|
public function getTryTimes(): int |
91
|
|
|
{ |
92
|
8 |
|
return $this->tryTimes; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param int $tryTimes |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
* |
100
|
|
|
* @author duc <[email protected]> |
101
|
|
|
*/ |
102
|
5 |
|
public function setTryTimes(int $tryTimes) |
103
|
|
|
{ |
104
|
5 |
|
$this->tryTimes = $tryTimes; |
105
|
|
|
|
106
|
5 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $name |
111
|
|
|
* @param \Exception $e |
112
|
|
|
* |
113
|
|
|
* @author duc <[email protected]> |
114
|
|
|
*/ |
115
|
6 |
|
public function logError(string $name, \Exception $e): void |
116
|
|
|
{ |
117
|
6 |
|
$this->errors[] = "provider: {$name}. " . $e->getMessage(); |
118
|
6 |
|
} |
119
|
|
|
} |
120
|
|
|
|