1 | <?php |
||
2 | |||
3 | namespace Bjos\Weather; |
||
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 | * Controller to get location of an IP address. |
||
14 | * |
||
15 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
16 | */ |
||
17 | class WeatherApiController implements ContainerInjectableInterface |
||
18 | { |
||
19 | use ContainerInjectableTrait; |
||
20 | |||
21 | private $validate; |
||
22 | protected $geo; |
||
23 | protected $ipStack; |
||
24 | protected $option; |
||
25 | protected $weather; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * The initialize method is optional and will always be called before the |
||
30 | * target method/action. This is a convienient method where you could |
||
31 | * setup internal properties that are commonly used by several methods. |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | 9 | public function initialize() : void |
|
36 | { |
||
37 | // Use to initialise member variables. |
||
38 | 9 | $this->ipStack = "http://api.ipstack.com/"; |
|
39 | 9 | $this->option = "?access_key="; |
|
40 | 9 | $this->geo = $this->di->get("geolocation"); |
|
41 | 9 | $this->validate = $this->di->get("validate"); |
|
42 | 9 | $this->weather = $this->di->get("weather"); |
|
43 | 9 | } |
|
44 | |||
45 | /** |
||
46 | * Shows basic instruction how to use api. |
||
47 | * |
||
48 | * @return array |
||
49 | */ |
||
50 | 1 | public function indexActionGet() : array |
|
51 | { |
||
52 | $json = [ |
||
53 | "data" => [ |
||
54 | 1 | "message" => "Use POST with IP-address or coordinates separated by comma (',') in body to get weather", |
|
55 | "example ip" => "POST /weatherapi/ {'search': '8.8.8.8'}", |
||
56 | "example Long,Lat" => "POST /weatherapi/ {'search': '67,20'}", |
||
57 | ], |
||
58 | ]; |
||
59 | 1 | return [$json]; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Get wheter info by ip-address or coordinates. |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | 8 | public function indexActionPost() : array |
|
68 | { |
||
69 | 8 | $mapUrl = "https://www.openstreetmap.org/search?query="; |
|
70 | 8 | $lat = null; |
|
71 | 8 | $long = null; |
|
72 | 8 | $location= null; |
|
73 | 8 | $type = ["history", "forecast"]; |
|
74 | 8 | $forecast = null; |
|
75 | 8 | $city = null; |
|
76 | 8 | $country = null; |
|
77 | 8 | $search = $this->di->request->getPost("search"); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
78 | 8 | $weather = $this->di->request->getPost("weather"); |
|
79 | |||
80 | 8 | if (!$search && !$weather) { |
|
81 | 1 | http_response_code(400); |
|
82 | $json = [ |
||
83 | "error" => [ |
||
84 | 1 | "status" => http_response_code(), |
|
85 | 1 | "error" => "Bad Request", |
|
86 | 1 | "search" => $search, |
|
87 | 1 | "weather" => $weather, |
|
88 | 1 | "message" => "Argument search and weather missing in body" |
|
89 | ] |
||
90 | ]; |
||
91 | 1 | return [$json]; |
|
92 | 7 | } elseif (!in_array($weather, $type)) { |
|
93 | 1 | http_response_code(400); |
|
94 | $json = [ |
||
95 | "error" => [ |
||
96 | 1 | "status" => http_response_code(), |
|
97 | 1 | "error" => "Bad Request", |
|
98 | 1 | "weather" => $weather, |
|
99 | 1 | "message" => "Not valid input 'weather': '{$weather}', use 'history' or 'forecast'" |
|
100 | ] |
||
101 | ]; |
||
102 | 1 | return [$json]; |
|
103 | 6 | } elseif ($search) { |
|
104 | 5 | $valid = $this->validate->validate($search); |
|
105 | 5 | if ($valid) { |
|
106 | 1 | $location = $this->geo->getLocation($search, $this->ipStack, $this->option); |
|
107 | 1 | $lat = $location["latitude"]; |
|
108 | 1 | $long = $location["longitude"]; |
|
109 | 1 | $city = $location["city"]; |
|
110 | 1 | $country = $location["country_name"]; |
|
111 | } else { |
||
112 | 4 | $location = $this->weather->getLatLong($search); |
|
113 | 4 | $lat = $location["latitude"]; |
|
114 | 4 | $long = $location["longitude"]; |
|
115 | } |
||
116 | |||
117 | 5 | if ($weather === "history") { |
|
118 | 2 | $forecast = $this->weather->getHistory($lat, $long); |
|
119 | 3 | } elseif ($weather === "forecast") { |
|
120 | 3 | $forecast = $this->weather->getWeather($lat, $long); |
|
121 | } |
||
122 | |||
123 | 5 | $mapLink = $mapUrl . $location['latitude'] . "," . $location['longitude']; |
|
124 | $json = [ |
||
125 | 5 | "search" => $search, |
|
126 | 5 | "ip_address" => $valid, |
|
127 | 5 | "weather_type" => $weather, |
|
128 | "location" => [ |
||
129 | 5 | "latitude" => $location["latitude"], |
|
130 | 5 | "longitude" => $location["longitude"], |
|
131 | 5 | "city" => $city, |
|
132 | 5 | "country_name" => $country, |
|
133 | ], |
||
134 | 5 | "forecast" => $forecast, |
|
135 | 5 | "map_link" => $location['latitude'] !== null ? $mapLink : null |
|
136 | ]; |
||
137 | } else { |
||
138 | 1 | http_response_code(400); |
|
139 | $json = [ |
||
140 | "error" => [ |
||
141 | 1 | "status" => http_response_code(), |
|
142 | 1 | "error" => "Bad Request", |
|
143 | 1 | "search" => $search, |
|
144 | 1 | "message" => "'search' missing in body" |
|
145 | ] |
||
146 | ]; |
||
147 | 1 | return [$json]; |
|
148 | } |
||
149 | 5 | if (empty($lat) || empty($forecast)) { |
|
150 | 2 | http_response_code(404); |
|
151 | $json = [ |
||
152 | "error" => [ |
||
153 | 2 | "status" => http_response_code(), |
|
154 | 2 | "error" => "Not found", |
|
155 | 2 | "search" => $search, |
|
156 | 2 | "weather" => $weather, |
|
157 | 2 | "message" => "Location not found" |
|
158 | ] |
||
159 | ]; |
||
160 | } |
||
161 | 5 | return [$json]; |
|
162 | } |
||
163 | } |
||
164 |