mangepong /
ramverk1-weather
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Anax\Controller; |
||
| 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 WeatherCheckJsonController implements ContainerInjectableInterface |
||
| 22 | { |
||
| 23 | use ContainerInjectableTrait; |
||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string $db a sample member variable that gets initialised |
||
| 29 | */ |
||
| 30 | private $db = "not active"; |
||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * The initialize method is optional and will always be called before the |
||
| 36 | * target method/action. This is a convienient method where you could |
||
| 37 | * setup internal properties that are commonly used by several methods. |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | public function initialize() : void |
||
| 42 | { |
||
| 43 | // Use to initialise member variables. |
||
| 44 | $this->db = "active"; |
||
| 45 | $this->IpCurl = new IpCurl(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 46 | $this->WeatherCurl = new WeatherCurl(); |
||
|
0 ignored issues
–
show
|
|||
| 47 | } |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Update current selected style. |
||
| 52 | * |
||
| 53 | * @return object |
||
| 54 | */ |
||
| 55 | public function indexActionGet() |
||
| 56 | { |
||
| 57 | $request = $this->di->get("request"); |
||
| 58 | $ip = $request->getGet("ip"); |
||
| 59 | |||
| 60 | $ipres = $this->IpCurl->curl($ip); |
||
| 61 | $lat = $ipres["latitude"]; |
||
| 62 | $long = $ipres["longitude"]; |
||
| 63 | $weatherres = $this->WeatherCurl->curl($lat, $long); |
||
| 64 | |||
| 65 | if (isset($weatherres[0]["daily"])) { |
||
| 66 | $day1 = [ |
||
| 67 | "valid" => "True", |
||
| 68 | "summary" => $weatherres[0]["daily"]["data"][0]["summary"], |
||
| 69 | "date" => gmdate("Y-m-d", $weatherres[0]["daily"]["data"][0]["time"]), |
||
| 70 | "highest_temp" => $weatherres[0]["daily"]["data"][0]["temperatureMax"], |
||
| 71 | "lowest_temp" => $weatherres[0]["daily"]["data"][0]["temperatureMin"], |
||
| 72 | ]; |
||
| 73 | $day2 = [ |
||
| 74 | "valid" => "True", |
||
| 75 | "summary" => $weatherres[1]["daily"]["data"][0]["summary"], |
||
| 76 | "date" => gmdate("Y-m-d", $weatherres[1]["daily"]["data"][0]["time"]), |
||
| 77 | "highest_temp" => $weatherres[1]["daily"]["data"][0]["temperatureMax"], |
||
| 78 | "lowest_temp" => $weatherres[1]["daily"]["data"][0]["temperatureMin"], |
||
| 79 | ]; |
||
| 80 | $day3 = [ |
||
| 81 | "valid" => "True", |
||
| 82 | "summary" => $weatherres[2]["daily"]["data"][0]["summary"], |
||
| 83 | "date" => gmdate("Y-m-d", $weatherres[2]["daily"]["data"][0]["time"]), |
||
| 84 | "highest_temp" => $weatherres[2]["daily"]["data"][0]["temperatureMax"], |
||
| 85 | "lowest_temp" => $weatherres[2]["daily"]["data"][0]["temperatureMin"], |
||
| 86 | ]; |
||
| 87 | $day4 = [ |
||
| 88 | "valid" => "True", |
||
| 89 | "summary" => $weatherres[3]["daily"]["data"][0]["summary"], |
||
| 90 | "date" => gmdate("Y-m-d", $weatherres[3]["daily"]["data"][0]["time"]), |
||
| 91 | "highest_temp" => $weatherres[3]["daily"]["data"][0]["temperatureMax"], |
||
| 92 | "lowest_temp" => $weatherres[3]["daily"]["data"][0]["temperatureMin"], |
||
| 93 | ]; |
||
| 94 | $day5 = [ |
||
| 95 | "valid" => "True", |
||
| 96 | "summary" => $weatherres[4]["daily"]["data"][0]["summary"], |
||
| 97 | "date" => gmdate("Y-m-d", $weatherres[4]["daily"]["data"][0]["time"]), |
||
| 98 | "highest_temp" => $weatherres[4]["daily"]["data"][0]["temperatureMax"], |
||
| 99 | "lowest_temp" => $weatherres[4]["daily"]["data"][0]["temperatureMin"], |
||
| 100 | ]; |
||
| 101 | $day6 = [ |
||
| 102 | "valid" => "True", |
||
| 103 | "summary" => $weatherres[5]["daily"]["data"][0]["summary"], |
||
| 104 | "date" => gmdate("Y-m-d", $weatherres[5]["daily"]["data"][0]["time"]), |
||
| 105 | "highest_temp" => $weatherres[5]["daily"]["data"][0]["temperatureMax"], |
||
| 106 | "lowest_temp" => $weatherres[5]["daily"]["data"][0]["temperatureMin"], |
||
| 107 | ]; |
||
| 108 | $day7 = [ |
||
| 109 | "valid" => "True", |
||
| 110 | "summary" => $weatherres[6]["daily"]["data"][0]["summary"], |
||
| 111 | "date" => gmdate("Y-m-d", $weatherres[6]["daily"]["data"][0]["time"]), |
||
| 112 | "highest_temp" => $weatherres[6]["daily"]["data"][0]["temperatureMax"], |
||
| 113 | "lowest_temp" => $weatherres[6]["daily"]["data"][0]["temperatureMin"], |
||
| 114 | ]; |
||
| 115 | $res = [ |
||
| 116 | "1" => $day1, |
||
| 117 | "2" => $day2, |
||
| 118 | "3" => $day3, |
||
| 119 | "4" => $day4, |
||
| 120 | "5" => $day5, |
||
| 121 | "6" => $day6, |
||
| 122 | "7" => $day7, |
||
| 123 | ]; |
||
| 124 | } else { |
||
| 125 | $jsonres = [ |
||
| 126 | "Message" => "No data found for this location!", |
||
| 127 | ]; |
||
| 128 | return [$jsonres]; |
||
|
0 ignored issues
–
show
|
|||
| 129 | } |
||
| 130 | |||
| 131 | return [$res]; |
||
|
0 ignored issues
–
show
|
|||
| 132 | } |
||
| 133 | } |
||
| 134 |