IpController::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\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
 * 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 IpController implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
25
26
27
    /**
28
     * @var string $db a sample member variable that gets initialised
29
     */
30
    private $db = "not active";
31
32
33
34
    /**
35
     * The initialize method is optional and will always be called before the
36
     * target method/action. This is a convienient method where you could
37
     * setup internal properties that are commonly used by several methods.
38
     *
39
     * @return void
40
     */
41 11
    public function initialize() : void
42
    {
43
        // Use to initialise member variables.
44 11
        $this->db = "active";
45 11
    }
46
47
48
49
    /**
50
     * This is the index method action, it handles:
51
     * ANY METHOD mountpoint
52
     * ANY METHOD mountpoint/
53
     * ANY METHOD mountpoint/index
54
     *
55
     * @return object
56
     */
57 1
    public function indexAction() : object
58
    {
59
        // Deal with the action and return a response.
60 1
        $page = $this->di->get("page");
61
62 1
        $page->add("anax/v2/ip/index");
63 1
        return $page->render(
64
            [
65 1
                "title" => "Validera Ip-adress",
66
                "baseTitle" => " | Anax development utilities"
67
            ]
68
        );
69
    }
70
71
72
73
    /**
74
     * This is the index method action, it handles:
75
     * ANY METHOD mountpoint
76
     * ANY METHOD mountpoint/
77
     * ANY METHOD mountpoint/index
78
     *
79
     * @return string
80
     */
81 4
    public function validateActionGet() : object
82
    {
83 4
        $page = $this->di->get("page");
84 4
        $request = $this->di->get("request");
85 4
        if ($request->getGet("data") == "json") {
86 1
            return $this->di->get("response")->redirect("ip/json?ip=" . $request->getGet("ip"));
87
        }
88 3
        $client = $request->getServer('REMOTE_ADDR');
89 3
        $ipAddress = $request->getGet("ip") ? $request->getGet("ip") : $client;
90
91 3
        $ipm = $this->di->get("callurlmodel");
92 3
        $ipm->setIpAddress($ipAddress);
93
94 3
        $page->add("anax/v2/ip/validate", $ipm->validateIp());
95 3
        return $page->render(
96
            [
97 3
                "title" => "Validera Ip-adress",
98
                "baseTitle" => " | Anax development utilities"
99
            ]
100
        );
101
    }
102
103
104
    /**
105
     * This is the index method action, it handles:
106
     * ANY METHOD mountpoint
107
     * ANY METHOD mountpoint/
108
     * ANY METHOD mountpoint/index
109
     *
110
     * @return string
111
     */
112 6
    public function geoInfoActionGet() : object
113
    {
114 6
        $page = $this->di->get("page");
115 6
        $request = $this->di->get("request");
116 6
        if ($request->getGet("data") == "json") {
117 1
            return $this->di->get("response")->redirect("ip/geojson?ip=" . $request->getGet("ip"));
118
        }
119 5
        $ipAddress = $request->getGet("ip") ? $request->getGet("ip") : $request->getServer('REMOTE_ADDR');
120 5
        $ipm = $this->di->get("callurlmodel");
121 5
        $ipm->setIpAddress($ipAddress);
122 5
        $page->add("anax/v2/ip/geo-info", $ipm->fetchGeoInfo());
123 5
        return $page->render(
124
            [
125 5
                "title" => "Hämta geografisk information från Ip-adress",
126
                "baseTitle" => " | Anax development utilities"
127
            ]
128
        );
129
    }
130
}
131