1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\OpenWeatherMapBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherInterface; |
6
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotAuthorizedException; |
7
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotFoundException; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class WeatherController. |
13
|
|
|
*/ |
14
|
|
|
class WeatherController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherInterface |
18
|
|
|
*/ |
19
|
|
|
protected $weather; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* WeatherController constructor. |
23
|
|
|
* |
24
|
|
|
* @param \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherInterface $weather |
25
|
|
|
*/ |
26
|
|
|
public function __construct(WeatherInterface $weather) |
27
|
|
|
{ |
28
|
|
|
$this->weather = $weather; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns weather data by geographic coordinates. |
33
|
|
|
* |
34
|
|
|
* @param float $latitude |
35
|
|
|
* @param float $longitude |
36
|
|
|
* |
37
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function byGeographicCoordinates($latitude, $longitude) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$response = new Response(); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$data = $this->weather->fetchWeatherDataByGeographicCoordinates($latitude, $longitude); |
45
|
|
|
$response->setContent($data); |
46
|
|
|
} catch (NotAuthorizedException $e) { |
47
|
|
|
$response->setContent($e->getMessage()); |
48
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
49
|
|
|
} catch (NotFoundException $e) { |
50
|
|
|
$response->setContent($e->getMessage()); |
51
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $response; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns weather data by city name. |
59
|
|
|
* |
60
|
|
|
* @param string $cityName |
61
|
|
|
* @param string $countryCode |
62
|
|
|
* |
63
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function byCityName($cityName, $countryCode = '') |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$response = new Response(); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$data = $this->weather->fetchWeatherDataByCityName($cityName, $countryCode); |
71
|
|
|
$response->setContent($data); |
72
|
|
|
} catch (NotAuthorizedException $e) { |
73
|
|
|
$response->setContent($e->getMessage()); |
74
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
75
|
|
|
} catch (NotFoundException $e) { |
76
|
|
|
$response->setContent($e->getMessage()); |
77
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $response; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns weather data by city id. |
85
|
|
|
* |
86
|
|
|
* @param int $cityId |
87
|
|
|
* |
88
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function byCityId($cityId) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$response = new Response(); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$data = $this->weather->fetchWeatherDataByCityId($cityId); |
96
|
|
|
$response->setContent($data); |
97
|
|
|
} catch (NotAuthorizedException $e) { |
98
|
|
|
$response->setContent($e->getMessage()); |
99
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
100
|
|
|
} catch (NotFoundException $e) { |
101
|
|
|
$response->setContent($e->getMessage()); |
102
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $response; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns weather data by zip code. |
110
|
|
|
* |
111
|
|
|
* @param int $zipCode |
112
|
|
|
* @param string $countryCode |
113
|
|
|
* |
114
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function byZipCode($zipCode, $countryCode = '') |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$response = new Response(); |
119
|
|
|
|
120
|
|
|
try { |
121
|
|
|
$data = $this->weather->fetchWeatherDataByZipCode($zipCode, $countryCode); |
122
|
|
|
$response->setContent($data); |
123
|
|
|
} catch (NotAuthorizedException $e) { |
124
|
|
|
$response->setContent($e->getMessage()); |
125
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
126
|
|
|
} catch (NotFoundException $e) { |
127
|
|
|
$response->setContent($e->getMessage()); |
128
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $response; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns weather data for cities in rectangle zone. |
136
|
|
|
* |
137
|
|
|
* @param float $longitudeLeft |
138
|
|
|
* @param float $latitudeBottom |
139
|
|
|
* @param float $longitudeRight |
140
|
|
|
* @param float $latitudeTop |
141
|
|
|
* @param int $mapZoom |
142
|
|
|
* @param string $cluster |
143
|
|
|
* |
144
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function byRectangleZone($longitudeLeft, $latitudeBottom, $longitudeRight, $latitudeTop, $mapZoom = 10, $cluster = 'yes') |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$boundingBox = array($longitudeLeft, $latitudeBottom, $longitudeRight, $latitudeTop, $mapZoom); |
149
|
|
|
|
150
|
|
|
$response = new Response(); |
151
|
|
|
|
152
|
|
|
try { |
153
|
|
|
$data = $this->weather->fetchWeatherDataForCitiesWithinRectangleZone($boundingBox, $cluster); |
154
|
|
|
$response->setContent($data); |
155
|
|
|
} catch (NotAuthorizedException $e) { |
156
|
|
|
$response->setContent($e->getMessage()); |
157
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
158
|
|
|
} catch (NotFoundException $e) { |
159
|
|
|
$response->setContent($e->getMessage()); |
160
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $response; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns weather data for cities in circle. |
168
|
|
|
* |
169
|
|
|
* @param float $latitude |
170
|
|
|
* @param float $longitude |
171
|
|
|
* @param string $cluster |
172
|
|
|
* @param int $numberOfCities |
173
|
|
|
* |
174
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
175
|
|
|
*/ |
176
|
|
View Code Duplication |
public function byCircle($latitude, $longitude, $cluster = 'yes', $numberOfCities = 10) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
$response = new Response(); |
179
|
|
|
|
180
|
|
|
try { |
181
|
|
|
$data = $this->weather->fetchWeatherDataForCitiesInCycle($latitude, $longitude, $cluster, $numberOfCities); |
182
|
|
|
$response->setContent($data); |
183
|
|
|
} catch (NotAuthorizedException $e) { |
184
|
|
|
$response->setContent($e->getMessage()); |
185
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
186
|
|
|
} catch (NotFoundException $e) { |
187
|
|
|
$response->setContent($e->getMessage()); |
188
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $response; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns weather data by several city ids. |
196
|
|
|
* |
197
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
198
|
|
|
* |
199
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
200
|
|
|
*/ |
201
|
|
|
public function byCityIds(Request $request) |
202
|
|
|
{ |
203
|
|
|
$response = new Response(); |
204
|
|
|
if (!$request->query->has('cities')) { |
205
|
|
|
return $response->setStatusCode(Response::HTTP_BAD_REQUEST); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$cities = $request->query->get('cities'); |
209
|
|
|
$cities = explode(',', $cities); |
210
|
|
|
|
211
|
|
|
try { |
212
|
|
|
$data = $this->weather->fetchWeatherDataForSeveralCityIds($cities); |
213
|
|
|
$response->setContent($data); |
214
|
|
|
} catch (NotAuthorizedException $e) { |
215
|
|
|
$response->setContent($e->getMessage()); |
216
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
217
|
|
|
} catch (NotFoundException $e) { |
218
|
|
|
$response->setContent($e->getMessage()); |
219
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $response; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.