Passed
Push — main ( deb0a6...cc23cb )
by Aron
02:27
created

IPAPIController::generateJSON1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 1
dl 0
loc 20
ccs 17
cts 17
cp 1
crap 1
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\Controller;
4
5
namespace artes\IP;
6
7
// use Anax\Commons\AppInjectableInterface;
8
// use Anax\Commons\AppInjectableTrait;
9
use Anax\Commons\ContainerInjectableInterface;
10
use Anax\Commons\ContainerInjectableTrait;
11
use Anax\Route\Exception\NotFoundException;
12
13
/**
14
 * A sample controller to show how a controller class can be implemented.
15
 * The controller will be injected with $app if implementing the interface
16
 * AppInjectableInterface, 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
 * @SuppressWarnings(PHPMD)
21
 */
22
class IPAPIController implements ContainerInjectableInterface
23
{
24
    use ContainerInjectableTrait;
25
26
    /**
27
     * This is the index method action, it handles:
28
     * ANY METHOD mountpoint
29
     * ANY METHOD mountpoint/
30
     * ANY METHOD mountpoint/index
31
     *
32
     * @return object
33
     */
34 1
    public function indexActionGet() : object
35
    {
36 1
        $page = $this->di->get("page");
37 1
        $realip = new RealIP();
38 1
        $ipaddress = $realip->getRealIpAddr();
39
        $data = [
40 1
            "ip" => $ipaddress
41
        ];
42 1
        $page->add(
43 1
            "ip/ipapi",
44
            $data
45
        );
46
47 1
        return $page->render([
48 1
            "title" => "My IP",
49
        ]);
50
    }
51
52
    /**
53
     * This is the index method action, it handles:
54
     * ANY METHOD mountpoint
55
     * ANY METHOD mountpoint/
56
     * ANY METHOD mountpoint/index
57
     *
58
     * @return array
59
     */
60 1
    public function checkActionGet() : array
61
    {
62 1
        $request = $this->di->get("request");
63 1
        $ip  = $request->getGet("ip", "");
64 1
        $firstJSON = $this->generateJSON1($ip);
65 1
        $myjson = $this->generateJSON2($firstJSON, $ip);
66 1
        return [json_encode($myjson, JSON_UNESCAPED_UNICODE)];
67
    }
68
69 2
    public function generateJSON1($ip)
70
    {
71 2
        $userip = new IPCheck($ip);
72 2
        $ip4 = $userip->ipv4();
73 2
        $ip6 = $userip->ipv6();
74 2
        $userinput = $userip->getUserInput();
75 2
        $corrected = $userip->getCorrectedInput();
76 2
        $domain = $userip->getDomainName();
77 2
        $ipmsg = $userip->printIPMessage();
78 2
        $domainmsg = $userip->printDomainMessage();
79
        $firstJSON = [
80 2
            "ip4" => $ip4,
81 2
            "ip6" => $ip6,
82 2
            "userinput" => $userinput,
83 2
            "corrected" => $corrected,
84 2
            "domain" => $domain,
85 2
            "ipmsg" => $ipmsg,
86 2
            "domainmsg" => $domainmsg,
87
        ];
88 2
        return $firstJSON;
89
    }
90
91 2
    public function generateJSON2($myjson, $ip)
92
    {
93 2
        $ipkey = "";
94
        // this loads $ipkey and $weatherkey
95 2
        include(ANAX_INSTALL_PATH . '/config/api/apikeys.php');
96 2
        $geoip = new IPGeotag($ipkey);
97 2
        $myjson["continent"] = $geoip->parseJson($ip, "continent_name");
98 2
        $myjson["country"] = $geoip->parseJson($ip, "country_name");
99 2
        $myjson["city"] = $geoip->parseJson($ip, "city");
100 2
        $myjson["zip"] = $geoip->parseJson($ip, "zip");
101 2
        $myjson["language"] = $geoip->parseJson($ip, "location", "languages", "name");
102 2
        $myjson["latitude"] = $geoip->parseJson($ip, "latitude");
103 2
        $myjson["longitude"] = $geoip->parseJson($ip, "longitude");
104 2
        $myjson["map"] = $geoip->getmap($ip);
105
106 2
        return $myjson;
107
    }
108
109
    /**
110
     * This is the index method action, it handles:
111
     * ANY METHOD mountpoint
112
     * ANY METHOD mountpoint/
113
     * ANY METHOD mountpoint/index
114
     *
115
     * @return array
116
     */
117 1
    public function checkActionPost() : array
118
    {
119 1
        $request = $this->di->get("request");
120 1
        $ip  = $request->getPost("ip", "");
121 1
        $firstJSON = $this->generateJSON1($ip);
122 1
        $myjson = $this->generateJSON2($firstJSON, $ip);
123 1
        return [json_encode($myjson, JSON_UNESCAPED_UNICODE)];
124
    }
125
}
126