|
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 Json controller |
|
17
|
|
|
* |
|
18
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
19
|
|
|
*/ |
|
20
|
|
|
class ForecastJsonController 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
|
4 |
|
public function indexAction() : object |
|
33
|
|
|
{ |
|
34
|
4 |
|
$title = "Forecast"; |
|
35
|
4 |
|
$page = $this->di->get("page"); |
|
36
|
4 |
|
$submitBtn = $this->di->get("request")->getPost("submitBtn"); |
|
37
|
4 |
|
$lat = $this->di->get("request")->getPost("lat"); |
|
38
|
4 |
|
$lon = $this->di->get("request")->getPost("lon"); |
|
39
|
4 |
|
$ipadd = $this->di->get("request")->getPost("ip"); |
|
40
|
4 |
|
$period = $this->di->get("request")->getPost("period"); |
|
41
|
|
|
|
|
42
|
|
|
// cURL wrapper to inject in ForecastAPI |
|
43
|
4 |
|
$curl = $this->di->get("mycurl"); |
|
44
|
|
|
|
|
45
|
|
|
// Client's IP as default for input field |
|
46
|
4 |
|
if (!$ipadd) { |
|
47
|
2 |
|
$ipadd = $this->di->get("request")->getServer('REMOTE_ADDR'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
$route = "index-json"; |
|
51
|
4 |
|
$res = $msg = ""; |
|
52
|
|
|
|
|
53
|
4 |
|
if ($submitBtn) { |
|
54
|
4 |
|
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 = "result-json"; |
|
60
|
|
|
} else { |
|
61
|
1 |
|
$msg = "Latitude and/or longitude are not valid"; |
|
62
|
|
|
} |
|
63
|
3 |
|
} else if ($ipadd) { |
|
64
|
2 |
|
$ipstackApi = new IPStackAPI($ipadd); |
|
65
|
2 |
|
$ipstackApi->setDI($this->di); |
|
66
|
2 |
|
if ($ipstackApi->isValid($ipadd)) { |
|
67
|
1 |
|
$res = $ipstackApi->request(); |
|
68
|
1 |
|
$forecastApi = new ForecastAPI( |
|
69
|
1 |
|
$curl, |
|
70
|
1 |
|
$res["latitude"], |
|
71
|
1 |
|
$res["longitude"] |
|
72
|
|
|
); |
|
73
|
1 |
|
$forecastApi->setDI($this->di); |
|
74
|
1 |
|
$res = $forecastApi->request($period); |
|
75
|
1 |
|
$route = "result-json"; |
|
76
|
|
|
} else { |
|
77
|
1 |
|
$msg = $ipadd . " is not a valid IP address"; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
$page->add("anax/forecast/$route", [ |
|
83
|
4 |
|
"ip" => $ipadd, |
|
84
|
4 |
|
"lat" => $lat, |
|
85
|
4 |
|
"lon" => $lon, |
|
86
|
4 |
|
"res" => $res, |
|
87
|
4 |
|
"msg" => $msg |
|
88
|
|
|
]); |
|
89
|
|
|
|
|
90
|
4 |
|
return $page->render([ |
|
91
|
4 |
|
"title" => $title, |
|
92
|
|
|
]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|