IPGeotag   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 135
ccs 51
cts 51
cp 1
rs 10
wmc 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 4
A getAllDataSorted() 0 12 2
A getAllData() 0 10 2
B getClientIP() 0 30 8
A build() 0 11 3
1
<?php
2
3
namespace Pamo\IPGeotag;
4
5
/**
6
 * IP validator
7
 */
8
class IPGeotag
9
{
10
    /**
11
     * @var string $baseAddress for the API.
12
     * @var string $apiKey for authentication.
13
     * @var array $allData to store Geotag data.
14
     * @var string $clientIP for the current IP Address.
15
     */
16
    private $baseAddress;
17
    private $apiKey;
18
    private $allData;
19
    private $clientIP;
20
21
22
23
    /**
24
     * Geotag current IP
25
     *
26
     * @param string $baseAddress for the API.
27
     * @param string $accessKey for authentication.
28
     *
29
     */
30 5
    public function init()
31
    {
32 5
        $filename = ANAX_INSTALL_PATH . "/config/api.php";
33 5
        $api =  file_exists($filename) ? require $filename : null;
34 5
        $this->baseAddress = $api ? $api["url"]["geoTag"] : getenv("API_URL_GEOTAG");
35 5
        $this->apiKey = $api ? $api["key"]["geoTag"] : getenv("API_KEY_GEOTAG");
36 5
        $this->allData = [];
37 5
    }
38
39
40
41
    /**
42
     * Valide an IP Address.
43
     *
44
     * @param string $ipAddress is the IP Address to validate.
45
     *
46
     * @return object
47
     */
48 1
    public function getAllData(string $ipAddress = null) : object
49
    {
50 1
        $ipAddress = ($ipAddress ? $ipAddress : $this->clientIP);
51 1
        $data = file_get_contents(
52 1
            "$this->baseAddress/$ipAddress?access_key=$this->apiKey"
53
        );
54
55 1
        $dataJson = json_decode($data);
56
57 1
        return $dataJson;
58
    }
59
60
61
62
    /**
63
     * Valide an IP Address.
64
     *
65
     * @param string $ipAddress is the IP Address to validate.
66
     *
67
     * @return array
68
     */
69 1
    public function getAllDataSorted(string $ipAddress = null) : array
70
    {
71 1
        $ipAddress = ($ipAddress ? $ipAddress : $this->clientIP);
72 1
        $result = file_get_contents(
73 1
            "$this->baseAddress/$ipAddress?access_key=$this->apiKey"
74
        );
75
76 1
        $resultArray = json_decode($result, true);
77
78 1
        array_walk_recursive($resultArray, array('self', 'build'));
79
80 1
        return $this->allData;
81
    }
82
83
84
85
    /**
86
     * Build array with arrays, recursive
87
     *
88
     * @param string $value
89
     * @param string $key
90
     *
91
     */
92 1
    public function build($value, $key)
93
    {
94 1
        $key = str_replace("_", " ", $key);
95
96 1
        if ($value) {
97 1
            if (in_array($key, ["code", "name", "native"])) {
98 1
                $key = "Language " . $key;
99
            } else {
100 1
                $key = ucfirst($key);
101
            }
102 1
            $this->allData[$key] = $value;
103
        }
104 1
    }
105
106
107
108
    /**
109
     * Get client IP Address
110
     *
111
     * @return string
112
     */
113 3
    public function getClientIP($test = null) : string
114
    {
115 3
        if ($test) {
116 1
            return $test;
117
        } else {
118
            switch (true) {
119 2
                case (isset($_SERVER['HTTP_CLIENT_IP'])):
120 1
                    $ipAddress = $_SERVER['HTTP_CLIENT_IP'];
121 1
                    break;
122 2
                case (isset($_SERVER['HTTP_X_FORWARDED_FOR'])):
123 1
                    $ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
124 1
                    break;
125 2
                case (isset($_SERVER['HTTP_X_FORWARDED'])):
126 1
                    $ipAddress = $_SERVER['HTTP_X_FORWARDED'];
127 1
                    break;
128 2
                case (isset($_SERVER['HTTP_FORWARDED_FOR'])):
129 1
                    $ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
130 1
                    break;
131 2
                case (isset($_SERVER['HTTP_FORWARDED'])):
132 1
                    $ipAddress = $_SERVER['HTTP_FORWARDED'];
133 1
                    break;
134 2
                case (isset($_SERVER['REMOTE_ADDR'])):
135 1
                    $ipAddress = $_SERVER['REMOTE_ADDR'];
136 1
                    break;
137
                default:
138 1
                    $ipAddress = 'unknown';
139
            }
140
        }
141
142 2
        return $ipAddress;
143
    }
144
}
145