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