1 | <?php |
||||
2 | |||||
3 | namespace Anax\IpController; |
||||
4 | |||||
5 | use Anax\Models; |
||||
6 | use Anax\Commons\ContainerInjectableInterface; |
||||
7 | use Anax\Commons\ContainerInjectableTrait; |
||||
8 | |||||
9 | /** |
||||
10 | * |
||||
11 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||||
12 | */ |
||||
13 | class GeoWeatherController implements ContainerInjectableInterface |
||||
14 | { |
||||
15 | use ContainerInjectableTrait; |
||||
16 | |||||
17 | private $darksky; |
||||
18 | private $ipstack; |
||||
19 | |||||
20 | 10 | public function initialize() |
|||
21 | { |
||||
22 | 10 | $this->darksky = $this->di->get('apiDarkSky'); |
|||
23 | 10 | $this->ipstack = $this->di->get('apiIpStack'); |
|||
24 | 10 | } |
|||
25 | |||||
26 | 1 | public function indexActionGet() |
|||
27 | { |
||||
28 | 1 | $page = $this->di->get("page"); |
|||
29 | 1 | $request = $this->di->get("request"); |
|||
30 | |||||
31 | 1 | $title = "Väderprognos"; |
|||
32 | |||||
33 | $json = [ |
||||
34 | 1 | "ip" => $request->getServer("REMOTE_ADDR", ""), |
|||
35 | 1 | "status" => "", |
|||
36 | ]; |
||||
37 | |||||
38 | 1 | $page->add("weather/showWeather", [ |
|||
39 | 1 | "weatherJson" => $json, |
|||
40 | "dataExists" => false, |
||||
41 | 1 | "status" => "Ingen data tillgänglig", |
|||
42 | ]); |
||||
43 | |||||
44 | 1 | return $page->render([ |
|||
45 | 1 | "title" => $title, |
|||
46 | ]); |
||||
47 | } |
||||
48 | |||||
49 | /** |
||||
50 | * |
||||
51 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||||
52 | */ |
||||
53 | 8 | public function indexActionPost() |
|||
54 | { |
||||
55 | 8 | $title = "Väderprognos"; |
|||
56 | 8 | $request = $this->di->get("request"); |
|||
57 | 8 | $page = $this->di->get("page"); |
|||
58 | 8 | $location = $request->getPost("location"); |
|||
59 | 8 | $futureOrPast = $request->getPost("weather"); |
|||
60 | |||||
61 | 8 | $json = $this->ipstack->validate($location); |
|||
62 | |||||
63 | 8 | $lat = ""; |
|||
64 | 8 | $long = ""; |
|||
65 | |||||
66 | 8 | if ($json["valid"] == true) { |
|||
67 | 4 | $lat = $json["ipStackData"]->{"latitude"}; |
|||
68 | 4 | $long = $json["ipStackData"]->{"longitude"}; |
|||
69 | 4 | $weatherJson = $this->darksky->getWeatherGeo($lat, $long, $futureOrPast); |
|||
70 | 4 | $dataExists = true; |
|||
71 | 4 | $status = ""; |
|||
72 | } else { |
||||
73 | 4 | $latLong = explode(",", $location); |
|||
74 | 4 | if (count($latLong) == 2 && is_numeric($latLong[0]) && is_numeric($latLong[1])) { |
|||
75 | 3 | if ($latLong[0] <= 90 && $latLong[0] >= -90 && $latLong[1] >= -180 && $latLong[1] <= 180) { |
|||
76 | 2 | $lat = trim($latLong[0]); |
|||
77 | 2 | $long = trim($latLong[1]); |
|||
78 | 2 | $weatherJson = $this->darksky->getWeatherGeo($lat, $long, $futureOrPast); |
|||
79 | 2 | $dataExists = true; |
|||
80 | 2 | $status = ""; |
|||
81 | } else { |
||||
82 | 1 | $weatherJson = ""; |
|||
83 | 1 | $dataExists = false; |
|||
84 | 3 | $status = "Okänd plats"; |
|||
85 | } |
||||
86 | } else { |
||||
87 | 1 | $weatherJson = ""; |
|||
88 | 1 | $dataExists = false; |
|||
89 | 1 | $status = "Användar error!"; |
|||
90 | } |
||||
91 | } |
||||
92 | |||||
93 | 8 | if ($dataExists == true) { |
|||
0 ignored issues
–
show
|
|||||
94 | 6 | $mapCode = "var latitude = $lat; var longitude = $long;"; |
|||
95 | } else { |
||||
96 | 2 | $mapCode = ""; |
|||
97 | } |
||||
98 | |||||
99 | 8 | $page->add("weather/showWeather", [ |
|||
100 | 8 | "weatherJson" => $weatherJson, |
|||
101 | 8 | "dataExists" => $dataExists, |
|||
102 | 8 | "status" => $status, |
|||
103 | 8 | "mapCode" => $mapCode, |
|||
104 | ]); |
||||
105 | |||||
106 | 8 | return $page->render([ |
|||
107 | 8 | "title" => $title, |
|||
108 | ]); |
||||
109 | } |
||||
110 | |||||
111 | |||||
112 | /** |
||||
113 | * Adding an optional catchAll() method will catch all actions sent to the |
||||
114 | * router. You can then reply with an actual response or return void to |
||||
115 | * allow for the router to move on to next handler. |
||||
116 | * A catchAll() handles the following, if a specific action method is not |
||||
117 | * created: |
||||
118 | * ANY METHOD mountpoint/** |
||||
119 | * |
||||
120 | * @param array $args as a variadic parameter. |
||||
121 | * |
||||
122 | * @return mixed |
||||
123 | * |
||||
124 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||||
125 | */ |
||||
126 | 1 | public function catchAll(...$args) |
|||
0 ignored issues
–
show
The parameter
$args is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
127 | { |
||||
128 | // Deal with the request and send an actual response, or not. |
||||
129 | //return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args); |
||||
130 | 1 | return "404 Not Found"; |
|||
131 | } |
||||
132 | } |
||||
133 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.