1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\SelfDiagnosis\Checks; |
4
|
|
|
|
5
|
|
|
use BeyondCode\SelfDiagnosis\Exceptions\InvalidConfigurationException; |
6
|
|
|
use BeyondCode\SelfDiagnosis\Server; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use JJG\Ping; |
10
|
|
|
|
11
|
|
|
class ServersArePingable implements Check |
12
|
|
|
{ |
13
|
|
|
protected const DEFAULT_TIMEOUT = 5; |
14
|
|
|
|
15
|
|
|
/** @var Collection */ |
16
|
|
|
protected $notReachableServers; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The name of the check. |
20
|
|
|
* |
21
|
|
|
* @param array $config |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function name(array $config): string |
25
|
|
|
{ |
26
|
|
|
return trans('self-diagnosis::checks.servers_are_pingable.name'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Perform the actual verification of this check. |
31
|
|
|
* |
32
|
|
|
* @param array $config |
33
|
|
|
* @return bool |
34
|
|
|
* @throws InvalidConfigurationException |
35
|
|
|
*/ |
36
|
28 |
|
public function check(array $config): bool |
37
|
|
|
{ |
38
|
28 |
|
$this->notReachableServers = $this->parseConfiguredServers(Arr::get($config, 'servers', [])); |
39
|
16 |
|
if ($this->notReachableServers->isEmpty()) { |
40
|
4 |
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
6 |
|
$this->notReachableServers = $this->notReachableServers->reject(function (Server $server) { |
44
|
12 |
|
$ping = new Ping($server->getHost()); |
45
|
12 |
|
$ping->setPort($server->getPort()); |
46
|
12 |
|
$ping->setTimeout($server->getTimeout()); |
47
|
|
|
|
48
|
12 |
|
if ($ping->getPort() === null) { |
49
|
12 |
|
$latency = $ping->ping('exec'); |
50
|
|
|
} else { |
51
|
|
|
$latency = $ping->ping('fsockopen'); |
52
|
|
|
} |
53
|
|
|
|
54
|
12 |
|
return $latency !== false; |
55
|
12 |
|
}); |
56
|
|
|
|
57
|
12 |
|
return $this->notReachableServers->isEmpty(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The error message to display in case the check does not pass. |
62
|
|
|
* |
63
|
|
|
* @param array $config |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
4 |
|
public function message(array $config): string |
67
|
|
|
{ |
68
|
4 |
|
return $this->notReachableServers->map(function (Server $server) { |
69
|
8 |
|
return trans('self-diagnosis::checks.servers_are_pingable.message', [ |
70
|
8 |
|
'host' => $server->getHost(), |
71
|
8 |
|
'port' => $server->getPort() ?? 'n/a', |
72
|
8 |
|
'timeout' => $server->getTimeout(), |
73
|
|
|
]); |
74
|
8 |
|
})->implode(PHP_EOL); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Parses an array of servers which can be given in different formats. |
79
|
|
|
* Unifies the format for the resulting collection. |
80
|
|
|
* |
81
|
|
|
* @param array $servers |
82
|
|
|
* @return Collection |
83
|
|
|
* @throws InvalidConfigurationException |
84
|
|
|
*/ |
85
|
28 |
|
private function parseConfiguredServers(array $servers): Collection |
86
|
|
|
{ |
87
|
28 |
|
$result = new Collection(); |
88
|
|
|
|
89
|
28 |
|
foreach ($servers as $server) { |
90
|
24 |
|
if (is_array($server)) { |
91
|
20 |
|
if (!empty(Arr::except($server, ['host', 'port', 'timeout']))) { |
92
|
4 |
|
throw new InvalidConfigurationException('Servers in array notation may only contain a host, port and timeout parameter.'); |
93
|
|
|
} |
94
|
16 |
|
if (!Arr::has($server, 'host')) { |
95
|
4 |
|
throw new InvalidConfigurationException('For servers in array notation, the host parameter is required.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
12 |
|
$host = Arr::get($server, 'host'); |
99
|
12 |
|
$port = Arr::get($server, 'port'); |
100
|
12 |
|
$timeout = Arr::get($server, 'timeout', self::DEFAULT_TIMEOUT); |
101
|
|
|
|
102
|
12 |
|
$result->push(new Server($host, $port, $timeout)); |
103
|
4 |
|
} else if (is_string($server)) { |
104
|
|
|
$result->push(new Server($server, null, self::DEFAULT_TIMEOUT)); |
105
|
|
|
} else { |
106
|
10 |
|
throw new InvalidConfigurationException('The server configuration may only contain arrays or strings.'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
16 |
|
return $result; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|