WeatherIpValidation::isValidMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Lefty\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
// use Anax\Route\Exception\ForbiddenException;
9
// use Anax\Route\Exception\NotFoundException;
10
// use Anax\Route\Exception\InternalErrorException;
11
12
/**
13
 * A sample controller to show how a controller class can be implemented.
14
 * The controller will be injected with $di if implementing the interface
15
 * ContainerInjectableInterface, like this sample class does.
16
 * The controller is mounted on a particular route and can then handle all
17
 * requests for that mount point.
18
 *
19
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
20
 */
21
22
23
class WeatherIpValidation implements ContainerInjectableInterface
24
{
25
    use ContainerInjectableTrait;
26
27
    private $ipAddress = "";
28
    private $isValid = false;
29
    private $isValidIPv4 = false;
30
    private $isValidIPv6 = false;
31
    private $isValidMessage = " is not a valid IP";
32
    private $hostname;
33
    private $geoLocation = [];
34
35 10
    public function __construct(string $value)
36
    {
37 10
        $this->ipAddress = $value;
38 10
        $this->validate();
39
        // if ($this->isValidIPv4) {
40
        //     $this->geoLocation($this->ipAddress);
41
        // }
42 10
    }
43
44
45 10
    private function validate()
46
    {
47 10
        if (filter_var($this->ipAddress, FILTER_VALIDATE_IP)) {
48 6
            $this->isValid = true;
49 6
            $this->isValidIPv4 = boolval(filter_var($this->ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
50 6
            $this->isValidIPv6 = boolval(filter_var($this->ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
51 6
            $this->isValidMessage = " is a valid IP address";
52 6
            $this->hostname = gethostbyaddr($this->ipAddress);
53
        }
54 10
    }
55
56 10
    public function isValidIPv4()
57
    {
58 10
        return $this->isValidIPv4;
59
    }
60
61 10
    public function isValidIPv6()
62
    {
63 10
        return $this->isValidIPv6;
64
    }
65
66 10
    public function isValid()
67
    {
68 10
        return $this->isValid;
69
    }
70
71 10
    public function getIp()
72
    {
73 10
        return $this->ipAddress;
74
    }
75
76 10
    public function getGeoLocation()
77
    {
78 10
        return $this->geoLocation;
79
    }
80
81 10
    public function getHostname()
82
    {
83 10
        return $this->hostname;
84
    }
85
86 10
    public function isValidMessage()
87
    {
88 10
        return $this->isValidMessage;
89
    }
90
91
92 10
    public function answer()
93
    {
94
95
        $data = [
96 10
            "isValid" => $this->isValid(),
97 10
            "isValidIPv4" => $this->isValidIPv4(),
98 10
            "isValidIPv6" => $this->isValidIPv6(),
99 10
            "isValidMessage" => $this->isValidMessage(),
100 10
            "ip" => $this->getIp(),
101 10
            "hostname" => $this->getHostname(),
102 10
            "geoLocation" => $this->getGeoLocation()
103
            ];
104
105 10
        return $data;
106
    }
107
}
108