Hatooz /
weather-module
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Anax\Controller; |
||
| 4 | |||
| 5 | use Anax\Commons\ContainerInjectableInterface; |
||
| 6 | use Anax\Commons\ContainerInjectableTrait; |
||
| 7 | use Anax\Ipstack\Ipstack; |
||
| 8 | |||
| 9 | // use Anax\Route\Exception\ForbiddenException; |
||
| 10 | // use Anax\Route\Exception\NotFoundException; |
||
| 11 | // use Anax\Route\Exception\InternalErrorException; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * A sample JSON 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 | class WeatherCheckController implements ContainerInjectableInterface |
||
| 21 | { |
||
| 22 | use ContainerInjectableTrait; |
||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string $db a sample member variable that gets initialised |
||
| 28 | */ |
||
| 29 | private $db = "not active"; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * The initialize method is optional and will always be called before the |
||
| 34 | * target method/action. This is a convienient method where you could |
||
| 35 | * setup internal properties that are commonly used by several methods. |
||
| 36 | * |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | 5 | public function initialize() : void |
|
| 40 | { |
||
| 41 | // Use to initialise member variables. |
||
| 42 | 5 | $this->db = "active"; |
|
| 43 | 5 | } |
|
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * This is the index method action, it handles: |
||
| 49 | * GET METHOD mountpoint |
||
| 50 | * GET METHOD mountpoint/ |
||
| 51 | * GET METHOD mountpoint/index |
||
| 52 | * |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | 1 | public function indexActionGet() : object |
|
| 56 | { |
||
| 57 | 1 | $weather = $this->di->session->get("weatherData")["content"]["weather"] ?? null; |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 58 | 1 | $ip = $this->di->session->get("weatherData")["content"]["ip"] ?? null; |
|
| 59 | |||
| 60 | $data = [ |
||
| 61 | 1 | "body" => $weather["list"] ?? null, |
|
| 62 | 1 | "lat" => $weather["city"]["coord"]["lat"] ?? null, |
|
| 63 | 1 | "lon" => $weather["city"]["coord"]["lon"] ?? null, |
|
| 64 | 1 | "ip" => $ip ?? null, |
|
| 65 | ]; |
||
| 66 | |||
| 67 | 1 | return $this->di->get("page") |
|
| 68 | 1 | ->add("weather_check", $data) |
|
| 69 | 1 | ->render(["title" => "Weather Check"]); |
|
| 70 | } |
||
| 71 | |||
| 72 | 2 | public function weatherActionPost($withCoords = null) |
|
| 73 | { |
||
| 74 | 2 | $ip = ($this->di->get("request")->getPost("lat") ? $this->di->get("request")->getPost("lat") : $withCoords) ? null : "127.0.0.1"; |
|
| 75 | 2 | $lat = ($this->di->get("request")->getPost("lat") ? $this->di->get("request")->getPost("lat") : $withCoords) ? "56.06" : null; |
|
| 76 | 2 | $lon = ($this->di->get("request")->getPost("lon") ? $this->di->get("request")->getPost("lon") : $withCoords) ? "14.15" : null; |
|
| 77 | 2 | $service = $this->di->get("weatherservice"); |
|
| 78 | $data = [ |
||
| 79 | 2 | "content" => $service->getWeather($ip, $lat, $lon), |
|
| 80 | ]; |
||
| 81 | |||
| 82 | 2 | $this->di->session->set("weatherData", $data); |
|
|
0 ignored issues
–
show
|
|||
| 83 | |||
| 84 | 2 | return $this->di->get("response")->redirect("weather_check"); |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * This sample method dumps the content of $di. |
||
| 89 | * GET mountpoint/dump-app |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | 1 | public function dumpDiActionGet() : array |
|
| 94 | { |
||
| 95 | // Deal with the action and return a response. |
||
| 96 | 1 | $services = implode(", ", $this->di->getServices()); |
|
| 97 | $json = [ |
||
| 98 | 1 | "message" => __METHOD__ . "<p>\$di contains: $services", |
|
| 99 | 1 | "di" => $this->di->getServices(), |
|
| 100 | ]; |
||
| 101 | 1 | return [$json]; |
|
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Try to access a forbidden resource. |
||
| 108 | * ANY mountpoint/forbidden |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | 1 | public function forbiddenAction() : array |
|
| 113 | { |
||
| 114 | // Deal with the action and return a response. |
||
| 115 | $json = [ |
||
| 116 | 1 | "message" => __METHOD__ . ", forbidden to access.", |
|
| 117 | ]; |
||
| 118 | 1 | return [$json, 403]; |
|
| 119 | } |
||
| 120 | } |
||
| 121 |