1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\OpenWeatherMapBundle\Controller; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\AirPollutionInterface; |
7
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotAuthorizedException; |
8
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotFoundException; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AirPollutionController. |
13
|
|
|
*/ |
14
|
|
|
class AirPollutionController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\AirPollutionInterface |
18
|
|
|
*/ |
19
|
|
|
protected $airPollution; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* AirPollutionController constructor. |
23
|
|
|
* |
24
|
|
|
* @param \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\AirPollutionInterface $airPollution |
25
|
|
|
*/ |
26
|
|
|
public function __construct(AirPollutionInterface $airPollution) |
27
|
|
|
{ |
28
|
|
|
$this->airPollution = $airPollution; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns ozone data. |
33
|
|
|
* |
34
|
|
|
* @param float $latitude |
35
|
|
|
* @param float $longitude |
36
|
|
|
* @param string $datetime ISO 8601 date string |
37
|
|
|
* |
38
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function getOzoneData($latitude, $longitude, $datetime = 'current') |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
if ($datetime !== 'current') { |
43
|
|
|
$datetime = DateTime::createFromFormat('c', $datetime); |
44
|
|
|
|
45
|
|
|
if ($datetime === false) { |
46
|
|
|
$datetime = 'current'; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$response = new Response(); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$data = $this->airPollution->fetchOzoneData($latitude, $longitude, $datetime); |
54
|
|
|
$response->setContent($data); |
55
|
|
|
} catch (NotAuthorizedException $e) { |
56
|
|
|
$response->setContent($e->getMessage()); |
57
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
58
|
|
|
} catch (NotFoundException $e) { |
59
|
|
|
$response->setContent($e->getMessage()); |
60
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $response; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns carbon monoxide data. |
68
|
|
|
* |
69
|
|
|
* @param float $latitude |
70
|
|
|
* @param float $longitude |
71
|
|
|
* @param string $datetime ISO 8601 date string |
72
|
|
|
* |
73
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function getCarbonMonoxideData($latitude, $longitude, $datetime = 'current') |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
if ($datetime !== 'current') { |
78
|
|
|
$datetime = DateTime::createFromFormat('c', $datetime); |
79
|
|
|
|
80
|
|
|
if ($datetime === false) { |
81
|
|
|
$datetime = 'current'; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$response = new Response(); |
86
|
|
|
|
87
|
|
|
try { |
88
|
|
|
$data = $this->airPollution->fetchCarbonMonoxideData($latitude, $longitude, $datetime); |
89
|
|
|
$response->setContent($data); |
90
|
|
|
} catch (NotAuthorizedException $e) { |
91
|
|
|
$response->setContent($e->getMessage()); |
92
|
|
|
$response->setStatusCode(Response::HTTP_UNAUTHORIZED); |
93
|
|
|
} catch (NotFoundException $e) { |
94
|
|
|
$response->setContent($e->getMessage()); |
95
|
|
|
$response->setStatusCode(Response::HTTP_NOT_FOUND); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $response; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.