IPGeoController::indexActionGet()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 27
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 39
ccs 27
cts 27
cp 1
crap 3
rs 9.488
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
14
 *
15
 */
16
class IPGeoController implements ContainerInjectableInterface
17
{
18
    use ContainerInjectableTrait;
19
20
    /**
21
     * Index page
22
     *
23
     * @return object
24
     */
25 2
    public function indexActionGet() : object
26
    {
27 2
        $page = $this->di->get("page");
28 2
        $title = "Validera IP-adress";
29 2
        $request = $this->di->get("request");
30 2
        $ip = $request->getGet("ip");
31
32 2
        if (empty($ip)) {
33 1
            $ipgetcurrent = $this->di->get("ipgetcurrent");
34 1
            $ip = $ipgetcurrent->getIP();
35
        }
36
37 2
        $validator = $this->di->get("validator");
38 2
        $valid = $validator->isValid($ip);
39 2
        $protocol = $validator->getProtocol($ip);
40 2
        $host = $validator->getHost($ip);
41 2
        $ipstackRes = $this->getIPData($ip);
42
43
        $data = [
44 2
            "ip" => $ip ?? null,
45 2
            "valid" => $valid ?? null,
46 2
            "protocol" => $protocol ?? null,
47 2
            "host" => $host ?? null,
48 2
            "longitude" => $ipstackRes["longitude"] ?? null,
49 2
            "latitude" => $ipstackRes["latitude"] ?? null,
50 2
            "city" => $ipstackRes["city"] ?? null,
51 2
            "country_name" => $ipstackRes["country_name"] ?? null,
52
        ];
53
54 2
        $page->add("ip-geo/form", $data);
55
56 2
        if ($ip) {
57 2
            $page->add("ip-geo/result", $data);
58
        }
59
60 2
        $page->add("ip-geo/map", $data);
61
62 2
        return $page->render([
63 2
            "title" => $title
64
        ]);
65
    }
66
67
    /**
68
     * Get ip data
69
     *
70
     */
71
    public function getIPData($ip)
72
    {
73
        $ipstack = $this->di->get("ipstack");
74
        $ipstack->setUrl($ip);
75
        $ipstackRes = $ipstack->getData();
76
77
        return $ipstackRes;
78
    }
79
}
80