Weather   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 29
c 1
b 0
f 0
dl 0
loc 46
ccs 28
cts 28
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setApiKey() 0 4 1
A getWeather() 0 35 4
1
<?php
2
3
namespace Anax\Weather;
4
5
class Weather
6
{
7
    private $key;
8
9 9
    public function setApiKey($key)
10
    {
11 9
        $this->key = $key["config"]["key"];
12 9
        return $this;
13
    }
14
15
16 7
    public function getWeather($lat, $lon)
17
    {
18
19 7
        $muH = curl_multi_init();
20 7
        $res = [];
21 7
        $handles = [];
22 7
        $handles[0] = curl_init();
23 7
        curl_setopt($handles[0], CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=hourly,minutely&appid={$this->key}&units=metric&lang=sv");
0 ignored issues
show
Bug introduced by
It seems like $handles[0] can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
        curl_setopt(/** @scrutinizer ignore-type */ $handles[0], CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=hourly,minutely&appid={$this->key}&units=metric&lang=sv");
Loading history...
24 7
        curl_setopt($handles[0], CURLOPT_RETURNTRANSFER, true);
25 7
        curl_setopt($handles[0], CURLOPT_HEADER, false);
26 7
        curl_multi_add_handle($muH, $handles[0]);
0 ignored issues
show
Bug introduced by
It seems like $handles[0] can also be of type false; however, parameter $ch of curl_multi_add_handle() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        curl_multi_add_handle($muH, /** @scrutinizer ignore-type */ $handles[0]);
Loading history...
27
28 7
        for ($i = 1; $i < 6; $i++) {
29 7
            $handles[$i] = curl_init();
30 7
            $days = "-${i} days";
31 7
            $time = date(strtotime($days));
32 7
            curl_setopt($handles[$i], CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=${lat}&lon=${lon}&dt=${time}&exclude=hourly,minutely&appid={$this->key}&units=metric&lang=sv");
33 7
            curl_setopt($handles[$i], CURLOPT_RETURNTRANSFER, true);
34 7
            curl_setopt($handles[$i], CURLOPT_HEADER, false);
35 7
            curl_multi_add_handle($muH, $handles[$i]);
36
        }
37
38 7
        $running = 0;
39
        do {
40 7
            \curl_multi_exec($muH, $running);
41 7
        } while ($running > 0);
42
43 7
        foreach ($handles as $i => $handle) {
44 7
            $res[$i] = \json_decode(\curl_multi_getcontent($handle), true);
45 7
            \curl_multi_remove_handle($muH, $handle);
46
        }
47
48 7
        \curl_multi_close($muH);
49
50 7
        return $res;
51
    }
52
}
53