ServersArePingable   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parseConfiguredServers() 0 32 7
A check() 0 23 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\SelfDiagnosis\Checks;
6
7
use BeyondCode\SelfDiagnosis\Checks\Check;
8
use BeyondCode\SelfDiagnosis\Checks\ServersArePingable as BaseServersArePingable;
9
use BeyondCode\SelfDiagnosis\Exceptions\InvalidConfigurationException;
10
use BeyondCode\SelfDiagnosis\Server;
11
use Illuminate\Support\Arr;
12
use Illuminate\Support\Collection;
13
use JJG\Ping;
14
15
use function is_array;
16
use function is_string;
17
use function parse_url;
18
19
class ServersArePingable extends BaseServersArePingable implements Check
20
{
21
    /**
22
     * Perform the actual verification of this check.
23
     *
24
     * @param array $config
25
     * @return bool
26
     * @throws \BeyondCode\SelfDiagnosis\Exceptions\InvalidConfigurationException
27
     */
28
    public function check(array $config): bool
29
    {
30
        $this->notReachableServers = $this->parseConfiguredServers(Arr::get($config, 'servers', []));
31
        if ($this->notReachableServers->isEmpty()) {
32
            return true;
33
        }
34
35
        $this->notReachableServers = $this->notReachableServers->reject(static function (Server $server) {
36
            $ping = new Ping($server->getHost());
37
            $ping->setPort($server->getPort());
38
            $ping->setTimeout($server->getTimeout());
39
40
            if ($ping->getPort() === null) {
41
                $latency = $ping->ping('exec');
42
            } else {
43
                $latency = $ping->ping('fsockopen');
44
            }
45
46
            return $latency !== false;
47
        });
48
49
        return $this->notReachableServers->isEmpty();
50
    }
51
52
    private function parseConfiguredServers(array $servers): Collection
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

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.

Loading history...
53
    {
54
        $result = new Collection();
55
56
        foreach ($servers as $server) {
57
            if (is_array($server)) {
58
                if (! empty(Arr::except($server, ['host', 'port', 'timeout']))) {
59
                    throw new InvalidConfigurationException('Servers in array notation may only contain a host, port and timeout parameter.');
60
                }
61
                if (! Arr::has($server, 'host')) {
62
                    throw new InvalidConfigurationException('For servers in array notation, the host parameter is required.');
63
                }
64
65
                $host = Arr::get($server, 'host');
66
                $port = Arr::get($server, 'port');
67
                $timeout = Arr::get($server, 'timeout', self::DEFAULT_TIMEOUT);
68
69
                $parsed = parse_url($host);
70
                $host = $parsed['host'] ?? $parsed['path'];
71
                if (empty($port)) {
72
                    $port = $parsed['port'] ?? null;
73
                }
74
                $result->push(new Server($host, $port, $timeout));
75
            } elseif (is_string($server)) {
76
                $result->push(new Server($server, null, self::DEFAULT_TIMEOUT));
77
            } else {
78
                throw new InvalidConfigurationException('The server configuration may only contain arrays or strings.');
79
            }
80
        }
81
82
        return $result;
83
    }
84
}
85