Passed
Push — master ( 86479e...2843a8 )
by Marcus
02:54
created

Ip   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 92.68%

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 128
ccs 38
cts 41
cp 0.9268
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getIpAddress() 0 6 3
A ipInfo() 0 14 1
A validateIp() 0 29 5
A validateCoord() 0 21 4
1
<?php
2
3
namespace Mahw17\Weather;
4
5
/**
6
 * Ip class, validates and get info about a specific IP
7
 */
8
class Ip
9
{
10
11
    /**
12
     * API KEY.
13
     */
14
    private $apiKey = null;
15
16
    /**
17
     * API URL.
18
     */
19
    private $apiUrl = null;
20
21
    /**
22
     * Constructor fetching api key and api url from specific supplier.
23
     *
24
     * @param array $config containing details for connecting to the supplier API.
25
     */
26 14
    public function __construct($config)
27
    {
28 14
        $this->apiKey = $config['ipstack']['apiKey'];
29 14
        $this->apiUrl = $config['ipstack']['apiUrl'];
30 14
    }
31
32
    /**
33
     * Validate ip address
34
     *
35
     * @param int $ipAddress ip address to validte.
36
     *
37
     * @return $this
38
     */
39 8
    public function validateIp($ipAddress)
40
    {
41
        // Default value on return attributes
42 8
        $valid = false;
43 8
        $ipType = false;
44 8
        $hostname = false;
45
46
        // Check if IP is valid either as a IPV4 or an IPV6
47 8
        if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
48 2
            $valid      = true;
49 2
            $ipType    = 'IPV4';
50 6
        } elseif (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
51 1
            $valid      = true;
52 1
            $ipType    = 'IPV6';
53
        }
54
55
        // If IP valid get connected domain, if not null
56 8
        if ($valid) {
57 3
            $hostname = $ipAddress !== gethostbyaddr($ipAddress) ? gethostbyaddr($ipAddress) : null;
58
        }
59
60
        // Collect and return results
61
        $result = [
62 8
            "ipAddress" => $ipAddress,
63 8
            "valid"     => $valid,
64 8
            "ipType"    => $ipType,
65 8
            "hostname"  => $hostname
66
        ];
67 8
        return $result;
68
    }
69
70
    /**
71
     * Validate ip:s coordinates
72
     *
73
     * @param int $ipAddress ip address to validte.
74
     *
75
     * @return mixed array if valid or boolean if false
76
     */
77 3
    public function validateCoord($ipAddress)
78
    {
79
        // Validate ip
80 3
        $ipValid = $this->validateIp($ipAddress)['valid'];
81
82
        // If valid Ip fetch and verify coordinates
83 3
        if ($ipValid) {
84 1
            $ipInfo = $this->ipInfo($ipAddress);
85
86 1
            if (isset($ipInfo->latitude) && isset($ipInfo->longitude)) {
87
                $coordinates = [
88
                    "lat" => $ipInfo->latitude,
89
                    "lon" => $ipInfo->longitude
90
                ];
91
            } else {
92 1
                return false;
93
            }
94
        } else {
95 2
            return false;
96
        }
97
        return $coordinates;
98
    }
99
100
101
102
    /**
103
     * Fetch ip information
104
     *
105
     * @param string $ipAddress
106
     *
107
     * @return array
108
     */
109 1
    public function ipInfo($ipAddress)
110
    {
111
        // Default value on return attributes
112 1
        $url = $this->apiUrl . $ipAddress . '?access_key=' . $this->apiKey;
113
114
        // Initiate new curl object
115 1
        $curl = new \Mahw17\Weather\Curl();
116
117
        // Get response
118 1
        $result = $curl->fetchSingleUrl($url);
119 1
        $result = json_decode($result);
120
121
        // Collect and return results
122 1
        return $result;
123
    }
124
125
    /**
126
     * Get user ip-adress.
127
     *
128
     * @return string ip-address
129
     */
130 1
    public function getIpAddress()
131
    {
132 1
        foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
133
        // foreach (array('REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED') as $key) {
134 1
            if (array_key_exists($key, $_SERVER) === true) {
135 1
                    return $_SERVER[$key];
136
            }
137
        }
138 1
    }
139
}
140