1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Lodash package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Longman\LaravelLodash\SelfDiagnosis\Checks; |
13
|
|
|
|
14
|
|
|
use BeyondCode\SelfDiagnosis\Checks\Check; |
15
|
|
|
use BeyondCode\SelfDiagnosis\Checks\ServersArePingable as BaseServersArePingable; |
16
|
|
|
use BeyondCode\SelfDiagnosis\Exceptions\InvalidConfigurationException; |
17
|
|
|
use BeyondCode\SelfDiagnosis\Server; |
18
|
|
|
use Illuminate\Support\Arr; |
19
|
|
|
use Illuminate\Support\Collection; |
20
|
|
|
use JJG\Ping; |
21
|
|
|
|
22
|
|
|
class ServersArePingable extends BaseServersArePingable implements Check |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Perform the actual verification of this check. |
26
|
|
|
* |
27
|
|
|
* @param array $config |
28
|
|
|
* @return bool |
29
|
|
|
* @throws \BeyondCode\SelfDiagnosis\Exceptions\InvalidConfigurationException |
30
|
|
|
*/ |
31
|
|
|
public function check(array $config): bool |
32
|
|
|
{ |
33
|
|
|
$this->notReachableServers = $this->parseConfiguredServers(Arr::get($config, 'servers', [])); |
34
|
|
|
if ($this->notReachableServers->isEmpty()) { |
35
|
|
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->notReachableServers = $this->notReachableServers->reject(function (Server $server) { |
39
|
|
|
$ping = new Ping($server->getHost()); |
40
|
|
|
$ping->setPort($server->getPort()); |
41
|
|
|
$ping->setTimeout($server->getTimeout()); |
42
|
|
|
|
43
|
|
|
if ($ping->getPort() === null) { |
44
|
|
|
$latency = $ping->ping('exec'); |
45
|
|
|
} else { |
46
|
|
|
$latency = $ping->ping('fsockopen'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $latency !== false; |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
return $this->notReachableServers->isEmpty(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function parseConfiguredServers(array $servers): Collection |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$result = new Collection(); |
58
|
|
|
|
59
|
|
|
foreach ($servers as $server) { |
60
|
|
|
if (is_array($server)) { |
61
|
|
|
if (! empty(Arr::except($server, ['host', 'port', 'timeout']))) { |
62
|
|
|
throw new InvalidConfigurationException('Servers in array notation may only contain a host, port and timeout parameter.'); |
63
|
|
|
} |
64
|
|
|
if (! Arr::has($server, 'host')) { |
65
|
|
|
throw new InvalidConfigurationException('For servers in array notation, the host parameter is required.'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$host = Arr::get($server, 'host'); |
69
|
|
|
$port = Arr::get($server, 'port'); |
70
|
|
|
$timeout = Arr::get($server, 'timeout', self::DEFAULT_TIMEOUT); |
71
|
|
|
|
72
|
|
|
$parsed = parse_url($host); |
73
|
|
|
$host = $parsed['host'] ?? $parsed['path']; |
74
|
|
|
if (empty($port)) { |
75
|
|
|
$port = $parsed['port'] ?? null; |
76
|
|
|
} |
77
|
|
|
$result->push(new Server($host, $port, $timeout)); |
78
|
|
|
} elseif (is_string($server)) { |
79
|
|
|
$result->push(new Server($server, null, self::DEFAULT_TIMEOUT)); |
80
|
|
|
} else { |
81
|
|
|
throw new InvalidConfigurationException('The server configuration may only contain arrays or strings.'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.