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