1 | <?php |
||
2 | |||
3 | namespace Bashar\Weather; |
||
4 | |||
5 | use Anax\Commons\ContainerInjectableInterface; |
||
6 | use Anax\Commons\ContainerInjectableTrait; |
||
7 | |||
8 | /** |
||
9 | * A sample controller to show how a controller class can be implemented. |
||
10 | * The controller will be injected with $di if implementing the interface |
||
11 | * ContainerInjectableInterface, like this sample class does. |
||
12 | * The controller is mounted on a particular route and can then handle all |
||
13 | * requests for that mount point. |
||
14 | * |
||
15 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||
16 | */ |
||
17 | class WeatherIpController implements ContainerInjectableInterface |
||
18 | { |
||
19 | use ContainerInjectableTrait; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $enteredIp; |
||
25 | private $geoApiKey; |
||
26 | private $geoData; |
||
27 | private $ipValidationResult; |
||
28 | protected $getInfoPrevious; |
||
29 | protected $getInfoNext; |
||
30 | |||
31 | |||
32 | 3 | public function initialize() |
|
33 | { |
||
34 | 3 | $this->geoLocationModel = new \Bashar\GeoLocationModel\GeoLocationByIpModel(); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
35 | 3 | $this->geoLocationModel = $this->di->get("ipstackcfg"); |
|
36 | 3 | $this->geoApiKey = $this->geoLocationModel->getDetails(); |
|
37 | |||
38 | 3 | $this->openWeatherMapModel = new \Bashar\WeatherModel\OpenWeatherMapModel(); |
|
0 ignored issues
–
show
|
|||
39 | 3 | $this->openWeatherMapModel = $this->di->get("openWeatherMap"); |
|
40 | |||
41 | 3 | $request = $this->di->get("request"); |
|
42 | 3 | $this->enteredIp = $request->getGet("ip") ?? null; |
|
43 | |||
44 | 3 | $this->openWeatherMapModel->setGeoApi($this->enteredIp, $this->geoApiKey); |
|
45 | |||
46 | 3 | $this->curl = $this->di->get("curl"); |
|
0 ignored issues
–
show
|
|||
47 | |||
48 | 3 | $geoApiUrl = $this->openWeatherMapModel->getGeoApiUrl(); |
|
49 | 3 | $this->geoData = $this->curl->getData($geoApiUrl); |
|
50 | |||
51 | 3 | $this->ipValidatorClass = new \Bashar\WeatherModel\IpValidator(); |
|
0 ignored issues
–
show
|
|||
52 | |||
53 | 3 | $this->ipValidationResult = $this->ipValidatorClass->validateIpInput($this->enteredIp); |
|
54 | 3 | } |
|
55 | |||
56 | /** |
||
57 | * This is the index method action, it handles: |
||
58 | * ANY METHOD mountPoint |
||
59 | * ANY METHOD mountPoint/ |
||
60 | * ANY METHOD mountPoint/index |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | 1 | public function indexAction() : object |
|
65 | { |
||
66 | 1 | $page = $this->di->get("page"); |
|
67 | 1 | $enteredIp = $this->enteredIp; |
|
68 | 1 | $getIpAddress = $_SERVER['REMOTE_ADDR'] ?? null; |
|
69 | 1 | $ipValidationResult = $this->ipValidationResult; |
|
70 | |||
71 | $data = [ |
||
72 | 1 | "getIpAddress" => $getIpAddress ?? null, |
|
73 | 1 | "enteredIp" => $enteredIp ?? null, |
|
74 | 1 | "ipValidationResult" => $ipValidationResult?? null |
|
75 | ]; |
||
76 | |||
77 | 1 | $page->add("WeatherView/index", $data); |
|
78 | 1 | return $page->render([ |
|
79 | 1 | "title" => "Weather landing", |
|
80 | ]); |
||
81 | } |
||
82 | |||
83 | |||
84 | /** |
||
85 | * This is the index method action, it handles: |
||
86 | * ANY METHOD mountPoint |
||
87 | * ANY METHOD mountPoint/ |
||
88 | * ANY METHOD mountPoint/index |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 1 | public function previousAction() : object |
|
93 | { |
||
94 | 1 | $page = $this->di->get("page"); |
|
95 | 1 | $enteredIp = $this->enteredIp; |
|
96 | 1 | $getIpAddress = $_SERVER['REMOTE_ADDR'] ?? null; |
|
97 | 1 | $geoData = $this->geoData; |
|
98 | 1 | $lati = $geoData["latitude"] ?? null; |
|
99 | 1 | $long = $geoData["longitude"] ?? null; |
|
100 | |||
101 | 1 | $this->openWeatherMapModel->setWeatherApi5PreviousDays($lati, $long); |
|
102 | 1 | $this->weatherUrlPrevious = $this->openWeatherMapModel->getWeatherApiPrevious(); |
|
0 ignored issues
–
show
|
|||
103 | 1 | $weatherUrlPrevious = $this->weatherUrlPrevious; |
|
104 | |||
105 | 1 | $this->getInfoPrevious = $this->curl->getDataArray($weatherUrlPrevious); |
|
106 | 1 | $getInfoPrevious = $this->getInfoPrevious; |
|
107 | |||
108 | 1 | $arrIconPrevious = []; |
|
109 | 1 | $tempArrayPrevious= []; |
|
110 | 1 | $dateArrayPrevious = []; |
|
111 | 1 | $wrStatArrayPrev = []; |
|
112 | |||
113 | 1 | if (is_array($getInfoPrevious) || is_object($getInfoPrevious)) { |
|
114 | 1 | foreach ($getInfoPrevious as $value) { |
|
115 | 1 | $weatherStatus = $value["current"]["weather"][0]["main"] ?? null; |
|
116 | 1 | array_push($wrStatArrayPrev, $weatherStatus); |
|
117 | |||
118 | 1 | $timezonePrevious = $value["timezone"] ?? null; |
|
119 | 1 | $date = date("d/m", $value["hourly"][11]["dt"] ?? null); |
|
120 | 1 | array_push($dateArrayPrevious, $date); |
|
121 | |||
122 | 1 | array_push($tempArrayPrevious, round($value["current"]["temp"] ?? null, 0)); |
|
123 | |||
124 | 1 | $icon = $value["current"]["weather"][0]["icon"] ?? null; |
|
125 | 1 | array_push($arrIconPrevious, $icon); |
|
126 | } |
||
127 | } |
||
128 | |||
129 | $json = [ |
||
130 | 1 | "tempArrayPrevious" => $tempArrayPrevious ?? null, |
|
131 | 1 | "dateArrayPrevious" => $dateArrayPrevious ?? null, |
|
132 | 1 | "arrIconPrevious" => $arrIconPrevious ?? null, |
|
133 | 1 | "weatherStatusArrayPrevious" => $wrStatArrayPrev ?? null, |
|
134 | 1 | "timezonePrevious" => $timezonePrevious ?? null, |
|
135 | ]; |
||
136 | |||
137 | 1 | $this->weatherInJson = $this->openWeatherMapModel->getWeatherInJson($json); |
|
0 ignored issues
–
show
|
|||
138 | 1 | $weatherInJson= $this->weatherInJson; |
|
139 | |||
140 | $data = [ |
||
141 | 1 | "weatherInJson" => $weatherInJson ?? null, |
|
142 | 1 | "geoData" => $geoData ?? null, |
|
143 | 1 | "getIpAddress" => $getIpAddress ?? null, |
|
144 | 1 | "enteredIp" => $enteredIp ?? null, |
|
145 | 1 | "tempArrayPrevious" => $tempArrayPrevious ?? null, |
|
146 | 1 | "dateArrayPrevious" => $dateArrayPrevious ?? null, |
|
147 | 1 | "arrIconPrevious" => $arrIconPrevious ?? null, |
|
148 | 1 | "weatherStatusArrayPrevious" => $wrStatArrayPrev ?? null, |
|
149 | 1 | "timezonePrevious" => $timezonePrevious ?? null, |
|
150 | ]; |
||
151 | |||
152 | 1 | $page->add("WeatherView/previous", $data); |
|
153 | 1 | return $page->render([ |
|
154 | 1 | "title" => "Weather history", |
|
155 | ]); |
||
156 | } |
||
157 | |||
158 | |||
159 | /** |
||
160 | * This is the index method action, it handles: |
||
161 | * ANY METHOD mountPoint |
||
162 | * ANY METHOD mountPoint/ |
||
163 | * ANY METHOD mountPoint/index |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | 1 | public function nextAction() : object |
|
168 | { |
||
169 | 1 | $page = $this->di->get("page"); |
|
170 | 1 | $enteredIp = $this->enteredIp; |
|
171 | 1 | $getIpAddress = $_SERVER['REMOTE_ADDR'] ?? null; |
|
172 | 1 | $geoData = $this->geoData; |
|
173 | 1 | $lati = $geoData["latitude"] ?? null; |
|
174 | 1 | $long = $geoData["longitude"] ?? null; |
|
175 | 1 | $ipValidationResult = $this->ipValidationResult; |
|
176 | |||
177 | 1 | $this->openWeatherMapModel->setWeatherApiNext10Days($lati, $long); |
|
178 | 1 | $this->weatherUrlNext = $this->openWeatherMapModel->getWeatherApiNext(); |
|
0 ignored issues
–
show
|
|||
179 | 1 | $weatherUrlNext = $this->weatherUrlNext; |
|
180 | |||
181 | 1 | $this->getInfoNext = $this->curl->getData($weatherUrlNext); |
|
182 | 1 | $getInfoNext = $this->getInfoNext; |
|
183 | |||
184 | |||
185 | 1 | $arrIconNext = []; |
|
186 | 1 | $tempArrayNext= []; |
|
187 | 1 | $dateArrayNext = []; |
|
188 | 1 | $wrStatusArrayNext = []; |
|
189 | 1 | $timezoneNext = $getInfoNext["timezone"] ?? null; |
|
190 | 1 | $getInfoDaily = $getInfoNext["daily"] ?? null; |
|
191 | |||
192 | 1 | if (is_array($getInfoDaily) || is_object($getInfoDaily)) { |
|
193 | foreach ($getInfoDaily as $value1) { |
||
194 | $weatherStatus = $value1["weather"][0]["main"] ?? null; |
||
195 | array_push($wrStatusArrayNext, $weatherStatus); |
||
196 | |||
197 | $date = date("d/m", $value1["dt"] ?? null); |
||
198 | array_push($dateArrayNext, $date); |
||
199 | |||
200 | $icon = $value1["weather"][0]["icon"] ?? null; |
||
201 | array_push($arrIconNext, $icon); |
||
202 | |||
203 | array_push($tempArrayNext, round($value1["temp"]["day"] ?? null, 0)); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | $json = [ |
||
208 | 1 | "tempArrayNext" => $tempArrayNext ?? null, |
|
209 | 1 | "dateArrayNext" => $dateArrayNext ?? null, |
|
210 | 1 | "arrIconNext" => $arrIconNext?? null, |
|
211 | 1 | "weatherStatusArrayNext" => $wrStatusArrayNext?? null, |
|
212 | 1 | "timezoneNext" => $timezoneNext?? null, |
|
213 | ]; |
||
214 | |||
215 | 1 | $this->weatherInJson = $this->openWeatherMapModel->getWeatherInJson($json); |
|
0 ignored issues
–
show
|
|||
216 | 1 | $weatherInJson= $this->weatherInJson; |
|
217 | |||
218 | $data = [ |
||
219 | 1 | "weatherInJson" => $weatherInJson ?? null, |
|
220 | 1 | "geoData" => $geoData ?? null, |
|
221 | 1 | "getIpAddress" => $getIpAddress ?? null, |
|
222 | 1 | "enteredIp" => $enteredIp ?? null, |
|
223 | 1 | "tempArrayNext" => $tempArrayNext ?? null, |
|
224 | 1 | "dateArrayNext" => $dateArrayNext ?? null, |
|
225 | 1 | "arrIconNext" => $arrIconNext?? null, |
|
226 | 1 | "weatherStatusArrayNext" => $wrStatusArrayNext?? null, |
|
227 | 1 | "timezoneNext" => $timezoneNext?? null, |
|
228 | 1 | "ipValidationResult" => $ipValidationResult?? null |
|
229 | ]; |
||
230 | |||
231 | 1 | $page->add("WeatherView/next", $data); |
|
232 | 1 | return $page->render([ |
|
233 | 1 | "title" => "Weather forecast", |
|
234 | ]); |
||
235 | } |
||
236 | } |
||
237 |