GeoController::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 1
rs 9.9666
1
<?php
2
3
namespace Lioo19\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
// use Anax\Route\Exception\ForbiddenException;
9
// use Anax\Route\Exception\NotFoundException;
10
// use Anax\Route\Exception\InternalErrorException;
11
12
/**
13
 *
14
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
15
 */
16
class GeoController implements ContainerInjectableInterface
17
{
18
    use ContainerInjectableTrait;
19
20
    /**
21
     * This is the index method action, it handles:
22
     * ANY METHOD mountpoint
23
     * ANY METHOD mountpoint/
24
     * ANY METHOD mountpoint/index
25
     *
26
     * @return string
27
     */
28 1
    public function indexAction() : object
29
    {
30 1
        $page = $this->di->get("page");
31 1
        $request = $this->di->get("request");
32 1
        $ipDefault = $this->di->get("ipdefault");
33 1
        $usersIp = $ipDefault->getDefaultIp($request);
34
35
        $data = [
36 1
            "defaultIp" => $usersIp,
37
        ];
38
39
        //MAPPEN inte url
40 1
        $page->add("ip/iptest", $data);
41
42 1
        return $page->render([
43 1
            "title" => __METHOD__,
44
        ]);
45
    }
46
47
    /**
48
     * POST for ip, redirects to result-page
49
     * Sends the ip-adress with post and redirects
50
     *
51
     * @return object
52
     */
53 1
    public function validationActionPost() : object
54
    {
55 1
        $request = $this->di->get("request");
56 1
        $page = $this->di->get("page");
57 1
        $title = "Validera IP";
58
        //request to get the posted information
59 1
        $userip = $request->getPost("ipinput", null);
60
61 1
        $validation = $this->di->get("iptest");
62 1
        $validation->setInput($userip);
63
64 1
        $ip4 = $validation->ip4test();
65 1
        $ip6 = $validation->ip6test();
66
67 1
        if ($ip6 || $ip4) {
68 1
            $hostname = gethostbyaddr($userip);
69 1
            $geoInfo = $this->getGeo($userip);
70
        } else {
71 1
            $hostname = "Ej korrekt ip";
72 1
            $geoInfo = "Inget att visa";
73
        }
74
75
        $data = [
76 1
            "ip" => $userip,
77 1
            "ip4" => $ip4,
78 1
            "ip6" => $ip6,
79 1
            "hostname" => $hostname,
80 1
            "geoInfo" => $geoInfo,
81
        ];
82
83 1
        $page->add("ip/validation", $data);
84
85 1
        return $page->render([
86 1
            "title" => $title,
87
        ]);
88
    }
89
90
    public function getGeo($userip)
91
    {
92
        $geo = $this->di->get("ipgeo");
93
        $geo->setInput($userip);
94
        $geoInfo = $geo->fetchGeo();
95
96
        return $geoInfo;
97
    }
98
}
99