WeatherController::getGeo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Lioo19\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Lioo19\Models\GeoMap;
8
use Lioo19\Models\Weather;
9
10
// use Anax\Route\Exception\ForbiddenException;
11
// use Anax\Route\Exception\NotFoundException;
12
// use Anax\Route\Exception\InternalErrorException;
13
14
/**
15
 *
16
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
17
 */
18
class WeatherController implements ContainerInjectableInterface
19
{
20
    use ContainerInjectableTrait;
21
22
    /**
23
     * This is the index method action, it handles:
24
     * ANY METHOD mountpoint
25
     * ANY METHOD mountpoint/
26
     * ANY METHOD mountpoint/index
27
     *
28
     * @return string
29
     */
30 1
    public function indexAction() : object
31
    {
32 1
        $page = $this->di->get("page");
33 1
        $request = $this->di->get("request");
34 1
        $ipDefault = $this->di->get("ipdefault");
35 1
        $usersIp = $ipDefault->getDefaultIp($request);
36
37
        $data = [
38 1
            "defaultIp" => $usersIp,
39
        ];
40
41
        //MAPPEN inte url
42 1
        $page->add("weather/weather", $data);
43
44 1
        return $page->render([
45 1
            "title" => __METHOD__,
46
        ]);
47
    }
48
49
    /**
50
     * Post for redirecting to final page of Weather, picking up given values
51
     * Sends the ip-adress with post and redirects
52
     *
53
     * @return object
54
     */
55 3
    public function validationActionPost() : object
56
    {
57 3
        $request = $this->di->get("request");
58 3
        $page = $this->di->get("page");
59 3
        $title = "Vädret";
60
        //request to get the posted information
61 3
        $userip = $request->getPost("ipinput", null);
62 3
        $userlon = $request->getPost("lon", null);
63 3
        $userlat = $request->getPost("lat", null);
64
65 3
        if ($userip) {
66 1
            $data = $this->getIpData($userip);
67
68 1
            if (count($data) < 2) {
69 1
                $page->add("weather/validationfail", $data);
70
            } else {
71
                //data for map
72
                $data2 = [
73 1
                    "lon" => $data["geoInfo"]["longitude"],
74 1
                    "lat" => $data["geoInfo"]["latitude"],
75
                ];
76
77 1
                $page->add("weather/validationip", $data);
78 1
                $page->add("weather/map", $data2);
79
            }
80 2
        } elseif ($userlon && $userlat) {
81 1
            $data = $this->getPosData($userlon, $userlat);
82
83 1
            if (count($data) < 2) {
84 1
                $page->add("weather/validationfail", $data);
85
            } else {
86
                $data2 = [
87 1
                    "lon" => $userlon,
88 1
                    "lat" => $userlat,
89
                ];
90
91 1
                $page->add("weather/validationpos", $data);
92 1
                $page->add("weather/map", $data2);
93
            }
94
        } else {
95
            $datafail = [
96 1
                "message" => "Gå tillbaka och försök igen"
97
                ];
98 1
            $page->add("weather/validationfail", $datafail);
99
        }
100 3
        return $page->render([
101 3
            "title" => $title,
102
        ]);
103
    }
104
105 1
    private function getIPData($userip)
106
    {
107 1
        $validation = $this->di->get("iptest");
108 1
        $validation->setInput($userip);
109
110 1
        $ip4 = $validation->ip4test();
111 1
        $ip6 = $validation->ip6test();
112
113 1
        if ($ip6 || $ip4) {
114 1
            $hostname = gethostbyaddr($userip);
115 1
            $geoInfo = $this->getGeo($userip);
116 1
            $lon = $geoInfo["longitude"];
117 1
            $lat = $geoInfo["latitude"];
118 1
            $map = new GeoMap($lon, $lat);
0 ignored issues
show
Unused Code introduced by
The call to Lioo19\Models\GeoMap::__construct() has too many arguments starting with $lon. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
            $map = /** @scrutinizer ignore-call */ new GeoMap($lon, $lat);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
119 1
            $forweather = $this->getWeather("forecast", $lon, $lat);
120 1
            $histweather = $this->getWeather("historical", $lon, $lat);
121
        } else {
122
            $data = [
123 1
                "message" => "Inkorrekt IP, försök igen",
124
            ];
125
126 1
            return $data;
127
        }
128
129
        $data = [
130 1
            "ip" => $userip,
131 1
            "hostname" => $hostname,
132 1
            "geoInfo" => $geoInfo,
133 1
            "map" => $map,
134 1
            "forweather" => $forweather,
135 1
            "histweather" => $histweather,
136
        ];
137
138 1
        return $data;
139
    }
140
141 1
    private function getPosData($userlon, $userlat)
142
    {
143
        //check that lon/lat are valid floats
144 1
        if (floatval($userlon) != 0 && floatval($userlat) != 0) {
145 1
            $map = new GeoMap($userlon, $userlat);
0 ignored issues
show
Unused Code introduced by
The call to Lioo19\Models\GeoMap::__construct() has too many arguments starting with $userlon. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
            $map = /** @scrutinizer ignore-call */ new GeoMap($userlon, $userlat);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
146 1
            $forweather = $this->getWeather("forecast", $userlon, $userlat);
147 1
            $histweather = $this->getWeather("historical", $userlon, $userlat);
148 1
            if (is_array($forweather)) {
149
                $data = [
150 1
                    "map" => $map,
151 1
                    "forweather" => $forweather,
152 1
                    "histweather" => $histweather,
153 1
                    "lon" => $userlon,
154 1
                    "lat" => $userlat
155
                ];
156
157 1
                return $data;
158
            }
159
        }
160
        $data = [
161 1
            "message" => "Inkorrekt position, försök igen",
162
        ];
163
164 1
        return $data;
165
    }
166
167
    public function getGeo($userip)
168
    {
169
        $geo = $this->di->get("ipgeo");
170
        $geo->setInput($userip);
171
        $geoInfo = $geo->fetchGeo();
172
173
        return $geoInfo;
174
    }
175
176
    public function getWeather($which, $userlon, $userlat)
177
    {
178
        if ($which == "forecast") {
179
            $weather = new Weather();
180
            $forweather = $weather->fetchForecastWeather($userlon, $userlat);
181
            return $forweather;
182
        } else {
183
            $weather = new Weather();
184
            $histweather = $weather->fetchHistoricalWeather($userlon, $userlat);
185
            return $histweather;
186
        }
187
    }
188
}
189