fridalindgren91 /
ramverk1-module
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Anax\Controller; |
||
| 4 | |||
| 5 | use Anax\Commons\ContainerInjectableInterface; |
||
| 6 | use Anax\Commons\ContainerInjectableTrait; |
||
| 7 | |||
| 8 | use Anax\DI; |
||
| 9 | |||
| 10 | use Anax\models\ipAdress; |
||
| 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 WeatherController implements ContainerInjectableInterface |
||
| 22 | { |
||
| 23 | use ContainerInjectableTrait; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string $db a sample member variable that gets initialised |
||
| 27 | */ |
||
| 28 | private $db = "not active"; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The initialize method is optional and will always be called before the |
||
| 32 | * target method/action. This is a convienient method where you could |
||
| 33 | * setup internal properties that are commonly used by several methods. |
||
| 34 | * |
||
| 35 | * @return void |
||
| 36 | */ |
||
| 37 | 10 | public function initialize() : void |
|
| 38 | { |
||
| 39 | // Use to initialise member variables. |
||
| 40 | 10 | $this->db = "active"; |
|
| 41 | 10 | } |
|
| 42 | |||
| 43 | /** |
||
| 44 | * This is the index method action, it handles: |
||
| 45 | * ANY METHOD mountpoint |
||
| 46 | * ANY METHOD mountpoint/ |
||
| 47 | * ANY METHOD mountpoint/index |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | 1 | public function indexAction() : string |
|
| 52 | { |
||
| 53 | 1 | return __METHOD__ . ", \$db is {$this->db}"; |
|
| 54 | } |
||
| 55 | |||
| 56 | 1 | public function jsonActionGet() : array |
|
| 57 | { |
||
| 58 | 1 | $services = implode(", ", $this->di->getServices()); |
|
| 59 | $json = [ |
||
| 60 | "message" => __METHOD__ . "<p>\$di contains: |
||
| 61 | 1 | $services", |
|
| 62 | 1 | "di" => $this->di->getServices(), |
|
| 63 | ]; |
||
| 64 | 1 | return [$json]; |
|
| 65 | } |
||
| 66 | |||
| 67 | 1 | public function pageActionGet() : object |
|
| 68 | { |
||
| 69 | 1 | $page = $this->di->get("page"); |
|
| 70 | |||
| 71 | 1 | $ipModel = new IpAdress(); |
|
| 72 | 1 | $userIp = $ipModel->getUserIp($this->di->get("request")); |
|
| 73 | |||
| 74 | $data = [ |
||
| 75 | 1 | "content" => "<h3>Väderprognos för specifik plats</h3>", |
|
| 76 | 1 | "contentJSON" => "<h3>Väderprognos för specifik plats (JSON)", |
|
| 77 | 1 | "userIp" => $userIp |
|
| 78 | ]; |
||
| 79 | |||
| 80 | 1 | $title = "Väderprognos"; |
|
| 81 | |||
| 82 | 1 | $page->add("weather/weather", $data); |
|
| 83 | 1 | return $page->render([ |
|
| 84 | 1 | "title" => $title |
|
| 85 | ]); |
||
| 86 | } |
||
| 87 | |||
| 88 | 5 | public function checkAdressActionPost() : object |
|
| 89 | { |
||
| 90 | 5 | $ipAdress = null; |
|
| 91 | // $data = []; |
||
| 92 | 5 | $errorMessage = null; |
|
| 93 | 5 | $longitude = null; |
|
| 94 | 5 | $latitude = null; |
|
| 95 | 5 | $weatherJSONObject = null; |
|
| 96 | 5 | $inputLong = null; |
|
| 97 | 5 | $inputLat = null; |
|
| 98 | 5 | $city = null; |
|
| 99 | |||
| 100 | 5 | if ($this->di->get("request")->getPost("ipadress") != null) { |
|
| 101 | 2 | $ipAdress = $this->di->get("request")->getPost("ipadress"); |
|
| 102 | } |
||
| 103 | 5 | if ($this->di->get("request")->getPost("inputLong") != null && $this->di->get("request")->getPost("inputLat") != null) { |
|
| 104 | 2 | $inputLong = $this->di->get("request")->getPost("inputLong"); |
|
| 105 | 2 | $inputLat = $this->di->get("request")->getPost("inputLat"); |
|
| 106 | } |
||
| 107 | 5 | if (($ipAdress == null) && $inputLat == null && $inputLong == null) { |
|
| 108 | 1 | $errorMessage = "Du måste ange antingen en ip-adress eller longitud och latitud!"; |
|
| 109 | } |
||
| 110 | |||
| 111 | 5 | if ($ipAdress != null) { |
|
| 112 | 2 | $validateIp = $this->validateIp($ipAdress); |
|
| 113 | 2 | if ($validateIp == false) { |
|
|
0 ignored issues
–
show
|
|||
| 114 | 1 | $errorMessage = "Du har angett en ogiltig ip-adress!"; |
|
| 115 | } else { |
||
| 116 | 1 | $ipstack = $this->di->get("ipstack"); |
|
| 117 | 1 | $longlatJSONObject = $ipstack->getIpInfo($ipAdress); |
|
| 118 | 1 | $city = $longlatJSONObject["city"]; |
|
| 119 | 1 | $longitude = $longlatJSONObject["longitude"]; |
|
| 120 | 1 | $latitude = $longlatJSONObject["latitude"]; |
|
| 121 | 1 | $darksky = $this->di->get("darksky"); |
|
| 122 | 2 | $weatherJSONObject = $darksky->getWeatherData($longitude, $latitude); |
|
| 123 | } |
||
| 124 | 3 | } elseif ($ipAdress == null && $inputLong != null && $inputLat != null) { |
|
| 125 | 2 | $latitude = $inputLat; |
|
| 126 | 2 | $longitude = $inputLong; |
|
| 127 | 2 | $darksky = $this->di->get("darksky"); |
|
| 128 | 2 | $weatherJSONObject = $darksky->getWeatherData($longitude, $latitude); |
|
| 129 | 2 | if (isset($weatherJSONObject[0]["code"]) && ($weatherJSONObject[0]["code"] == "400")) { |
|
| 130 | 1 | $errorMessage = "Du har angett felaktiga koordinater!"; |
|
| 131 | 1 | $weatherJSONObject = null; |
|
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | $data = [ |
||
| 136 | 5 | "content" => "<h3>Väderprognos</h3>", |
|
| 137 | 5 | "contentJSON" => "<h3>Väderprognos (JSON)", |
|
| 138 | 5 | "result" => $weatherJSONObject, |
|
| 139 | 5 | "longitude" => $longitude, |
|
| 140 | 5 | "latitude" => $latitude, |
|
| 141 | 5 | "userIp" => $ipAdress, |
|
| 142 | 5 | "errorMessage" => $errorMessage, |
|
| 143 | 5 | "city" => $city |
|
| 144 | ]; |
||
| 145 | |||
| 146 | 5 | $page = $this->di->get("page"); |
|
| 147 | 5 | $title = "Väderprognos"; |
|
| 148 | 5 | $page->add("weather/weather", $data); |
|
| 149 | 5 | return $page->render([ |
|
| 150 | 5 | "title" => $title |
|
| 151 | ]); |
||
| 152 | } |
||
| 153 | |||
| 154 | 2 | public function checkIPJSONActionGet() : array |
|
| 155 | { |
||
| 156 | 2 | $ipAdressJSON = null; |
|
| 157 | 2 | $inputLongJSON = null; |
|
| 158 | 2 | $inputLatJSON = null; |
|
| 159 | 2 | $weatherJSONObject = null; |
|
| 160 | 2 | $errorMessage = null; |
|
| 161 | 2 | $city = null; |
|
| 162 | |||
| 163 | 2 | if ($this->di->get("request")->getGet("ipadressJSON") != null) { |
|
| 164 | 1 | $ipAdressJSON = $this->di->get("request")->getGet("ipadressJSON"); |
|
| 165 | } |
||
| 166 | 2 | if ($this->di->get("request")->getGet("inputLongJSON") != null && $this->di->get("request")->getGet("inputLatJSON") != null) { |
|
| 167 | 1 | $inputLongJSON = $this->di->get("request")->getGet("inputLongJSON"); |
|
| 168 | 1 | $inputLatJSON = $this->di->get("request")->getGet("inputLatJSON"); |
|
| 169 | } |
||
| 170 | 2 | if (($ipAdressJSON == null) && $inputLatJSON == null && $inputLongJSON == null) { |
|
| 171 | $errorMessage = "Du måste ange antingen en ip-adress eller longitud och latitud!"; |
||
| 172 | } |
||
| 173 | |||
| 174 | 2 | if ($ipAdressJSON != null) { |
|
| 175 | 1 | $validateIp = $this->validateIp($ipAdressJSON); |
|
| 176 | 1 | if ($validateIp == false) { |
|
|
0 ignored issues
–
show
|
|||
| 177 | 1 | $errorMessage = "Du har angett en ogiltig ip-adress!"; |
|
| 178 | } else { |
||
| 179 | $ipstack = $this->di->get("ipstack"); |
||
| 180 | $longlatJSONObject = $ipstack->getIpInfo($ipAdressJSON); |
||
| 181 | $longitude = $longlatJSONObject["longitude"]; |
||
| 182 | $latitude = $longlatJSONObject["latitude"]; |
||
| 183 | $city = $longlatJSONObject["city"]; |
||
| 184 | $darksky = $this->di->get("darksky"); |
||
| 185 | 1 | $weatherJSONObject = $darksky->getWeatherData($longitude, $latitude); |
|
| 186 | } |
||
| 187 | 1 | } elseif ($ipAdressJSON == null && $inputLongJSON != null && $inputLatJSON != null) { |
|
| 188 | 1 | $latitude = $inputLatJSON; |
|
| 189 | 1 | $longitude = $inputLongJSON; |
|
| 190 | 1 | $darksky = $this->di->get("darksky"); |
|
| 191 | 1 | $weatherJSONObject = $darksky->getWeatherData($longitude, $latitude); |
|
| 192 | 1 | if (isset($weatherJSONObject[0]["code"]) && ($weatherJSONObject[0]["code"] == "400")) { |
|
| 193 | 1 | $errorMessage = "Du har angett felaktiga koordinater!"; |
|
| 194 | 1 | $weatherJSONObject = null; |
|
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | 2 | if ($errorMessage !== null) { |
|
| 199 | $json = [ |
||
| 200 | 2 | "error" => $errorMessage |
|
| 201 | ]; |
||
| 202 | 2 | return [$json]; |
|
| 203 | } elseif ($city != null) { |
||
| 204 | $json = [ |
||
| 205 | "sammanfattning" => $weatherJSONObject[0]["daily"]["summary"], |
||
| 206 | "plats" => $city, |
||
| 207 | "väderObjekt" => $weatherJSONObject[0]["daily"]["data"] |
||
| 208 | ]; |
||
| 209 | return [$json]; |
||
| 210 | } else { |
||
| 211 | $json = [ |
||
| 212 | "sammanfattning" => $weatherJSONObject[0]["daily"]["summary"], |
||
| 213 | "longitud" => $longitude, |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 214 | "latitude" => $latitude, |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 215 | "väderObjekt" => $weatherJSONObject[0]["daily"]["data"] |
||
| 216 | ]; |
||
| 217 | return [$json]; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | 3 | public function validateIp($ip) |
|
| 222 | { |
||
| 223 | 3 | if (filter_var($ip, FILTER_VALIDATE_IP)) { |
|
| 224 | 1 | return true; |
|
| 225 | } else { |
||
| 226 | 2 | return false; |
|
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.