1 | <?php |
||
2 | |||
3 | namespace Jiad\Weather; |
||
4 | |||
5 | use Anax\Commons\ContainerInjectableInterface; |
||
6 | use Anax\Commons\ContainerInjectableTrait; |
||
7 | use Jiad\Modules\GeoTag; |
||
8 | use Jiad\Modules\Curl; |
||
9 | |||
10 | // use Anax\Route\Exception\ForbiddenException; |
||
11 | // use Anax\Route\Exception\NotFoundException; |
||
12 | // use Anax\Route\Exception\InternalErrorException; |
||
13 | |||
14 | /** |
||
15 | * A sample controller to show how a controller class can be implemented. |
||
16 | * The controller will be injected with $di if implementing the interface |
||
17 | * ContainerInjectableInterface, like this sample class does. |
||
18 | * The controller is mounted on a particular route and can then handle all |
||
19 | * requests for that mount point. |
||
20 | * |
||
21 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||
22 | */ |
||
23 | class WeatherController implements ContainerInjectableInterface |
||
24 | { |
||
25 | use ContainerInjectableTrait; |
||
26 | |||
27 | |||
28 | |||
29 | |||
30 | /** |
||
31 | * @var string $db a sample member variable that gets initialised |
||
32 | */ |
||
33 | private $db = "not active"; |
||
34 | |||
35 | |||
36 | |||
37 | /** |
||
38 | * The initialize method is optional and will always be called before the |
||
39 | * target method/action. This is a convienient method where you could |
||
40 | * setup internal properties that are commonly used by several methods. |
||
41 | * |
||
42 | * @return void |
||
43 | */ |
||
44 | 3 | public function initialize() : void |
|
45 | { |
||
46 | // Use to initialise member variables. |
||
47 | 3 | $this->db = "active"; |
|
48 | 3 | } |
|
49 | |||
50 | /** |
||
51 | * This is the index method action, it handles: |
||
52 | * ANY METHOD mountpoint |
||
53 | * ANY METHOD mountpoint/ |
||
54 | * ANY METHOD mountpoint/index |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | 1 | public function indexAction() : object |
|
59 | { |
||
60 | 1 | $title = "Weather"; |
|
61 | 1 | $page = $this->di->get("page"); |
|
62 | |||
63 | 1 | $client_ip = $this->di->get("request")->getServer("REMOTE_ADDR", "127.0.0.1"); |
|
64 | |||
65 | $data = [ |
||
66 | 1 | "client_ip" => $client_ip, |
|
67 | ]; |
||
68 | |||
69 | 1 | $page->add("weather/form", $data); |
|
70 | |||
71 | 1 | return $page->render([ |
|
72 | 1 | "title" => $title |
|
73 | ]); |
||
74 | } |
||
75 | |||
76 | |||
77 | /** |
||
78 | * This is the Validate method action, it handles: |
||
79 | * POST METHOD mountpoint/ |
||
80 | * |
||
81 | * @return object |
||
82 | */ |
||
83 | 2 | public function indexActionPost() : object |
|
84 | { |
||
85 | 2 | $page = $this->di->get("page"); |
|
86 | 2 | $title = "Weather Result"; |
|
87 | |||
88 | // Post |
||
89 | 2 | $searchType = $this->di->request->getPost("searchType"); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
90 | 2 | $search = $this->di->request->getPost("search"); |
|
91 | 2 | $time = $this->di->request->getPost("time"); |
|
92 | |||
93 | 2 | $test = ""; |
|
94 | |||
95 | 2 | $geotag = $this->di->get("geotag"); |
|
96 | |||
97 | 2 | $ipInfo = $geotag->getGeoInfo($search); |
|
98 | |||
99 | |||
100 | $searchOptions = [ |
||
101 | 2 | "lat" => $ipInfo->latitude, |
|
102 | 2 | "long" => $ipInfo->longitude |
|
103 | ]; |
||
104 | |||
105 | 2 | $curl = $this->di->get("curl"); |
|
106 | |||
107 | 2 | $error = 0; |
|
108 | 2 | $type = ""; |
|
109 | |||
110 | 2 | if ($time == "future") { |
|
111 | 1 | $weatherInfo = $curl->sCurl($searchOptions); // Single Curl |
|
112 | 1 | } else if ($time == "past") { |
|
113 | 1 | $weatherInfo = $curl->mCurl($searchOptions); // Multi Curl |
|
114 | } |
||
115 | |||
116 | |||
117 | $data = [ |
||
118 | 2 | "searchType" => $searchType, |
|
119 | 2 | "search" => $search, |
|
120 | 2 | "time" => $time, |
|
121 | 2 | "ipInfo" => $ipInfo, |
|
122 | 2 | "test" => $test, |
|
123 | 2 | "type" => $type, |
|
124 | 2 | "error" => $error, |
|
125 | 2 | "weatherInfo" => $weatherInfo |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
126 | ]; |
||
127 | |||
128 | 2 | $page->add("weather/weatherResult", $data); |
|
129 | |||
130 | 2 | return $page->render([ |
|
131 | 2 | "title" => $title |
|
132 | ]); |
||
133 | } |
||
134 | } |
||
135 |