1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\OpenWeatherMapBundle\Controller; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\UltravioletIndexInterface; |
7
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotAuthorizedException; |
8
|
|
|
use Netgen\Bundle\OpenWeatherMapBundle\Exception\NotFoundException; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class UltravioletIndexController. |
13
|
|
|
*/ |
14
|
|
|
class UltravioletIndexController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\UltravioletIndexInterface |
18
|
|
|
*/ |
19
|
|
|
protected $ultravioletIndex; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* UltravioletIndexController constructor. |
23
|
|
|
* |
24
|
|
|
* @param \Netgen\Bundle\OpenWeatherMapBundle\API\OpenWeatherMap\Weather\UltravioletIndexInterface $ultravioletIndex |
25
|
|
|
*/ |
26
|
|
|
public function __construct(UltravioletIndexInterface $ultravioletIndex) |
27
|
|
|
{ |
28
|
|
|
$this->ultravioletIndex = $ultravioletIndex; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns ultraviolet index. |
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 getUltravioletIndex($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->ultravioletIndex->fetchUltravioletIndex($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
|
|
|
|
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.