theEmelie /
ramverk1-modul
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Anax\IpController; |
||||
| 4 | |||||
| 5 | use Anax\Models; |
||||
| 6 | use Anax\Commons\ContainerInjectableInterface; |
||||
| 7 | use Anax\Commons\ContainerInjectableTrait; |
||||
| 8 | |||||
| 9 | /** |
||||
| 10 | * |
||||
| 11 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||||
| 12 | */ |
||||
| 13 | class GeoWeatherJsonController implements ContainerInjectableInterface |
||||
| 14 | { |
||||
| 15 | use ContainerInjectableTrait; |
||||
| 16 | |||||
| 17 | private $darksky; |
||||
| 18 | private $ipstack; |
||||
| 19 | |||||
| 20 | 16 | public function initialize() |
|||
| 21 | { |
||||
| 22 | 16 | $this->darksky = $this->di->get('apiDarkSky'); |
|||
| 23 | 16 | $this->ipstack = $this->di->get('apiIpStack'); |
|||
| 24 | 16 | } |
|||
| 25 | |||||
| 26 | 1 | public function indexAction() |
|||
| 27 | { |
||||
| 28 | 1 | $page = $this->di->get("page"); |
|||
| 29 | 1 | $request = $this->di->get("request"); |
|||
| 30 | |||||
| 31 | 1 | $title = "Väderprognos"; |
|||
| 32 | |||||
| 33 | $json = [ |
||||
| 34 | 1 | "ip" => $request->getServer("REMOTE_ADDR", ""), |
|||
| 35 | 1 | "status" => "", |
|||
| 36 | ]; |
||||
| 37 | |||||
| 38 | 1 | $page->add("weather/weatherJson", [ |
|||
| 39 | 1 | "weatherJson" => $json, |
|||
| 40 | "dataExists" => false, |
||||
| 41 | 1 | "status" => "Ingen data tillgänglig", |
|||
| 42 | ]); |
||||
| 43 | |||||
| 44 | 1 | return $page->render([ |
|||
| 45 | 1 | "title" => $title, |
|||
| 46 | ]); |
||||
| 47 | } |
||||
| 48 | |||||
| 49 | 7 | public function indexActionPost() |
|||
| 50 | { |
||||
| 51 | 7 | $request = $this->di->get("request"); |
|||
| 52 | 7 | $location = $request->getPost("location"); |
|||
| 53 | 7 | $futureOrPast = $request->getPost("weather"); |
|||
| 54 | |||||
| 55 | 7 | $darkSkyData = $this->getWeather($location, $futureOrPast); |
|||
| 56 | |||||
| 57 | 7 | return json_encode($darkSkyData); |
|||
| 58 | } |
||||
| 59 | |||||
| 60 | 7 | public function weatherCheckActionGet() |
|||
| 61 | { |
||||
| 62 | 7 | $request = $this->di->get("request"); |
|||
| 63 | 7 | $location = $request->getGet("location"); |
|||
| 64 | 7 | $futureOrPast = $request->getGet("weather"); |
|||
| 65 | |||||
| 66 | 7 | $darkSkyData = $this->getWeather($location, $futureOrPast); |
|||
| 67 | |||||
| 68 | 7 | return json_encode($darkSkyData); |
|||
| 69 | } |
||||
| 70 | |||||
| 71 | 14 | private function getWeather($location, $futureOrPast) |
|||
| 72 | { |
||||
| 73 | 14 | $lat = ""; |
|||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||||
| 74 | 14 | $long = ""; |
|||
|
0 ignored issues
–
show
|
|||||
| 75 | 14 | $json = $this->ipstack->validate($location); |
|||
| 76 | |||||
| 77 | 14 | if ($json["valid"] == true) { |
|||
| 78 | 8 | $darkSkyData = $this->darksky->getDarkSkyWeather($json["ipStackData"]->{"latitude"}, $json["ipStackData"]->{"longitude"}, $futureOrPast); |
|||
| 79 | } else { |
||||
| 80 | 6 | $latLong = explode(",", $location); |
|||
| 81 | 6 | if (count($latLong) == 2 && is_numeric($latLong[0]) && is_numeric($latLong[1])) { |
|||
| 82 | 6 | $lat = trim($latLong[0]); |
|||
| 83 | 6 | $long = trim($latLong[1]); |
|||
| 84 | 6 | $darkSkyData = $this->darksky->getDarkSkyWeather($lat, $long, $futureOrPast); |
|||
| 85 | } else { |
||||
| 86 | $darkSkyData = [ |
||||
| 87 | "weatherJson" => "", |
||||
| 88 | "dataExists" => false, |
||||
| 89 | "status" => "Användar error!", |
||||
| 90 | ]; |
||||
| 91 | } |
||||
| 92 | } |
||||
| 93 | 14 | return $darkSkyData; |
|||
| 94 | } |
||||
| 95 | |||||
| 96 | /** |
||||
| 97 | * Adding an optional catchAll() method will catch all actions sent to the |
||||
| 98 | * router. You can then reply with an actual response or return void to |
||||
| 99 | * allow for the router to move on to next handler. |
||||
| 100 | * A catchAll() handles the following, if a specific action method is not |
||||
| 101 | * created: |
||||
| 102 | * ANY METHOD mountpoint/** |
||||
| 103 | * |
||||
| 104 | * @param array $args as a variadic parameter. |
||||
| 105 | * |
||||
| 106 | * @return mixed |
||||
| 107 | * |
||||
| 108 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||||
| 109 | */ |
||||
| 110 | 1 | public function catchAll(...$args) |
|||
|
0 ignored issues
–
show
The parameter
$args is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 111 | { |
||||
| 112 | // Deal with the request and send an actual response, or not. |
||||
| 113 | //return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args); |
||||
| 114 | 1 | return "404 Not Found"; |
|||
| 115 | } |
||||
| 116 | } |
||||
| 117 |