IpGeoJsonController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 59
ccs 18
cts 18
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A indexActionGet() 0 23 4
A initialize() 0 5 1
1
<?php
2
3
namespace Anax\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Anax\IpGeo\IpGeo;
8
9
// use Anax\Route\Exception\ForbiddenException;
10
// use Anax\Route\Exception\NotFoundException;
11
// use Anax\Route\Exception\InternalErrorException;
12
13
/**
14
 * A sample JSON controller to show how a controller class can be implemented.
15
 * The controller will be injected with $di if implementing the interface
16
 * ContainerInjectableInterface, like this sample class does.
17
 * The controller is mounted on a particular route and can then handle all
18
 * requests for that mount point.
19
 */
20
class IpGeoJsonController implements ContainerInjectableInterface
21
{
22
    use ContainerInjectableTrait;
23
    protected $ipGeo;
24
    protected $request;
25
26
27
    /**
28
     * @var string $db a sample member variable that gets initialised
29
     */
30
31
32
    /**
33
     * The initialize method is optional and will always be called before the
34
     * target method/action. This is a convienient method where you could
35
     * setup internal properties that are commonly used by several methods.
36
     *
37
     * @return void
38
     */
39 5
    public function initialize() : void
40
    {
41
        // Use to initialise member variables.
42 5
        $this->ipGeo = new IpGeo();
43 5
        $this->request = $this->di->get("request");
44 5
    }
45
46
47
48
    /**
49
     * This is the index method action, it handles:
50
     * GET METHOD mountpoint
51
     * GET METHOD mountpoint/
52
     * GET METHOD mountpoint/index
53
     *
54
     * @return array
55
     */
56 5
    public function indexActionGet() : array
57
    {
58 5
        $ipAddress = $this->request->getGet("ip");
59 5
        if ($ipAddress) {
60 2
            $ipGeoInfo = $this->ipGeo->getLocation($ipAddress);
61
        } else {
62 3
            $req = $this->di->get("request");
63 3
            if (!empty($req->getServer("HTTP_CLIENT_IP"))) {
64 1
                $ipAddress = $req->getServer("HTTP_CLIENT_IP");
65 2
            } elseif (!empty($req->getServer("HTTP_X_FORWARDED_FOR"))) {
66 1
                $ipAddress = $req->getServer("HTTP_X_FORWARDED_FOR");
67
            } else {
68 1
                $ipAddress = $req->getServer("REMOTE_ADDR");
69
            }
70 3
            $ipGeoInfo = $this->ipGeo->getLocation($ipAddress);
71
        }
72
73
74
        $json = [
75 5
            "geoInfo" => $ipGeoInfo,
76 5
            "ip" => $ipAddress,
77
        ];
78 5
        return [$json];
79
    }
80
}
81