Passed
Push — main ( cc23cb...7f5ce5 )
by Aron
02:22
created

IPAPIController::makeJSON()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 2
rs 10
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
        return $this->makeJSON();
63
    }
64
65
    /**
66
     * This is the index method action, it handles:
67
     * ANY METHOD mountpoint
68
     * ANY METHOD mountpoint/
69
     * ANY METHOD mountpoint/index
70
     *
71
     * @return array
72
     */
73 1
    public function checkActionPost() : array
74
    {
75 1
        return $this->makeJSON();
76
    }
77
78 2
    public function makeJSON()
79
    {
80 2
        $request = $this->di->get("request");
81 2
        $ip  = $request->getGet("ip", "");
82 2
        if (!$ip) {
83 1
            $ip  = $request->getPost("ip", "");
84
        }
85 2
        $firstJSON = $this->generateJSON1($ip);
86 2
        $myjson = $this->generateJSON2($firstJSON, $ip);
87 2
        return [json_encode($myjson, JSON_UNESCAPED_UNICODE)];
88
    }
89
90 2
    public function generateJSON1($ip)
91
    {
92 2
        $userip = new IPCheck($ip);
93 2
        $ip4 = $userip->ipv4();
94 2
        $ip6 = $userip->ipv6();
95 2
        $userinput = $userip->getUserInput();
96 2
        $corrected = $userip->getCorrectedInput();
97 2
        $domain = $userip->getDomainName();
98 2
        $ipmsg = $userip->printIPMessage();
99 2
        $domainmsg = $userip->printDomainMessage();
100
        $firstJSON = [
101 2
            "ip4" => $ip4,
102 2
            "ip6" => $ip6,
103 2
            "userinput" => $userinput,
104 2
            "corrected" => $corrected,
105 2
            "domain" => $domain,
106 2
            "ipmsg" => $ipmsg,
107 2
            "domainmsg" => $domainmsg,
108
        ];
109 2
        return $firstJSON;
110
    }
111
112 2
    public function generateJSON2($myjson, $ip)
113
    {
114 2
        $ipkey = "";
115
        // this loads $ipkey and $weatherkey
116 2
        include(ANAX_INSTALL_PATH . '/config/api/apikeys.php');
117 2
        $geoip = new IPGeotag($ipkey);
118 2
        $myjson["continent"] = $geoip->parseJson($ip, "continent_name");
119 2
        $myjson["country"] = $geoip->parseJson($ip, "country_name");
120 2
        $myjson["city"] = $geoip->parseJson($ip, "city");
121 2
        $myjson["zip"] = $geoip->parseJson($ip, "zip");
122 2
        $myjson["language"] = $geoip->parseJson($ip, "location", "languages", "name");
123 2
        $myjson["latitude"] = $geoip->parseJson($ip, "latitude");
124 2
        $myjson["longitude"] = $geoip->parseJson($ip, "longitude");
125 2
        $myjson["map"] = $geoip->getmap($ip);
126
127 2
        return $myjson;
128
    }
129
}
130