ejessyp /
weather
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Anax\Controller; |
||
| 4 | |||
| 5 | use Anax\Commons\ContainerInjectableInterface; |
||
| 6 | use Anax\Commons\ContainerInjectableTrait; |
||
| 7 | use Anax\OpenWeather\NameToGeo; |
||
| 8 | |||
| 9 | // use Anax\Route\Exception\ForbiddenException; |
||
| 10 | // use Anax\Route\Exception\NotFoundException; |
||
| 11 | // use Anax\Route\Exception\InternalErrorException; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * A sample controller to show how a controller class can be implemented. |
||
| 15 | * The controller will be injected with $di if implementing the interface |
||
| 16 | * ContainerInjectableInterface, like this sample class does. |
||
| 17 | * The controller is mounted on a particular route and can then handle all |
||
| 18 | * requests for that mount point. |
||
| 19 | * |
||
| 20 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||
| 21 | */ |
||
| 22 | class WeatherController implements ContainerInjectableInterface |
||
| 23 | { |
||
| 24 | use ContainerInjectableTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * This is the index method action, it handles: |
||
| 28 | * ANY METHOD mountpoint |
||
| 29 | * ANY METHOD mountpoint/ |
||
| 30 | * ANY METHOD mountpoint/index |
||
| 31 | * |
||
| 32 | * @return object |
||
| 33 | */ |
||
| 34 | 1 | public function indexActionGet() : object |
|
| 35 | { |
||
| 36 | // var_dump($this->di->get("request")); |
||
| 37 | 1 | $page = $this->di->get("page"); |
|
| 38 | 1 | $request = $this->di->get("request"); |
|
| 39 | // var_dump($request->getGet('ip')); |
||
| 40 | 1 | $submit = $request->getGet('submit'); |
|
| 41 | 1 | $ipAdd = $request->getGet('ip'); |
|
| 42 | // var_dump($ipAdd); |
||
| 43 | 1 | if ($submit) { |
|
| 44 | //Check if it is ip or a placename |
||
| 45 | 1 | if (preg_match('/[0-9.]/', $ipAdd)) { |
|
| 46 | // Check if it is valid ipv4 or ipv6 |
||
| 47 | 1 | if (filter_var($ipAdd, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) or |
|
| 48 | 1 | filter_var($ipAdd, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
| 49 | 1 | $ipgeoweather = $this->di->get("ipgeoweather"); |
|
| 50 | 1 | $ipjson = $ipgeoweather->getJson($ipAdd); |
|
| 51 | 1 | $lat = $ipjson["latitude"]; |
|
| 52 | 1 | $lon = $ipjson["longitude"]; |
|
| 53 | 1 | $arr =array($ipjson["country_name"], $ipjson["region_name"], $ipjson["city"]); |
|
| 54 | 1 | $full_add = implode("->", $arr); |
|
| 55 | } else { |
||
| 56 | 1 | $message = "IP format not valid."; |
|
| 57 | } |
||
| 58 | } else { |
||
| 59 | // find geo location from nominatim by placename |
||
| 60 | 1 | $geolocaion = new NameToGeo(); |
|
| 61 | 1 | $ipjson = $geolocaion -> getGeo($ipAdd); |
|
| 62 | 1 | if (count($ipjson) == 0) { |
|
| 63 | $message = "Could not find the place."; |
||
| 64 | } else { |
||
| 65 | 1 | $lat = $ipjson[0] ->lat; |
|
| 66 | 1 | $lon = $ipjson[0] ->lon; |
|
| 67 | 1 | $full_add = $ipjson[0]->display_name; |
|
| 68 | } |
||
| 69 | } |
||
| 70 | 1 | if (!empty($message)) { |
|
| 71 | $data = [ |
||
| 72 | "message" => $message, |
||
| 73 | ]; |
||
| 74 | } else { |
||
| 75 | 1 | $ipArr = array("address"=> $full_add, "latitude"=>$lat, "longitude"=>$lon); |
|
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
| 76 | // Generate unixtime for 5 days history |
||
| 77 | 1 | $fiveDays = []; |
|
| 78 | 1 | for ($i=1; $i < 6; $i++) { |
|
| 79 | 1 | $fiveDays[$i] = strtotime("-$i day"); |
|
| 80 | } |
||
| 81 | // Get data from openweather 5 days history and 7 days forecast |
||
| 82 | 1 | $openweather = $this->di->get("openweather"); |
|
| 83 | 1 | $weather = $openweather->getCurl($lat, $lon); |
|
| 84 | 1 | $history = $openweather->getHistoryMCurl($lat, $lon, $fiveDays); |
|
| 85 | |||
| 86 | $data = [ |
||
| 87 | 1 | "content" => json_encode($ipArr, JSON_PRETTY_PRINT), |
|
| 88 | 1 | "weather" => $weather, |
|
| 89 | 1 | "history" => $history, |
|
| 90 | ]; |
||
| 91 | } |
||
| 92 | // var_dump($data); |
||
| 93 | 1 | $page->add("weather", $data); |
|
| 94 | 1 | return $page->render([ |
|
| 95 | 1 | "title" => "Weather", |
|
| 96 | ]); |
||
| 97 | } else { |
||
| 98 | 1 | $page->add("weather"); |
|
| 99 | 1 | return $page->render([ |
|
| 100 | 1 | "title" => "Weather", |
|
| 101 | ]); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 |