Completed
Pull Request — master (#60)
by
unknown
02:31
created

ServersArePingable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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