1 | <?php |
||
2 | |||
3 | namespace Faxity\Weather; |
||
4 | |||
5 | use Anax\Commons\ContainerInjectableInterface; |
||
6 | use Anax\Commons\ContainerInjectableTrait; |
||
7 | |||
8 | /** |
||
9 | * Controller for the /ip routes |
||
10 | */ |
||
11 | class Controller implements ContainerInjectableInterface |
||
12 | { |
||
13 | use ContainerInjectableTrait; |
||
14 | |||
15 | /** API response example for a failed response */ |
||
16 | private const API_EXAMPLE_ERR = [ |
||
17 | "status" => 400, |
||
18 | "message" => "Positionen är inte en koordinat eller en ip-address", |
||
19 | ]; |
||
20 | |||
21 | /** API response example for a successfull response */ |
||
22 | private const API_EXAMPLE_OK = [ |
||
23 | "coords" => [ 56.1747726, 15.5694792 ], |
||
24 | "data" => [ |
||
25 | [ |
||
26 | "date" => "01 Dec, 2019", |
||
27 | "summary" => "Möjligtvis lätta regnskurar över natten.", |
||
28 | "icon" => "partly-cloudy-day", |
||
29 | "minTemp" => "-7°C", |
||
30 | "maxTemp" => "4°C", |
||
31 | ], |
||
32 | [ |
||
33 | "date" => "02 Dec, 2019", |
||
34 | "summary" => "Regnskurar på morgonen.", |
||
35 | "icon" => "rain", |
||
36 | "minTemp" => "-1°C", |
||
37 | "maxTemp" => "4°C", |
||
38 | ], |
||
39 | [ |
||
40 | "date" => "03 Dec, 2019", |
||
41 | "summary" => "Regnskurar under eftermiddagen och kvällen.", |
||
42 | "icon" => "rain", |
||
43 | "minTemp" => "-2°C", |
||
44 | "maxTemp" => "8°C", |
||
45 | ], |
||
46 | [ |
||
47 | "date" => "04 Dec, 2019", |
||
48 | "summary" => "Mulet under dagen.", |
||
49 | "icon" => "cloudy", |
||
50 | "minTemp" => "5°C", |
||
51 | "maxTemp" => "7°C", |
||
52 | ], |
||
53 | [ |
||
54 | "date" => "05 Dec, 2019", |
||
55 | "summary" => "Möjligtvis lite duggregn över natten.", |
||
56 | "icon" => "cloudy", |
||
57 | "minTemp" => "6°C", |
||
58 | "maxTemp" => "7°C", |
||
59 | ], |
||
60 | [ |
||
61 | "date" => "06 Dec, 2019", |
||
62 | "summary" => "Regnskurar under dagen.", |
||
63 | "icon" => "rain", |
||
64 | "minTemp" => "7°C", |
||
65 | "maxTemp" => "9°C", |
||
66 | ], |
||
67 | [ |
||
68 | "date" => "07 Dec, 2019", |
||
69 | "summary" => "Möjligtvis lite duggregn och hård vind på morgonen.", |
||
70 | "icon" => "rain", |
||
71 | "minTemp" => "4°C", |
||
72 | "maxTemp" => "9°C", |
||
73 | ], |
||
74 | [ |
||
75 | "date" => "08 Dec, 2019", |
||
76 | "summary" => "Regnskurar fram till kvällen.", |
||
77 | "icon" => "rain", |
||
78 | "minTemp" => "3°C", |
||
79 | "maxTemp" => "7°C", |
||
80 | ], |
||
81 | ], |
||
82 | ]; |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Creates a border box for OpenStreetMap |
||
87 | * @param array $coords Coordinates [ $lat, $long ] |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | 1 | private function createMapBorderBox(array $coords) : string |
|
92 | { |
||
93 | 1 | list($lat, $long) = $coords; |
|
94 | 1 | $offset = 0.02; |
|
95 | |||
96 | $bbox = [ |
||
97 | 1 | $long - $offset, |
|
98 | 1 | $lat - $offset, |
|
99 | 1 | $long + $offset, |
|
100 | 1 | $lat + $offset, |
|
101 | ]; |
||
102 | 1 | return implode(",", $bbox); |
|
103 | } |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Handles / for the controller |
||
108 | * |
||
109 | * @return object |
||
110 | */ |
||
111 | 2 | public function indexActionGet() : object |
|
112 | { |
||
113 | // Deal with the action and return a response. |
||
114 | 2 | $location = $this->di->request->getGet("location"); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
115 | 2 | $pastMonth = $this->di->request->getGet("past-month") !== null; |
|
116 | |||
117 | try { |
||
118 | 2 | if (!is_null($location)) { |
|
119 | 2 | $res = $this->di->weather->forecast($location, $pastMonth); |
|
0 ignored issues
–
show
|
|||
120 | } |
||
121 | 1 | } catch (\Exception $ex) { |
|
122 | 1 | $this->di->flash->err($ex->getMessage()); |
|
0 ignored issues
–
show
|
|||
123 | } |
||
124 | |||
125 | // Set Data needed in the render. |
||
126 | 2 | $this->di->page->add("faxity/weather/index", [ |
|
0 ignored issues
–
show
|
|||
127 | 2 | "res" => $res ?? null, |
|
128 | 2 | "apiUrl" => $this->di->url->create("weather-api"), |
|
0 ignored issues
–
show
|
|||
129 | 2 | "location" => $location, |
|
130 | 2 | "bbox" => isset($res) ? $this->createMapBorderBox($res->coords) : "", |
|
131 | 2 | "coords" => isset($res) ? implode(",", $res->coords) : "", |
|
132 | ]); |
||
133 | |||
134 | 2 | return $this->di->page->render([ |
|
135 | 2 | "title" => "Väderkollen", |
|
136 | ]); |
||
137 | } |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Handles /docs for the controller |
||
142 | * |
||
143 | * @return object |
||
144 | */ |
||
145 | 1 | public function docsActionGet() : object |
|
146 | { |
||
147 | // Set data needed in the render. |
||
148 | 1 | $this->di->page->add("faxity/weather/docs", [ |
|
0 ignored issues
–
show
|
|||
149 | 1 | "apiUrl" => $this->di->url->create("weather-api"), |
|
0 ignored issues
–
show
|
|||
150 | "examples" => (object) [ |
||
151 | 1 | "ok" => json_encode(self::API_EXAMPLE_OK, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), |
|
152 | 1 | "err" => json_encode(self::API_EXAMPLE_ERR, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), |
|
153 | ], |
||
154 | ]); |
||
155 | |||
156 | 1 | return $this->di->page->render([ |
|
157 | 1 | "title" => "Väderkollen API", |
|
158 | ]); |
||
159 | } |
||
160 | } |
||
161 |