Weather   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 73
ccs 25
cts 25
cp 1
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getWeather() 0 39 7
A __construct() 0 4 1
1
<?php
2
3
namespace EVB\Weather;
4
5
/**
6
 * Class for fetching weather data from an API.
7
 */
8
class Weather
9
{
10
    /**
11
     * The configured base url for all requests.
12
     *
13
     * @var string
14
     */
15
    private $baseUrl;
16
    /**
17
     * A MultiCurlInterface used to execute all requests.
18
     *
19
     * @var MultiCurlInterface
20
     */
21
    private $multiCurl;
22
23
    /**
24
     * Initialize a Weather object with config.
25
     *
26
     * @param string $baseUrl
27
     * @param MultiCurlInterface $curl
28
     */
29 3
    public function __construct(string $baseUrl, MultiCurlInterface $curl)
30
    {
31 3
        $this->baseUrl = $baseUrl;
32 3
        $this->multiCurl = $curl;
33 3
    }
34
35
    /**
36
     * Gets weather data for specified coordinates.
37
     *
38
     * @param string $latitude
39
     * @param string $longitude
40
     * @return mixed
41
     */
42 3
    public function getWeather(string $latitude, string $longitude)
43
    {
44 3
        $dateFormat = "Y-m-d";
45 3
        $timeFormat = "H:i:s";
46
47 3
        $dates = [];
48
49 3
        for ($day = -1; $day >= -30; $day--) {
50 3
            $dates[] = \date($dateFormat, \strtotime("$day days"));
51
        }
52
53 3
        foreach ($dates as $i => $date) {
54 3
            $dates[$i] = $date . "T" . \date($timeFormat);
55
        }
56
57
        $urls = [
58 3
            $latitude . "," . $longitude
59
        ];
60
61 3
        foreach ($dates as $date) {
62 3
            $urls[] = \implode(",", [$latitude, $longitude, $date]);
63
        }
64
65 3
        foreach ($urls as $i => $url) {
66 3
            $urls[$i] = $this->baseUrl
67 3
                . $url
68 3
                . "?lang=sv&units=si";
69
        }
70
71 3
        $result = $this->multiCurl->execute($urls);
72
73 3
        if (\array_key_exists("error", $result[0])) {
74 2
            if ($result[0]["code"] == 400) {
75 1
                return "Ogiltiga koordinater.";
76
            }
77 1
            return "Det verkar som att dagens förfrågningar har tagit slut. Använd \"Sök med exempel-väder\".";
78
        }
79
80 1
        return $result;
81
    }
82
}
83