Passed
Push — main ( cda00d...f0346b )
by Aron
03:30
created

WeatherAPIController::getWeather()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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