WeatherJSONController::getPosData()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 86
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 5.0021

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 62
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 86
ccs 43
cts 45
cp 0.9556
crap 5.0021
rs 8.5178

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Lioo19\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
use Lioo19\Models\GeoMap;
9
use Lioo19\Models\Weather;
10
11
// use Anax\Route\Exception\ForbiddenException;
12
// use Anax\Route\Exception\NotFoundException;
13
// use Anax\Route\Exception\InternalErrorException;
14
15
/**
16
 *
17
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
18
 */
19
class WeatherJSONController implements ContainerInjectableInterface
20
{
21
    use ContainerInjectableTrait;
22
23
    /**
24
     * This is the index method action, it handles:
25
     * ANY METHOD mountpoint
26
     * ANY METHOD mountpoint/
27
     * ANY METHOD mountpoint/index
28
     *
29
     * @return string
30
     */
31 6
    public function indexActionGet() : array
32
    {
33 6
        $request = $this->di->get("request");
34
        //request to get GET-info
35 6
        $userip = $request->getGet("ip", null);
36 6
        $userlon = $request->getGet("lon", null);
37 6
        $userlat = $request->getGet("lat", null);
38
39 6
        if ($userip) {
40 2
            $data = $this->getIpData($userip);
41 4
        } elseif ($userlon && $userlat) {
42 2
            $data = $this->getPosData($userlon, $userlat);
43
        } else {
44
            $data = [
45 2
                "message" => "Inkorrekt värde angivet"
46
            ];
47
        }
48 6
        return [$data];
49
    }
50
51
    public function getGeo($userip)
52
    {
53
        $geo = $this->di->get("ipgeo");
54
        $geo->setInput($userip);
55
        $geoInfo = $geo->fetchGeo();
56
57
        return $geoInfo;
58
    }
59
60
    public function getWeather($which, $userlon, $userlat)
61
    {
62
        if ($which == "forecast") {
63
            $weather = new Weather();
64
            $forweather = $weather->fetchForecastWeather($userlon, $userlat);
65
            return $forweather;
66
        } else {
67
            $weather = new Weather();
68
            $histweather = $weather->fetchHistoricalWeather($userlon, $userlat);
69
            return $histweather;
70
        }
71
    }
72
73 2
    private function getIPData($userip)
74
    {
75 2
        $validation = $this->di->get("iptest");
76 2
        $validation->setInput($userip);
77
78 2
        $ip4 = $validation->ip4test();
79 2
        $ip6 = $validation->ip6test();
80
81 2
        if ($ip6 || $ip4) {
82 1
            $hostname = gethostbyaddr($userip);
83 1
            $geoInfo = $this->getGeo($userip);
84 1
            $lon = $geoInfo["longitude"];
85 1
            $lat = $geoInfo["latitude"];
86 1
            $forweather = $this->getWeather("forecast", $lon, $lat);
87 1
            $histweather = $this->getWeather("historical", $lon, $lat);
88
        } else {
89
            $data = [
90 1
                "message" => "Inkorrekt IP, prova igen",
91
            ];
92
93 1
            return $data;
94
        }
95
96
        $data = [
97 1
            "ip" => $userip,
98 1
            "hostname" => $hostname,
99 1
            "geoInfo" => $geoInfo,
100
            "weathertoday" => [
101 1
                "description" => $forweather["current"]["weather"][0]["description"],
102 1
                "temp" => number_format($forweather["current"]["temp"], 2),
103 1
                "feels_like" => number_format($forweather["current"]["feels_like"], 2)
104
                ],
105
            "forecast" => [
106
                "tomorrow" => [
107 1
                    "description" => $forweather["daily"][0]["weather"][0]["description"],
108 1
                    "temp" => number_format($forweather["daily"][0]["temp"]["day"], 2),
109 1
                    "feels_like" => number_format($forweather["daily"][0]["feels_like"]["day"], 2)
110
                ],
111
                "in_2_days" => [
112 1
                    "description" => $forweather["daily"][1]["weather"][0]["description"],
113 1
                    "temp" => number_format($forweather["daily"][1]["temp"]["day"], 2),
114 1
                    "feels_like" => number_format($forweather["daily"][1]["feels_like"]["day"], 2)
115
                ],
116
                "in_3_days" => [
117 1
                    "description" => $forweather["daily"][2]["weather"][0]["description"],
118 1
                    "temp" => number_format($forweather["daily"][2]["temp"]["day"], 2),
119 1
                    "feels_like" => number_format($forweather["daily"][2]["feels_like"]["day"], 2)
120
                ],
121
                "in_4_days" => [
122 1
                    "description" => $forweather["daily"][3]["weather"][0]["description"],
123 1
                    "temp" => number_format($forweather["daily"][3]["temp"]["day"], 2),
124 1
                    "feels_like" => number_format($forweather["daily"][3]["feels_like"]["day"], 2)
125
                ],
126
                "in_5_days" => [
127 1
                    "description" => $forweather["daily"][4]["weather"][0]["description"],
128 1
                    "temp" => number_format($forweather["daily"][4]["temp"]["day"], 2),
129 1
                    "feels_like" => number_format($forweather["daily"][4]["feels_like"]["day"], 2)
130
                ],
131
            ],
132
            "histweather" => [
133
                "yesterday" => [
134 1
                    "description" => $histweather[0]["weather"][0]["description"],
135 1
                    "temp" => number_format($histweather[0]["temp"], 2),
136 1
                    "feels_like" => number_format($histweather[0]["feels_like"], 2)
137
                ],
138
                "two_days_ago" => [
139 1
                    "description" => $histweather[1]["weather"][0]["description"],
140 1
                    "temp" => number_format($histweather[1]["temp"], 2),
141 1
                    "feels_like" => number_format($histweather[1]["feels_like"], 2)
142
                ],
143
                "three_days_ago" => [
144 1
                    "description" => $histweather[2]["weather"][0]["description"],
145 1
                    "temp" => number_format($histweather[2]["temp"], 2),
146 1
                    "feels_like" => number_format($histweather[2]["feels_like"], 2)
147
                ],
148
                "four_days_ago" => [
149 1
                    "description" => $histweather[3]["weather"][0]["description"],
150 1
                    "temp" => number_format($histweather[3]["temp"], 2),
151 1
                    "feels_like" => number_format($histweather[3]["feels_like"], 2)
152
                ],
153
                "five_days_ago" => [
154 1
                    "description" => $histweather[4]["weather"][0]["description"],
155 1
                    "temp" => number_format($histweather[4]["temp"], 2),
156 1
                    "feels_like" => number_format($histweather[4]["feels_like"], 2)
157
                ]
158
            ]
159
        ];
160
161 1
        return $data;
162
    }
163
164 2
    private function getPosData($userlon, $userlat)
165
    {
166
        //check that lon/lat are valid floats
167 2
        if (floatval($userlon) != 0 && floatval($userlat) != 0) {
168 1
            $forweather = $this->getWeather("forecast", $userlon, $userlat);
169 1
            $histweather = $this->getWeather("historical", $userlon, $userlat);
170 1
            if (is_array($forweather) && array_key_exists("current", $forweather)) {
171
                $data = [
172 1
                    "lon" => $userlon,
173 1
                    "lat" => $userlat,
174
                    // "whole" => $forweather,
175
                    "weathertoday" => [
176 1
                        "description" => $forweather["current"]["weather"][0]["description"],
177 1
                        "temp" => number_format($forweather["current"]["temp"], 2),
178 1
                        "feels_like" => number_format($forweather["current"]["feels_like"], 2)
179
                        ],
180
                    "forecast" => [
181
                        "tomorrow" => [
182 1
                            "description" => $forweather["daily"][0]["weather"][0]["description"],
183 1
                            "temp" => number_format($forweather["daily"][0]["temp"]["day"], 2),
184 1
                            "feels_like" => number_format($forweather["daily"][0]["feels_like"]["day"], 2)
185
                        ],
186
                        "in_2_days" => [
187 1
                            "description" => $forweather["daily"][1]["weather"][0]["description"],
188 1
                            "temp" => number_format($forweather["daily"][1]["temp"]["day"], 2),
189 1
                            "feels_like" => number_format($forweather["daily"][1]["feels_like"]["day"], 2)
190
                        ],
191
                        "in_3_days" => [
192 1
                            "description" => $forweather["daily"][2]["weather"][0]["description"],
193 1
                            "temp" => number_format($forweather["daily"][2]["temp"]["day"], 2),
194 1
                            "feels_like" => number_format($forweather["daily"][2]["feels_like"]["day"], 2)
195
                        ],
196
                        "in_4_days" => [
197 1
                            "description" => $forweather["daily"][3]["weather"][0]["description"],
198 1
                            "temp" => number_format($forweather["daily"][3]["temp"]["day"], 2),
199 1
                            "feels_like" => number_format($forweather["daily"][3]["feels_like"]["day"], 2)
200
                        ],
201
                        "in_5_days" => [
202 1
                            "description" => $forweather["daily"][4]["weather"][0]["description"],
203 1
                            "temp" => number_format($forweather["daily"][4]["temp"]["day"], 2),
204 1
                            "feels_like" => number_format($forweather["daily"][4]["feels_like"]["day"], 2)
205
                        ],
206
                    ],
207
                    "histweather" => [
208
                        "yesterday" => [
209 1
                            "description" => $histweather[0]["weather"][0]["description"],
210 1
                            "temp" => number_format($histweather[0]["temp"], 2),
211 1
                            "feels_like" => number_format($histweather[0]["feels_like"], 2)
212
                        ],
213
                        "two_days_ago" => [
214 1
                            "description" => $histweather[1]["weather"][0]["description"],
215 1
                            "temp" => number_format($histweather[1]["temp"], 2),
216 1
                            "feels_like" => number_format($histweather[1]["feels_like"], 2)
217
                        ],
218
                        "three_days_ago" => [
219 1
                            "description" => $histweather[2]["weather"][0]["description"],
220 1
                            "temp" => number_format($histweather[2]["temp"], 2),
221 1
                            "feels_like" => number_format($histweather[2]["feels_like"], 2)
222
                        ],
223
                        "four_days_ago" => [
224 1
                            "description" => $histweather[3]["weather"][0]["description"],
225 1
                            "temp" => number_format($histweather[3]["temp"], 2),
226 1
                            "feels_like" => number_format($histweather[3]["feels_like"], 2)
227
                        ],
228
                        "five_days_ago" => [
229 1
                            "description" => $histweather[4]["weather"][0]["description"],
230 1
                            "temp" => number_format($histweather[4]["temp"], 2),
231 1
                            "feels_like" => number_format($histweather[4]["feels_like"], 2)
232
                        ]
233
                    ]
234
                ];
235
236 1
                return $data;
237
            } else {
238
                $data = [
239
                    "message" => "Inkorrekt position, prova igen",
240
                ];
241
242
                return $data;
243
            }
244
        } else {
245
            $data = [
246 1
                "message" => "Inkorrekt position, prova igen",
247
            ];
248
249 1
            return $data;
250
        }
251
    }
252
}
253