Weather   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 13.64%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 74
c 1
b 0
f 0
dl 0
loc 114
ccs 9
cts 66
cp 0.1364
rs 10
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getWeatherMultiCurl() 0 43 9
A setApiKeys() 0 4 1
B getWeather() 0 33 6
A getLocationData() 0 14 2
1
<?php
2
namespace Anax\Model;
3
class Weather
4
{
5
    protected $locationiqKey;
6
    protected $darkskyKey;
7
8 4
    public function setApiKeys($locationiqKey, $darkskyKey)
9
    {
10 4
        $this->locationiqKey = $locationiqKey;
11 4
        $this->darkskyKey = $darkskyKey;
12 4
    }
13
    /**
14
     *
15
     * @param $when -> 7 days in future, or 30 days in the past, $lat -> Latitude, $long -> Longitude
0 ignored issues
show
Documentation Bug introduced by
The doc comment -> at position 0 could not be parsed: Unknown type name '-' at position 0 in ->.
Loading history...
16
     * @return array of past weather
17
     *
18
     */
19
20 4
    public function getLocationData($search) : array
21
    {
22 4
        $url = "https://eu1.locationiq.com/v1/search.php?key={$this->locationiqKey}&q={$search}&format=json&limit=1";
23 4
        $response = get_headers($url);
24 4
        if ($response[0] === 'HTTP/1.1 200 OK') {
25
                $details = json_decode(file_get_contents($url));
26
                return [
27
                    "lat" => $details[0]->lat,
28
                    "lon"=> $details[0]->lon,
29
                    "error" => null,
30
                ];
31
        } else {
32
            return [
33 4
                "error" => $search . " is not a city",
34
            ];
35
        }
36
37
    }
38
    public function getWeather($when, $lat, $lon) : array
39
    {
40
        $now = time();
41
        $dates = array();
42
        $weather = array();
43
        $temp = array();
44
        $time = array();
45
        if ($when == "past") {
46
            for ($i = 0; $i < 30; $i++) {
47
                $now -= 86400;
48
                $dates[] = $now;
49
            }
50
        } elseif ($when == "future") {
51
            for ($i = 0; $i < 7; $i++) {
52
                $now += 86400;
53
                $dates[] = $now;
54
            }
55
        } else {
56
            return [
57
                "error" => "select a time",
58
            ];
59
        }
60
        for ($i = 0; $i < count($dates); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
61
            $details = json_decode(file_get_contents("https://api.darksky.net/forecast/{$this->darkskyKey}/{$lon},{$lat},{$dates[$i]}?lang=sv&units=si"));
62
            $time[] = date('Y-m-d', $details->currently->time);
63
            $weather[] = $details->currently->summary;
64
            $temp[] = $details->currently->temperature;
65
        }
66
        return [
67
            "time" => $time,
68
            "weather" => $weather,
69
            "temp" => $temp,
70
            "error" => "no error",
71
        ];
72
    }
73
74
    public function getWeatherMultiCurl($when, $lat, $lon) : array
75
    {
76
        $dates = [];
77
        $now = time();
78
        $url = "https://api.darksky.net/forecast/{$this->darkskyKey}/{$lon},{$lat}";
79
80
        if ($when == "past") {
81
            for ($i = 0; $i < 30; $i++) {
82
                $now -= 86400;
83
                $dates[] = $now;
84
            }
85
        } elseif($when == "future") {
86
            for ($i = 0; $i < 7; $i++) {
87
            $now += 86400;
88
            $dates[] = $now;
89
        }
90
        } else {
91
        return [
92
                "error" => "select a time",
93
        ];
94
    }
95
96
        $mh = curl_multi_init();
97
        $chAll = [];
98
        foreach ($dates as $days) {
99
            $ch = curl_init("$url,{$days}?lang=sv&units=si");
100
            curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true]);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() 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

100
            curl_setopt_array(/** @scrutinizer ignore-type */ $ch, [CURLOPT_RETURNTRANSFER => true]);
Loading history...
101
            curl_multi_add_handle($mh, $ch);
0 ignored issues
show
Bug introduced by
It seems like $ch 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

101
            curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
102
            $chAll[] = $ch;
103
        }
104
        $running = null;
105
        do {
106
            curl_multi_exec($mh, $running);
107
        } while ($running);
108
        foreach ($chAll as $ch) {
109
            curl_multi_remove_handle($mh, $ch);
110
        }
111
        $response = [];
112
        foreach ($chAll as $ch) {
113
            $data = curl_multi_getcontent($ch);
114
            $response[] = json_decode($data, true);
115
        }
116
        return $response;
117
    }
118
}
119