GeoLocation   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 59
c 1
b 0
f 0
ccs 24
cts 24
cp 1
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocation() 0 14 3
A __construct() 0 8 3
A getUserIp() 0 14 4
1
<?php
2
3
namespace Bjos\GeoLocation;
4
5
use Bjos\Curl\Curl;
6
7
class GeoLocation
8
{
9
    private $api;
10
    private $curl = null;
11
12
    /**
13
     *
14
     * @return void
15
     */
16 29
    public function __construct(object $curl)
17
    {
18 29
        $filePath = "/config/api_ipstack.php";
19 29
        $apiKey = "apiKey";
20 29
        $file = ANAX_INSTALL_PATH . $filePath;
21 29
        $ipStack = file_exists($file) ? require $file : null;
22 29
        $this->api = $ipStack ? $ipStack[$apiKey] : getenv("API_KEY");
23 29
        $this->curl = $curl;
24 29
    }
25
26
    /**
27
     * Method to get user ip.
28
     *
29
     * @return string|void $ipAdr
30
     */
31 12
    public function getUserIp()
32
    {
33 12
        $ipAdr = null;
34
35 12
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
36 1
            $ipAdr = $_SERVER['HTTP_CLIENT_IP'];
37 11
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
38 1
            $ipAdr = $_SERVER['HTTP_X_FORWARDED_FOR'];
39 10
        } elseif (!empty($_SERVER['REMOTE_ADDR'])) {
40 1
            $ipAdr = $_SERVER['REMOTE_ADDR'];
41
        } else {
42 9
            $ipAdr = "X.X.X.X";
43
        }
44 12
        return $ipAdr;
45
    }
46
47
    /**
48
     * Method to get location of a ip address.
49
     *
50
     * @return array|void $res
51
     */
52 8
    public function getLocation(
53
        string $ipAdr = null,
54
        string $url = null,
55
        string $option = null
56
    ) {
57 8
        $res = null;
58
59 8
        if ($url) {
60 7
            $url = $option ? $url . $ipAdr . $option . $this->api : $url;
61
62 7
            $res = $this->curl->curlApi($url);
63
        }
64
65 8
        return $res;
66
    }
67
}
68