Completed
Pull Request — master (#74)
by
unknown
06:53
created

ServersArePingable   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.19%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 102
ccs 33
cts 37
cp 0.8919
rs 10
c 0
b 0
f 0

4 Methods

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