|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lefty\Weather; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
|
|
8
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
|
9
|
|
|
// use Anax\Route\Exception\NotFoundException; |
|
10
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A sample controller to show how a controller class can be implemented. |
|
14
|
|
|
* The controller will be injected with $di if implementing the interface |
|
15
|
|
|
* ContainerInjectableInterface, like this sample class does. |
|
16
|
|
|
* The controller is mounted on a particular route and can then handle all |
|
17
|
|
|
* requests for that mount point. |
|
18
|
|
|
* |
|
19
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
20
|
|
|
*/ |
|
21
|
|
|
class WeatherController implements ContainerInjectableInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use ContainerInjectableTrait; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string $db a sample member variable that gets initialised |
|
28
|
|
|
*/ |
|
29
|
|
|
private $db = "not active"; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The initialize method is optional and will always be called before the |
|
35
|
|
|
* target method/action. This is a convienient method where you could |
|
36
|
|
|
* setup internal properties that are commonly used by several methods. |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
8 |
|
public function initialize(): void |
|
41
|
|
|
{ |
|
42
|
|
|
// Use to initialise member variables. |
|
43
|
8 |
|
$this->db = "active"; |
|
44
|
8 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* This is the index method action, it handles: |
|
49
|
|
|
* ANY METHOD mountpoint |
|
50
|
|
|
* ANY METHOD mountpoint/ |
|
51
|
|
|
* ANY METHOD mountpoint/index |
|
52
|
|
|
* |
|
53
|
|
|
* @return object |
|
54
|
|
|
*/ |
|
55
|
4 |
|
public function indexActionGet(): object |
|
56
|
|
|
{ |
|
57
|
4 |
|
$request = $this->di->get("request"); |
|
58
|
4 |
|
$iptocheck = $request->getGet("ip") ?? ""; |
|
59
|
|
|
|
|
60
|
4 |
|
$weatherInfo = []; |
|
|
|
|
|
|
61
|
4 |
|
$weatherInfoHist = []; |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
// Prepare page |
|
65
|
4 |
|
$page = $this->di->get("page"); |
|
66
|
4 |
|
$page->add("weather/index"); |
|
67
|
4 |
|
$title = "Weather | ramverk1"; |
|
68
|
|
|
|
|
69
|
|
|
// Validate IP |
|
70
|
4 |
|
$ipAddress = new WeatherIpValidation($iptocheck); |
|
71
|
4 |
|
$data = $ipAddress->answer(); |
|
72
|
|
|
|
|
73
|
4 |
|
if (empty($iptocheck)) { |
|
74
|
1 |
|
return $page->render([ |
|
75
|
1 |
|
"title" => $title, |
|
76
|
|
|
]); |
|
77
|
3 |
|
} elseif (!$ipAddress->isValid()) { |
|
78
|
1 |
|
$page->add("weather/notvalidIP", $data); |
|
79
|
1 |
|
return $page->render([ |
|
80
|
1 |
|
"title" => $title, |
|
81
|
|
|
]); |
|
82
|
|
|
} |
|
83
|
|
|
// Get IP location |
|
84
|
2 |
|
$geoLocation = new WeatherGeoLocation($iptocheck); |
|
|
|
|
|
|
85
|
2 |
|
$geoLocation->setDI($this->di); |
|
86
|
2 |
|
$geoLocation->setAPI("ipstack"); |
|
87
|
2 |
|
$geoLocation->checkGeoLocation($iptocheck); |
|
88
|
2 |
|
$locationInfo = $geoLocation->getGeoLocation(); |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
// Get Weather information Forecast |
|
92
|
2 |
|
$weatherRequest = new WeatherRequest(); |
|
93
|
2 |
|
$weatherRequest->setDI($this->di); |
|
94
|
2 |
|
$weatherRequest->setAPI("openweathermap"); |
|
95
|
2 |
|
$weatherRequest->checkWeather($geoLocation); |
|
96
|
2 |
|
$weatherInfo = (array)$weatherRequest->getWeather(); |
|
97
|
|
|
|
|
98
|
|
|
// Get Weather information Historical Data |
|
99
|
|
|
|
|
100
|
2 |
|
$weatherInfoHist = array("weatherInfoHistorical" => $weatherRequest->checkWeatherMulti($geoLocation)); |
|
101
|
2 |
|
$weatherInfo = array_merge($weatherInfo, $weatherInfoHist); |
|
102
|
|
|
|
|
103
|
|
|
// Merge location data with ip data |
|
104
|
2 |
|
$data = array_merge($data, (array)$locationInfo); |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
2 |
|
if ($ipAddress->isValid() && $geoLocation->geoLocationOK()) { |
|
108
|
|
|
$page->add("weather/validIP", $data); |
|
109
|
|
|
|
|
110
|
|
|
$weatherInfo = array_merge($weatherInfo, (array)$weatherInfoHist); |
|
111
|
|
|
$page->add("weather/weather", $weatherInfo); |
|
112
|
|
|
} else { |
|
113
|
2 |
|
$page->add("weather/notvalidWeather", $data); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
return $page->render([ |
|
117
|
2 |
|
"title" => $title, |
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* This is the index method action, it handles: |
|
123
|
|
|
* ANY METHOD mountpoint |
|
124
|
|
|
* ANY METHOD mountpoint/ |
|
125
|
|
|
* ANY METHOD mountpoint/index |
|
126
|
|
|
* |
|
127
|
|
|
* @return object |
|
128
|
|
|
*/ |
|
129
|
4 |
|
public function indexActionPost(): object |
|
130
|
|
|
{ |
|
131
|
4 |
|
$request = $this->di->get("request"); |
|
132
|
4 |
|
$iptocheck = $request->getPost("ip") ?? ""; |
|
133
|
|
|
|
|
134
|
4 |
|
$weatherInfo = []; |
|
|
|
|
|
|
135
|
4 |
|
$weatherInfoHist = []; |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
// Prepare page |
|
139
|
4 |
|
$page = $this->di->get("page"); |
|
140
|
4 |
|
$page->add("weather/index"); |
|
141
|
4 |
|
$title = "Weather | ramverk1"; |
|
142
|
|
|
|
|
143
|
|
|
// Validate IP |
|
144
|
4 |
|
$ipAddress = new WeatherIpValidation($iptocheck); |
|
145
|
4 |
|
$data = $ipAddress->answer(); |
|
146
|
|
|
|
|
147
|
4 |
|
if (empty($iptocheck)) { |
|
148
|
1 |
|
return $page->render([ |
|
149
|
1 |
|
"title" => $title, |
|
150
|
|
|
]); |
|
151
|
3 |
|
} elseif (!$ipAddress->isValid()) { |
|
152
|
1 |
|
$page->add("weather/notvalidIP", $data); |
|
153
|
1 |
|
return $page->render([ |
|
154
|
1 |
|
"title" => $title, |
|
155
|
|
|
]); |
|
156
|
|
|
} |
|
157
|
|
|
// Get IP location |
|
158
|
2 |
|
$geoLocation = new WeatherGeoLocation($iptocheck); |
|
|
|
|
|
|
159
|
2 |
|
$geoLocation->setDI($this->di); |
|
160
|
2 |
|
$geoLocation->setAPI("ipstack"); |
|
161
|
2 |
|
$geoLocation->checkGeoLocation($iptocheck); |
|
162
|
2 |
|
$locationInfo = $geoLocation->getGeoLocation(); |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
// Get Weather information Forecast |
|
166
|
2 |
|
$weatherRequest = new WeatherRequest(); |
|
167
|
2 |
|
$weatherRequest->setDI($this->di); |
|
168
|
2 |
|
$weatherRequest->setAPI("openweathermap"); |
|
169
|
2 |
|
$weatherRequest->checkWeather($geoLocation); |
|
170
|
2 |
|
$weatherInfo = (array)$weatherRequest->getWeather(); |
|
171
|
|
|
|
|
172
|
|
|
// Get Weather information Historical Data |
|
173
|
|
|
|
|
174
|
2 |
|
$weatherInfoHist = array("weatherInfoHistorical" => $weatherRequest->checkWeatherMulti($geoLocation)); |
|
175
|
2 |
|
$weatherInfo = array_merge($weatherInfo, $weatherInfoHist); |
|
176
|
|
|
|
|
177
|
|
|
// Merge location data with ip data |
|
178
|
2 |
|
$data = array_merge($data, (array)$locationInfo); |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
2 |
|
if ($ipAddress->isValid() && $geoLocation->geoLocationOK()) { |
|
182
|
|
|
$page->add("weather/validIP", $data); |
|
183
|
|
|
|
|
184
|
|
|
$weatherInfo = array_merge($weatherInfo, (array)$weatherInfoHist); |
|
185
|
|
|
$page->add("weather/weather", $weatherInfo); |
|
186
|
|
|
} else { |
|
187
|
2 |
|
$page->add("weather/notvalidWeather", $data); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
2 |
|
return $page->render([ |
|
191
|
2 |
|
"title" => $title, |
|
192
|
|
|
]); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|