Completed
Push — master ( cc347c...57d870 )
by Marcel
15:46 queued 14:16
created

ServersArePingable::parseConfiguredServers()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 15
cts 16
cp 0.9375
rs 8.8657
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6.0087
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(array_except($server, ['host', 'port', 'timeout']))) {
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated with message: Arr::except() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
92 4
                    throw new InvalidConfigurationException('Servers in array notation may only contain a host, port and timeout parameter.');
93
                }
94 16
                if (!array_has($server, 'host')) {
0 ignored issues
show
Deprecated Code introduced by
The function array_has() has been deprecated with message: Arr::has() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
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