1 | <?php |
||
2 | |||
3 | namespace Anax\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 | * |
||
14 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||
15 | */ |
||
16 | class WeatherAPIController implements ContainerInjectableInterface |
||
17 | { |
||
18 | use ContainerInjectableTrait; |
||
19 | |||
20 | /** |
||
21 | * This is the index method action, it handles: |
||
22 | * ANY METHOD mountpoint |
||
23 | * ANY METHOD mountpoint/ |
||
24 | * ANY METHOD mountpoint/index |
||
25 | * |
||
26 | * @return string |
||
27 | */ |
||
28 | 4 | public function indexAction() |
|
29 | { |
||
30 | 4 | $title = "Weather info"; |
|
31 | 4 | $page = $this->di->get("page"); |
|
32 | 4 | $request = $this->di->get("request"); |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
33 | |||
34 | 4 | $request = $this->di->get("request"); |
|
35 | |||
36 | 4 | if ($request->hasGet("location")) { |
|
37 | 3 | if ($request->hasGet("prev", true) && $request->hasGet("days")) { |
|
38 | 1 | return $this->getWeather($request->getGet("location"), $request->getGet("days")); |
|
0 ignored issues
–
show
|
|||
39 | } |
||
40 | 2 | return $this->getWeather($request->getGet("location")); |
|
0 ignored issues
–
show
|
|||
41 | } |
||
42 | 1 | $page->add("weather/form-json-weather", []); |
|
43 | 1 | $page->add("weather/api-exemple", []); |
|
44 | |||
45 | 1 | return $page->render([ |
|
46 | 1 | "title" => $title, |
|
47 | ]); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Get weather |
||
52 | * Today, and next 7 days or |
||
53 | * Previous days |
||
54 | * |
||
55 | * @return array |
||
56 | */ |
||
57 | 3 | public function getWeather($location, $days = null) |
|
58 | { |
||
59 | 3 | $CurlModel = $this->di->get("curl"); |
|
60 | 3 | $WeatherModel = $this->di->get("weather"); |
|
61 | 3 | $WeatherModel->setCurl($CurlModel); |
|
62 | |||
63 | 3 | $weatherInfo = []; |
|
64 | 3 | $convertedLocation = $WeatherModel->convertLocation($location); |
|
65 | 3 | ini_set('serialize_precision', -1); |
|
66 | |||
67 | 3 | if ($convertedLocation["match"] && $days !== null) { |
|
68 | 1 | $weatherInfo = $WeatherModel->getWeatherMulti($convertedLocation["latitude"], $convertedLocation["longitude"], $days); |
|
69 | 2 | } else if ($convertedLocation["match"]) { |
|
70 | 1 | $weatherInfo = $WeatherModel->getWeather($convertedLocation["latitude"], $convertedLocation["longitude"], $days); |
|
71 | } |
||
72 | return [[ |
||
73 | 3 | "match" => $convertedLocation["match"], |
|
74 | 3 | "location" => $convertedLocation, |
|
75 | 3 | "weatherinfo" => $weatherInfo |
|
76 | ]]; |
||
77 | } |
||
78 | } |
||
79 |