1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace daib17\Controller; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
use daib17\Model\IPStackAPI; |
9
|
|
|
use daib17\Model\ForecastAPI; |
10
|
|
|
|
11
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
12
|
|
|
// use Anax\Route\Exception\NotFoundException; |
13
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Forecast controller |
17
|
|
|
* |
18
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
19
|
|
|
*/ |
20
|
|
|
class ForecastController implements ContainerInjectableInterface |
21
|
|
|
{ |
22
|
|
|
use ContainerInjectableTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This is the index method action, it handles: |
26
|
|
|
* ANY METHOD mountpoint |
27
|
|
|
* ANY METHOD mountpoint/ |
28
|
|
|
* ANY METHOD mountpoint/index |
29
|
|
|
* |
30
|
|
|
* @return object |
31
|
|
|
*/ |
32
|
5 |
|
public function indexAction() : object |
33
|
|
|
{ |
34
|
5 |
|
$title = "Forecast"; |
35
|
5 |
|
$page = $this->di->get("page"); |
36
|
5 |
|
$submitBtn = $this->di->get("request")->getPost("submitBtn"); |
37
|
5 |
|
$lat = $this->di->get("request")->getPost("lat"); |
38
|
5 |
|
$lon = $this->di->get("request")->getPost("lon"); |
39
|
5 |
|
$ipadd = $this->di->get("request")->getPost("ip"); |
40
|
5 |
|
$period = $this->di->get("request")->getPost("period"); |
41
|
|
|
|
42
|
|
|
// cURL wrapper to inject in ForecastAPI |
43
|
5 |
|
$curl = $this->di->get("mycurl"); |
44
|
|
|
|
45
|
|
|
// Client's IP as default for input field |
46
|
5 |
|
if (!$ipadd) { |
47
|
2 |
|
$ipadd = $this->di->get("request")->getServer('REMOTE_ADDR'); |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
$route = "index"; |
51
|
5 |
|
$res = $msg = ""; |
52
|
|
|
|
53
|
5 |
|
if ($submitBtn) { |
54
|
5 |
|
if ($lat && $lon) { |
55
|
1 |
|
$forecastApi = new ForecastAPI($curl, $lat, $lon); |
56
|
1 |
|
$forecastApi->setDI($this->di); |
57
|
1 |
|
if ($forecastApi->isValid()) { |
58
|
1 |
|
$res = $forecastApi->request($period); |
59
|
1 |
|
$route = $period == 0 ? "result" : "result-past"; |
60
|
|
|
} else { |
61
|
1 |
|
$msg = "Latitude and/or longitude are not valid"; |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
4 |
|
$ipstackApi = new IPStackAPI($ipadd); |
65
|
4 |
|
$ipstackApi->setDI($this->di); |
66
|
4 |
|
if ($ipstackApi->isValid($ipadd)) { |
67
|
2 |
|
$res = $ipstackApi->request(); |
68
|
2 |
|
$forecastApi = new ForecastAPI( |
69
|
2 |
|
$curl, |
70
|
2 |
|
$res["latitude"], |
71
|
2 |
|
$res["longitude"] |
72
|
|
|
); |
73
|
2 |
|
$forecastApi->setDI($this->di); |
74
|
2 |
|
$res = $forecastApi->request($period); |
75
|
2 |
|
$route = $period == 0 ? "result" : "result-past"; |
76
|
|
|
} else { |
77
|
2 |
|
$msg = $ipadd . " is not a valid IP address"; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
$page->add("anax/forecast/$route", [ |
83
|
5 |
|
"ip" => $ipadd, |
84
|
5 |
|
"lat" => $lat, |
85
|
5 |
|
"lon" => $lon, |
86
|
5 |
|
"res" => $res, |
87
|
5 |
|
"msg" => $msg |
88
|
|
|
]); |
89
|
|
|
|
90
|
5 |
|
return $page->render([ |
91
|
5 |
|
"title" => $title, |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|