IpGeoController::indexActionPost()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
nc 2
nop 0
dl 0
loc 34
c 1
b 0
f 0
cc 2
rs 9.536
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 IpGeoController 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
    public function initialize() : void
42
    {
43
        // Use to initialise member variables.
44
        $this->db = "active";
45
        $this->IpCheck = new IpCheck();
0 ignored issues
show
Bug Best Practice introduced by
The property IpCheck does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
        $this->IpCurl = new IpCurl();
0 ignored issues
show
Bug Best Practice introduced by
The property IpCurl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
    }
48
49
50
51
    /**
52
    * This is the index method action, it handles:
53
    * ANY METHOD mountpoint
54
    * ANY METHOD mountpoint/
55
    * ANY METHOD mountpoint/index
56
    *
57
    * @return string
58
    */
59
    public function indexAction() : object
60
    {
61
        $title = "Ip Validator";
62
63
        $page = $this->di->get("page");
64
65
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
66
            $ip = $_SERVER['HTTP_CLIENT_IP'];
67
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
68
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
69
        } else {
70
            if (isset($_SERVER['REMOTE_ADDR'])) {
71
                $ip = $_SERVER['REMOTE_ADDR'];
72
            } else {
73
                $ip = "8.8.8.8";
74
            }
75
        }
76
77
        $page->add("anax/ipvalidatorgeo/index", [
78
            "ip" => $ip,
79
        ]);
80
81
        return $page->render([
82
            "title" => $title,
83
        ]);
84
    }
85
86
87
    /**
88
    * Update current selected style.
89
    *
90
    * @return object
91
    */
92
    public function indexActionPost() : object
93
    {
94
        $type = "null";
95
        $ort = "null";
96
        $country = "null";
97
        $domain = "Not found";
0 ignored issues
show
Unused Code introduced by
The assignment to $domain is dead and can be removed.
Loading history...
98
        $request = $this->di->get("request");
99
100
        $ip = $request->getPost("ip");
101
102
        if ($this->IpCheck->validateIp($ip)) {
103
            $api_result = $this->IpCurl->curl($ip);
104
            $type = $api_result['type'];
105
            $ort = $api_result['region_name'];
106
            $country = $api_result['country_name'];
107
            $domain = $this->IpCheck->validateDomain($ip);
108
            $res = "`$ip` is a valid IP Adress. Domainname: $domain";
109
        } else {
110
            $res = "`$ip` is not a valid IP Adress. Domainname: Not found";
111
        }
112
113
        $title = "Ip Validator";
114
115
        $page = $this->di->get("page");
116
117
        $page->add("anax/ipvalidatorgeo/result", [
118
            "res" => $res,
119
            "type" => $type,
120
            "ort" => $ort,
121
            "country" => $country,
122
        ]);
123
124
        return $page->render([
125
            "title" => $title,
126
        ]);
127
    }
128
}
129