Test Failed
Push — master ( 892598...558941 )
by Antonio Carlos
11:17
created

PortCheck   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 12 2
A hosnameAndIp() 0 4 2
A makeFinalResult() 0 13 1
A portCheck() 0 12 2
A portIsNotConnectable() 0 8 1
1
<?php
2
3
namespace PragmaRX\Health\Checkers;
4
5
use PragmaRX\Health\Support\Result;
6
7
class PortCheck extends Base
8
{
9
    /**
10
     * Check the target.
11
     *
12
     * @return Result
13
     */
14 1
    public function check()
15
    {
16
        if (
17 1
            $this->portIsNotConnectable(
18 1
                $ipAddress = ip_address_from_hostname($this->target->hostname)
0 ignored issues
show
Bug introduced by
The property hostname does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
19
            )
20
        ) {
21 1
            return $this->makeFinalResult($ipAddress);
22
        }
23
24 1
        return $this->makeHealthyResult();
25
    }
26
27
    /**
28
     * Get hostname and IP.
29
     *
30
     * @param $hostname
31
     * @return mixed
32
     */
33 1
    protected function hosnameAndIp($hostname, $ipAdress)
34
    {
35 1
        return $hostname . ($hostname != $ipAdress ? " ({$ipAdress})" : '');
36
    }
37
38
    /**
39
     * Make the result.
40
     *
41
     * @param bool $ipAddress
42
     * @return Result
43
     */
44 1
    protected function makeFinalResult($ipAddress): Result
45
    {
46 1
        return $this->target->setResult(
47 1
            $this->makeResult(
48 1
                false,
49 1
                sprintf(
50 1
                    $this->target->getErrorMessage(),
51 1
                    $this->hosnameAndIp($this->target->hostname, $ipAddress),
0 ignored issues
show
Bug introduced by
The property hostname does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52 1
                    $this->target->port
0 ignored issues
show
Bug introduced by
The property port does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
                )
54
            )
55 1
        )->getResult();
56
    }
57
58 1
    public function portCheck($ipAddress, $port, $timeout)
59
    {
60 1
        $fp = @fsockopen($ipAddress, $port, $errno, $errstr, $timeout);
61
62 1
        if (gettype($fp) !== 'resource') {
63 1
            return false;
64
        }
65
66 1
        fclose($fp);
67
68 1
        return true;
69
    }
70
71
    /**
72
     * @param $ipAddress
73
     * @return bool
74
     */
75 1
    protected function portIsNotConnectable($ipAddress): bool
76
    {
77 1
        return !$this->portCheck(
78 1
            $ipAddress,
79 1
            $this->target->port,
0 ignored issues
show
Bug introduced by
The property port does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
80 1
            $this->target->timeout ?? 1
0 ignored issues
show
Bug introduced by
The property timeout does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
81
        );
82
    }
83
}
84