1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\OpenWeatherMapBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\HourForecastInterface; |
6
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotAuthorizedException; |
7
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotFoundException; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class HourForecastController. |
12
|
|
|
*/ |
13
|
|
|
class HourForecastController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\HourForecastInterface |
17
|
|
|
*/ |
18
|
|
|
protected $hourForecast; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* HourForecastController constructor. |
22
|
|
|
* |
23
|
|
|
* @param \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\HourForecastInterface $hourForecast |
24
|
|
|
*/ |
25
|
|
|
public function __construct(HourForecastInterface $hourForecast) |
26
|
|
|
{ |
27
|
|
|
$this->hourForecast = $hourForecast; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns hour forecast by city name. |
32
|
|
|
* |
33
|
|
|
* @param string $cityName |
34
|
|
|
* @param string $countryCode |
35
|
|
|
* |
36
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function getForecastByCityName($cityName, $countryCode = '') |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$response = new Response(); |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
$data = $this->hourForecast->fetchForecastByCityName($cityName, $countryCode); |
44
|
|
|
$response->setContent($data); |
45
|
|
|
} catch (NotAuthorizedException $e) { |
46
|
|
|
$response->setContent($e->getMessage()); |
47
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
48
|
|
|
} catch (NotFoundException $e) { |
49
|
|
|
$response->setContent($e->getMessage()); |
50
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $response; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns hour forecast by city id. |
58
|
|
|
* |
59
|
|
|
* @param int $cityId |
60
|
|
|
* |
61
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function getForecastByCityId($cityId) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$response = new Response(); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$data = $this->hourForecast->fetchForecastByCityId($cityId); |
69
|
|
|
$response->setContent($data); |
70
|
|
|
} catch (NotAuthorizedException $e) { |
71
|
|
|
$response->setContent($e->getMessage()); |
72
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
73
|
|
|
} catch (NotFoundException $e) { |
74
|
|
|
$response->setContent($e->getMessage()); |
75
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $response; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns hour forecast by geographic coordinates. |
83
|
|
|
* |
84
|
|
|
* @param float $latitude |
85
|
|
|
* @param float $longitude |
86
|
|
|
* |
87
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function getForecastByCityGeographicCoordinates($latitude, $longitude) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$response = new Response(); |
92
|
|
|
|
93
|
|
|
try { |
94
|
|
|
$data = $this->hourForecast->fetchForecastByCityGeographicCoordinates($latitude, $longitude); |
95
|
|
|
$response->setContent($data); |
96
|
|
|
} catch (NotAuthorizedException $e) { |
97
|
|
|
$response->setContent($e->getMessage()); |
98
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
99
|
|
|
} catch (NotFoundException $e) { |
100
|
|
|
$response->setContent($e->getMessage()); |
101
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $response; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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.