UltravioletIndexController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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