IpGeoController::indexActionGet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 14
nc 1
nop 0
dl 0
loc 24
c 0
b 0
f 0
cc 1
ccs 14
cts 14
cp 1
crap 1
rs 9.7998
1
<?php
2
3
namespace Blixter\IpGeolocation;
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
 * A sample controller to show how a controller class can be implemented.
14
 * The controller will be injected with $di if implementing the interface
15
 * ContainerInjectableInterface, like this sample class does.
16
 * The controller is mounted on a particular route and can then handle all
17
 * requests for that mount point.
18
 *
19
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
20
 */
21
class IpGeoController implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
25
    /**
26
     * This is the index method action, it handles:
27
     * GET METHOD mountpoint
28
     * GET METHOD mountpoint/
29
     * GET METHOD mountpoint/index
30
     *
31
     * @return object
32
     */
33 2
    public function indexActionGet(): object
34
    {
35
        // Add content as a view and then render the page
36 2
        $page = $this->di->get("page");
37
38 2
        $title = "Geolokalisera en Ip-adress";
39
40 2
        $curlhandler = $this->di->get("curlhandler");
41 2
        $ipGeoModel = $this->di->get("ipgeo");
42 2
        $ipGeoModel->setCurl($curlhandler);
43
44 2
        $request = $this->di->get("request");
45 2
        $userIp = $ipGeoModel->getUserIpAddr($request);
46
        $data = [
47 2
            "title" => $title,
48 2
            "userIp" => $userIp,
49
        ];
50
51 2
        $page->add("blixter/ipgeolocation/header", $data);
52 2
        $page->add("blixter/ipgeolocation/search-ip", $data);
53
54
        // Deal with the action and return a response.
55 2
        return $page->render([
56 2
            "title" => $title,
57
        ]);
58
    }
59
60
    /**
61
     * This is the index method action, it handles:
62
     * POST METHOD mountpoint
63
     * POST METHOD mountpoint/
64
     * POST METHOD mountpoint/index
65
     *
66
     * @return object
67
     */
68 2
    public function indexActionPost(): object
69
    {
70
        // Add content as a view and then render the page
71 2
        $page = $this->di->get("page");
72 2
        $request = $this->di->get("request");
73
74
        // Using ipValidation class from $di.
75 2
        $ipValidation = $this->di->get("ipvalidation");
76 2
        $title = "Geolokalisering av Ip-adress";
77 2
        $ipaddress = $request->getPost("ipaddress");
78
79 2
        $curlhandler = $this->di->get("curlhandler");
80 2
        $ipGeoModel = $this->di->get("ipgeo");
81 2
        $ipGeoModel->setCurl($curlhandler);
82
83 2
        $isIpValid = $ipValidation->isIpValid($ipaddress);
84
85 2
        if ($isIpValid) {
86 1
            $protocol = $ipValidation->getProtocol($ipaddress);
87 1
            $domain = $ipValidation->getdomain($ipaddress);
88 1
            $apiRes = $ipGeoModel->fetchData($ipaddress);
89
        }
90
91
        $data = [
92 2
            "title" => $title,
93 2
            "ipaddress" => $ipaddress,
94 2
            "isIpValid" => $isIpValid,
95 2
            "protocol" => $protocol ?? null,
96 2
            "domain" => $domain ?? null,
97 2
            "apiRes" => $apiRes ?? null,
98
        ];
99
100 2
        $page->add("blixter/ipgeolocation/header", $data);
101 2
        $page->add("blixter/ipgeolocation/ip-result", $data);
102
103
        // Deal with the action and return a response.
104 2
        return $page->render([
105 2
            "title" => $title,
106
        ]);
107
    }
108
}
109