IPGeoJSONController::getIPData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * IP Controller
5
 */
6
7
namespace Hepa19\IPGeo;
8
9
use Anax\Commons\ContainerInjectableInterface;
10
use Anax\Commons\ContainerInjectableTrait;
11
12
/**
13
 * Controller for IP validator API
14
 *
15
 */
16
class IPGeoJSONController implements ContainerInjectableInterface
17
{
18
    use ContainerInjectableTrait;
19
20
    /**
21
     * Index page
22
     *
23
     * @return array
24
     */
25 3
    public function indexActionGet() : array
26
    {
27 3
        $request = $this->di->get("request");
28 3
        $ip = $request->getGet("ip");
29
30 3
        if (empty($ip)) {
31 1
            $ipgetcurrent = $this->di->get("ipgetcurrent");
32 1
            $ip = $ipgetcurrent->getIP();
33
        }
34
35 3
        $validator = $this->di->get("validator");
36 3
        $valid = $validator->isValid($ip);
37 3
        $protocol = $validator->getProtocol($ip);
38 3
        $host = $validator->getHost($ip);
39 3
        $ipstackRes = $this->getIPData($ip);
40 3
        $map = $this->di->get("map");
41 3
        $mapLink = $map->getMap($ipstackRes["longitude"], $ipstackRes["latitude"]);
42
43
        $data = [
44 3
            "ip" => $ip,
45 3
            "valid" => $valid ?? null,
46 3
            "protocol" => $protocol ?? null,
47 3
            "host" => $host ?? null,
48 3
            "country_name" => $ipstackRes["country_name"] ?? null,
49 3
            "city" => $ipstackRes["city"] ?? null,
50 3
            "longitude" => $ipstackRes["longitude"] ?? null,
51 3
            "latitude" => $ipstackRes["latitude"] ?? null,
52 3
            "map_link" => $mapLink ?? null
53
        ];
54
55 3
        return [$data];
56
    }
57
58
59
60
    /**
61
     * Get ip data
62
     *
63
     */
64
    public function getIPData($ip)
65
    {
66
        $ipstack = $this->di->get("ipstack");
67
        $ipstack->setUrl($ip);
68
        $ipstackRes = $ipstack->getData();
69
70
        return $ipstackRes;
71
    }
72
}
73