Test Setup Failed
Push — master ( 860cfe...01dec1 )
by Lydia
02:35
created

GeoTag::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace Anax\Model;
3
4
class GeoTag
5
{
6
7
    private $config;
8
9
    public function __construct($config)
10
    {
11
        $this->config = $config;
12
    }
13
14
    public function setConfig($config)
15
    {
16
        $this->config = $config;
17
    }
18
19
    /**
20
     *
21
     * @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...
22
     * @return array of weather using file_get_contents()
23
     *
24
     */
25
    public function getWeather($when, $lat, $long) : array
26
    {
27
        $now = time();
28
        $dates = array();
29
        $weather = array();
30
        $temp = array();
31
        $time = array();
32
33
        if ($when == "past") {
34
            for ($i = 0; $i < 30; $i++) {
35
                // 24h = 86400 unix time
36
                $now -= 86400;
37
                $dates[] = $now;
38
            }
39
        } else {
40
            for ($i = 0; $i < 7; $i++) {
41
                // 24h = 86400 unix time
42
                $now += 86400;
43
                $dates[] = $now;
44
            }
45
        }
46
47
        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...
48
            $details =      json_decode(file_get_contents("https://api.darksky.net/forecast/{$this->config}/{$lat},{$long},{$dates[$i]}?lang=sv&units=si"));
49
50
            $time[] = $details->currently->time;
51
            $weather[] = $details->currently->summary;
52
            $temp[] = $details->currently->temperature;
53
        }
54
55
        return [
56
            "time" => $time,
57
            "weather" => $weather,
58
            "temp" => $temp,
59
        ];
60
    }
61
62
63
    /**
64
     *
65
     * @param $id -> time id of weather 7 days in future,
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...
66
     * @param $lat -> Latitude,
67
     * @param $long -> Longitude,
68
     * @return array of weather using multicurl
69
     *
70
     */
71
    public function getWeatherMultiCurl($when, $lat, $long) : array
72
    {
73
        $dates = [];
74
        $now = time();
75
76
        if ($when == "past") {
77
            for ($i = 0; $i < 30; $i++) {
78
                // 24h = 86400 unix time
79
                $now -= 86400;
80
                $dates[] = $now;
81
            }
82
        } else {
83
            for ($i = 0; $i < 7; $i++) {
84
                // 24h = 86400 unix time
85
                $now += 86400;
86
                $dates[] = $now;
87
            }
88
        }
89
90
        $url = "https://api.darksky.net/forecast/{$this->config->config}/{$lat},{$long}";
91
92
        $options = [
93
            CURLOPT_RETURNTRANSFER => true,
94
        ];
95
96
        $mh = curl_multi_init();
97
        $chAll = [];
98
        foreach ($dates as $day) {
99
            $ch = curl_init("$url,{$day}?lang=sv&units=si");
100
            curl_setopt_array($ch, $options);
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, $options);
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
105
        // execute all queries simultaneously and countinue when all are complete
106
        $running = null;
107
        do {
108
            curl_multi_exec($mh, $running);
109
        } while ($running);
110
111
        // Close handle
112
        foreach ($chAll as $ch) {
113
            curl_multi_remove_handle($mh, $ch);
114
        }
115
        curl_multi_close($mh);
116
117
        //req are done, access results
118
        $response = [];
119
        foreach ($chAll as $ch) {
120
            $data = curl_multi_getcontent($ch);
121
            $response[] = json_decode($data, true);
122
123
            // $weather[] = $response->currently->summary;
124
            // $temp[] = $response->currently->temperature;
125
        }
126
        return $response;
127
    }
128
}
129