Weather::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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