Weather   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 41
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fetchWeather() 0 10 2
A parseCoordinates() 0 20 4
1
<?php
2
namespace Oliver\Weather;
3
4
use Oliver\Weather\WeatherServiceInterface;
5
use Oliver\Weather\Exception\BadFormatException;
6
7
/**
8
 *
9
 */
10
class Weather
11
{
12
    private $service;
13
14 11
    public function __construct(WeatherServiceInterface $service)
15
    {
16 11
        $this->service = $service;
17 11
    }
18
19 11
    public function parseCoordinates($coordinateString) : array
20
    {
21 11
        $values = explode(",", $coordinateString);
22
23 11
        if (count($values) != 2) {
24 4
            throw new BadFormatException("Värdet var fel formaterat!");
25
        }
26
27 7
        $lat = $values[0];
28 7
        $long = $values[1];
29
30 7
        if (!floatval($lat)) {
31 1
            throw new BadFormatException("Latituden <b>$lat</b> har fel format.");
32
        }
33 6
        if (!floatval($long)) {
34 1
            throw new BadFormatException("Longituden <b>$long</b> har fel format.");
35
        }
36
37 5
        $coordinates = ["lat" => $lat, "long" => $long];
38 5
        return $coordinates;
39
    }
40
41 11
    public function fetchWeather(string $coordinateString, string $option) : array
42
    {
43 11
        $coordinates = $this->parseCoordinates($coordinateString);
44
45 5
        if ($option == "history") {
46 2
            $weatherJSON = $this->service->history($coordinates, 30);
47 2
            return $weatherJSON;
48
        } else {
49 3
            $weatherJSON = $this->service->forecast($coordinates);
50 3
            return $weatherJSON[0];
51
        }
52
    }
53
}
54