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

WeatherAPIController::makeJSON()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
nc 3
nop 0
dl 0
loc 23
ccs 15
cts 16
cp 0.9375
crap 4.0039
rs 9.7
c 1
b 0
f 0
1
<?php
2
3
namespace Anax\Controller;
4
5
namespace artes\Weather;
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
use artes\IP\IPGeotag;
13
use artes\IP\RealIP;
14
15
/**
16
 * A sample controller to show how a controller class can be implemented.
17
 * The controller will be injected with $app if implementing the interface
18
 * AppInjectableInterface, like this sample class does.
19
 * The controller is mounted on a particular route and can then handle all
20
 * requests for that mount point.
21
 *
22
 * @SuppressWarnings(PHPMD)
23
 */
24
class WeatherAPIController implements ContainerInjectableInterface
25
{
26
    use ContainerInjectableTrait;
27
28
    /**
29
     * This is the index method action, it handles:
30
     * ANY METHOD mountpoint
31
     * ANY METHOD mountpoint/
32
     * ANY METHOD mountpoint/index
33
     *
34
     * @return object
35
     */
36 1
    public function indexActionGet() : object
37
    {
38 1
        $page = $this->di->get("page");
39 1
        $realip = new RealIP();
40 1
        $ipaddress = $realip->getRealIpAddr();
41
        $data = [
42 1
            "ip" => $ipaddress
43
        ];
44 1
        $page->add(
45 1
            "weather/weatherapi",
46
            $data
47
        );
48
49 1
        return $page->render([
50 1
            "title" => "My IP",
51
        ]);
52
    }
53
54
    /**
55
     * This is the index method action, it handles:
56
     * ANY METHOD mountpoint
57
     * ANY METHOD mountpoint/
58
     * ANY METHOD mountpoint/index
59
     *
60
     * @return array
61
     */
62 1
    public function infoActionGet() : array
63
    {
64 1
        return $this->makeJSON();
65
    }
66
67
    /**
68
     * This is the index method action, it handles:
69
     * ANY METHOD mountpoint
70
     * ANY METHOD mountpoint/
71
     * ANY METHOD mountpoint/index
72
     *
73
     * @return array
74
     */
75 1
    public function infoActionPost() : array
76
    {
77 1
        return $this->makeJSON();
78
    }
79
80 2
    public function makeJSON()
81
    {
82 2
        $ip = $this->di->get("ip");
83 2
        $request = $this->di->get("request");
84 2
        $userip  = $request->getGet("ip", "") ?? $request->getPost("userip", "");
85 2
        $lon  = $request->getGet("lon", "") ?? $request->getPost("longitud", "");
86 2
        $lat  = $request->getGet("lat", "") ?? $request->getPost("latitud", "");
87 2
        $type  = $request->getGet("type", "") ?? $request->getPost("type", "");
88 2
        $validator = new ValidAPIWeather($request, $ip);
89 2
        if ($validator->errormsg()) {
90
            $myjson = [
91 2
                "msg" => $validator->errormsg()
92
            ];
93 2
            return [json_encode($myjson, JSON_UNESCAPED_UNICODE)];
94
        }
95 2
        $data = $this->generateData($userip, $lat, $lon, $type);
96 2
        if (!($lat && $lon)) {
97
            $msg = [
98 2
                "msg" => "No geodata could be detected."
99
            ];
100 2
            return [json_encode($msg, JSON_UNESCAPED_UNICODE)];
101
        }
102
        return [json_encode($data, JSON_UNESCAPED_UNICODE)];
103
    }
104
105 2
    private function generateData($userip, $lat, $lon, $type)
106
    {
107 2
        $ipkey = "";
108 2
        $weatherkey = "";
109
        // this loads $ipkey and $weatherkey
110 2
        include(ANAX_INSTALL_PATH . '/config/api/apikeys.php');
111 2
        $geotag = new IPGeotag($ipkey);
112 2
        if ($userip) {
113 1
            $geoinfo = $geotag->checkdefaultip($userip);
114 1
            $lat = $geoinfo["latitude"] ?? "";
115 1
            $lon = $geoinfo["longitude"] ?? "";
116
        }
117 2
        $data = $this->getWeather($weatherkey, $lat, $lon, $type);
118 2
        return $data;
119
    }
120
121
    /**
122
     * This is the index method action, it handles:
123
     * ANY METHOD mountpoint
124
     * ANY METHOD mountpoint/
125
     * ANY METHOD mountpoint/index
126
     *
127
     * @return array
128
     */
129 3
    public function getWeather($weatherkey, $lat, $lon, $type) : array
130
    {
131 3
        $openweather = new OpenWeather($weatherkey, $lat, $lon);
132 3
        if ($type === "historical") {
133 2
            $data = $openweather->historicweather();
134 3
        } elseif ($type === "forecast") {
135 2
            $data = $openweather->forecast();
136
        } else {
137 3
            $data = $openweather->currentweather();
138
        }
139 3
        return $data;
140
    }
141
}
142