GeoLocation::getLocation()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
nc 3
nop 3
dl 0
loc 14
c 1
b 0
f 0
cc 3
ccs 6
cts 6
cp 1
crap 3
rs 10
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