1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\OpenWeatherMapBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherStationsInterface; |
6
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotAuthorizedException; |
7
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotFoundException; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class WeatherStationsController. |
12
|
|
|
*/ |
13
|
|
|
class WeatherStationsController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherStationsInterface |
17
|
|
|
*/ |
18
|
|
|
protected $weatherStations; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* WeatherStationsController constructor. |
22
|
|
|
* |
23
|
|
|
* @param \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\WeatherStationsInterface $weatherStations |
24
|
|
|
*/ |
25
|
|
|
public function __construct(WeatherStationsInterface $weatherStations) |
26
|
|
|
{ |
27
|
|
|
$this->weatherStations = $weatherStations; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns data from one stations by station id. |
32
|
|
|
* |
33
|
|
|
* @param int $stationId |
34
|
|
|
* |
35
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function getFromOnStationById($stationId) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$response = new Response(); |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
$data = $this->weatherStations->fetchFromOnStationById($stationId); |
43
|
|
|
$response->setContent($data); |
44
|
|
|
} catch (NotAuthorizedException $e) { |
45
|
|
|
$response->setContent($e->getMessage()); |
46
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
47
|
|
|
} catch (NotFoundException $e) { |
48
|
|
|
$response->setContent($e->getMessage()); |
49
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $response; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns data from several stations by rectangle zone. |
57
|
|
|
* |
58
|
|
|
* @param float $longitudeTopLeft |
59
|
|
|
* @param float $latitudeTopLeft |
60
|
|
|
* @param float $longitudeBottomRight |
61
|
|
|
* @param float $latitudeBottomRight |
62
|
|
|
* @param int $mapZoom |
63
|
|
|
* @param string $cluster |
64
|
|
|
* @param int $numberOfStations |
65
|
|
|
* |
66
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function getFromSeveralByRectangleZone($longitudeTopLeft, $latitudeTopLeft, $longitudeBottomRight, $latitudeBottomRight, $mapZoom, $cluster = 'yes', $numberOfStations = 10) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$boundingBox = array( |
71
|
|
|
$longitudeTopLeft, $latitudeTopLeft, $longitudeBottomRight, $latitudeBottomRight, $mapZoom, |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$response = new Response(); |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$data = $this->weatherStations->fetchFromSeveralByRectangleZone($boundingBox, $cluster, $numberOfStations); |
78
|
|
|
$response->setContent($data); |
79
|
|
|
} catch (NotAuthorizedException $e) { |
80
|
|
|
$response->setContent($e->getMessage()); |
81
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
82
|
|
|
} catch (NotFoundException $e) { |
83
|
|
|
$response->setContent($e->getMessage()); |
84
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $response; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns data from several stations by geo point. |
92
|
|
|
* |
93
|
|
|
* @param float $latitude |
94
|
|
|
* @param float $longitude |
95
|
|
|
* @param int $numberOfStations |
96
|
|
|
* |
97
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function getFromSeveralByGeoPoint($latitude, $longitude, $numberOfStations = 10) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$response = new Response(); |
102
|
|
|
|
103
|
|
|
try { |
104
|
|
|
$data = $this->weatherStations->fetchFromSeveralByGeoPoint($latitude, $longitude, $numberOfStations); |
105
|
|
|
$response->setContent($data); |
106
|
|
|
} catch (NotAuthorizedException $e) { |
107
|
|
|
$response->setContent($e->getMessage()); |
108
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
109
|
|
|
} catch (NotFoundException $e) { |
110
|
|
|
$response->setContent($e->getMessage()); |
111
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $response; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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.