1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Blixter\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 WeatherApi implements ContainerInjectableInterface |
22
|
|
|
{ |
23
|
|
|
use ContainerInjectableTrait; |
24
|
|
|
/** |
25
|
|
|
* This is the index method action, it handles: |
26
|
|
|
* GET METHOD mountpoint |
27
|
|
|
* GET METHOD mountpoint/ |
28
|
|
|
* GET METHOD mountpoint/index |
29
|
|
|
* |
30
|
|
|
* @return Array |
31
|
|
|
*/ |
32
|
2 |
|
public function indexActionGet(): array |
33
|
|
|
{ |
34
|
2 |
|
$request = $this->di->get("request"); |
35
|
2 |
|
$location = $request->getGet("location"); |
36
|
|
|
// Using ipValidation class from $di. |
37
|
2 |
|
$ipValidation = $this->di->get("ipvalidation"); |
38
|
2 |
|
$isIpValid = $ipValidation->isIpValid($location); |
39
|
2 |
|
$ipGeoModel = $this->di->get("ipgeo"); |
40
|
2 |
|
$curlhandler = $this->di->get("curlhandler"); |
41
|
2 |
|
$weatherModel = $this->di->get("weather"); |
42
|
2 |
|
$weatherModel->setCurl($curlhandler); |
43
|
2 |
|
$ipGeoModel->setCurl($curlhandler); |
44
|
|
|
|
45
|
2 |
|
$isLocationValid = $weatherModel->getCoordinates($location); |
46
|
2 |
|
$coords = null; |
47
|
|
|
|
48
|
|
|
// Check if the sent location was an valid IP. |
49
|
2 |
|
if ($isIpValid) { |
50
|
2 |
|
$apiRes = $ipGeoModel->fetchData($location); |
51
|
|
|
$coords = [ |
52
|
2 |
|
"lon" => $apiRes["longitude"] ?? null, |
53
|
2 |
|
"lat" => $apiRes["latitude"] ?? null, |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Check if the sent location was an valid address. |
58
|
2 |
|
if ($isLocationValid) { |
59
|
2 |
|
$coords = $isLocationValid; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
if ($coords) { |
63
|
2 |
|
$weatherChoice = $request->getGet("period"); |
64
|
2 |
|
if (!$weatherChoice) { |
65
|
1 |
|
$weatherChoice = "upcoming"; |
66
|
|
|
} |
67
|
2 |
|
if ($weatherChoice === "upcoming") { |
68
|
1 |
|
$weatherData = $weatherModel->fetchData($coords); |
69
|
1 |
|
} else if ($weatherChoice === "past") { |
70
|
1 |
|
$weatherData = $weatherModel->fetchDataMulti($coords); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$data = [ |
75
|
2 |
|
"weatherData" => $weatherData ?? "Not valid location", |
76
|
|
|
]; |
77
|
2 |
|
return [$data]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|