Passed
Push — main ( 1f692e...3ae92f )
by Aron
02:58
created

WeatherAPIController::indexActionGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 1
rs 9.9332
c 0
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
        $ip = $this->di->get("ip");
65 1
        $request = $this->di->get("request");
66 1
        $userip  = $request->getGet("ip", "");
67 1
        $lon  = $request->getGet("lon", "");
68 1
        $lat  = $request->getGet("lat", "");
69 1
        $type  = $request->getGet("type", "");
70 1
        $validator = new ValidAPIWeather($request, $ip);
71 1
        if ($validator->errormsg()) {
72
            $myjson = [
73 1
                "msg" => $validator->errormsg()
74
            ];
75 1
            return [json_encode($myjson, JSON_UNESCAPED_UNICODE)];
76
        }
77 1
        $data = $this->generateData($userip, $lat, $lon, $type);
78
79
        // $ipkey = "";
80
        // $weatherkey = "";
81
        // // this loads $ipkey and $weatherkey
82
        // include(ANAX_INSTALL_PATH . '/config/api/apikeys.php');
83
        // $geotag = new IPGeotag($ipkey);
84
        // if ($userip) {
85
        //     $geoinfo = $geotag->checkdefaultip($userip);
86
        //     $lat = $geoinfo["latitude"] ?? "";
87
        //     $lon = $geoinfo["longitude"] ?? "";
88
        // }
89
        // $data = $this->getWeather($weatherkey, $lat, $lon, $type);
90
91 1
        if (!($lat && $lon)) {
92
            $msg = [
93 1
                "msg" => "No geodata could be detected."
94
            ];
95 1
            return [json_encode($msg, JSON_UNESCAPED_UNICODE)];
96
        }
97
        return [json_encode($data, JSON_UNESCAPED_UNICODE)];
98
    }
99
100 2
    private function generateData($userip, $lat, $lon, $type)
101
    {
102 2
        $ipkey = "";
103 2
        $weatherkey = "";
104
        // this loads $ipkey and $weatherkey
105 2
        include(ANAX_INSTALL_PATH . '/config/api/apikeys.php');
106 2
        $geotag = new IPGeotag($ipkey);
107 2
        if ($userip) {
108 2
            $geoinfo = $geotag->checkdefaultip($userip);
109 2
            $lat = $geoinfo["latitude"] ?? "";
110 2
            $lon = $geoinfo["longitude"] ?? "";
111
        }
112 2
        $data = $this->getWeather($weatherkey, $lat, $lon, $type);
113 2
        return $data;
114
    }
115
116
    /**
117
     * This is the index method action, it handles:
118
     * ANY METHOD mountpoint
119
     * ANY METHOD mountpoint/
120
     * ANY METHOD mountpoint/index
121
     *
122
     * @return array
123
     */
124 1
    public function infoActionPost() : array
125
    {
126 1
        $ip = $this->di->get("ip");
127 1
        $request = $this->di->get("request");
128 1
        $userip  = $request->getPost("userip", "");
129 1
        $lon  = $request->getPost("longitud", "");
130 1
        $lat  = $request->getPost("latitud", "");
131 1
        $type  = $request->getPost("type", "");
132
133 1
        $validator = new ValidAPIWeather($request, $ip);
134 1
        if ($validator->errormsg()) {
135
            $myjson = [
136 1
                "msg" => $validator->errormsg()
137
            ];
138 1
            return [json_encode($myjson, JSON_UNESCAPED_UNICODE)];
139
        }
140 1
        $data = $this->generateData($userip, $lat, $lon, $type);
141
142
        // $ipkey = "";
143
        // $weatherkey = "";
144
        // // this loads $ipkey and $weatherkey
145
        // include(ANAX_INSTALL_PATH . '/config/api/apikeys.php');
146
        // $geotag = new IPGeotag($ipkey);
147
        // if ($userip) {
148
        //     $geoinfo = $geotag->checkdefaultip($userip);
149
        //     $lat = $geoinfo["latitude"] ?? "";
150
        //     $lon = $geoinfo["longitude"] ?? "";
151
        // }
152
        // $data = $this->getWeather($weatherkey, $lat, $lon, $type);
153 1
        if (!($lat && $lon)) {
154
            $msg = [
155 1
                "msg" => "No geodata could be detected."
156
            ];
157 1
            return [json_encode($msg, JSON_UNESCAPED_UNICODE)];
158
        }
159
        return [json_encode($data, JSON_UNESCAPED_UNICODE)];
160
    }
161
162
    /**
163
     * This is the index method action, it handles:
164
     * ANY METHOD mountpoint
165
     * ANY METHOD mountpoint/
166
     * ANY METHOD mountpoint/index
167
     *
168
     * @return array
169
     */
170 3
    public function getWeather($weatherkey, $lat, $lon, $type) : array
171
    {
172 3
        $openweather = new OpenWeather($weatherkey, $lat, $lon);
173 3
        if ($type === "historical") {
174 3
            $data = $openweather->historicweather();
175 3
        } elseif ($type === "forecast") {
176 3
            $data = $openweather->forecast();
177
        } else {
178 3
            $data = $openweather->currentweather();
179
        }
180 3
        return $data;
181
    }
182
}
183