Passed
Push — main ( 3ae92f...3d8e8a )
by Aron
02:57
created

OpenWeather::curlcall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
3
namespace artes\Weather;
4
5
/**
6
  * A class for OpenWeatherMap.
7
  *
8
  * @SuppressWarnings(PHPMD)
9
  */
10
class OpenWeather
11
{
12
    private $weatherkey;
13
    private $lat;
14
    private $long;
15
16
    /**
17
     * Constructor to initiate an OpenWeather object,
18
     *
19
     * @param string $userinput
20
     *
21
     */
22 8
    public function __construct(string $weatherkey, string $lat, string $long)
23
    {
24 8
        $this->weatherkey = $weatherkey;
25 8
        $this->lat = $lat;
26 8
        $this->long = $long;
27 8
    }
28
29 7
    public function curlcall($url)
30
    {
31 7
        $ch = curl_init();
32 7
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
33 7
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, 1);
34 7
        $apiresponse = curl_exec(/** @scrutinizer ignore-type */ $ch);
35
36 7
        $jsonresp = json_decode($apiresponse, /** @scrutinizer ignore-type */ JSON_UNESCAPED_UNICODE);
37 7
        return $jsonresp;
38
    }
39
40 6
    public function currentweather() : array
41
    {
42 6
        $url = "https://api.openweathermap.org/data/2.5/weather?lat=" . $this->lat . "&lon=" . $this->long . "&appid=" . $this->weatherkey . "&units=metric&lang=se";
43 6
        $jsonresp = $this->curlcall($url);
44 6
        return $jsonresp;
45
    }
46
47 6
    public function forecast() : array
48
    {
49 6
        $url = "https://api.openweathermap.org/data/2.5/onecall?lat=" . $this->lat . "&lon=" . $this->long . "&exclude=minutely,hourly&appid=" . $this->weatherkey . "&units=metric&lang=se";
50 6
        $jsonresp = $this->curlcall($url);
51 6
        return $jsonresp;
52
    }
53
54
    // multiple curls serially
55
    // public function historicweather() : array
56
    // {
57
    //     $days = $this->generateTimestamps();
58
    //     $result = [];
59
    //     $mh = curl_multi_init();
60
    //     for ($i = 0; $i < count($days); $i++) {
61
    //         $ch = curl_init();
62
    //         $url = "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=" . $this->lat . "&lon=" . $this->long .  "&dt=" . $days[$i]. "&appid=" . $this->weatherkey . "&units=metric&lang=se";
63
    //         curl_setopt($ch, CURLOPT_URL, $url);
64
    //         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
65
    //         $apiresponse = curl_exec($ch);
66
    //         $jsonresp = json_decode($apiresponse, JSON_UNESCAPED_UNICODE);
67
    //         $result[] = $jsonresp;
68
    //         curl_close ($ch);
69
    //     }
70
    //     return $result;
71
    // }
72
73
    // multiple curls parallelly
74 6
    public function historicweather() : array
75
    {
76 6
        $days = $this->generateTimestamps();
77 6
        $urls = [];
78 6
        $mycount = count($days);
79 6
        for ($i = 0; $i < $mycount; $i++) {
80 6
            $urls[] = "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=" . $this->lat . "&lon=" . $this->long .  "&dt=" . $days[$i]. "&appid=" . $this->weatherkey . "&units=metric&lang=se";
81
        }
82 6
        $result = $this->mymulticurl($urls);
83 6
        return $result;
84
    }
85
86 6
    private function getHandles($urls, $multi)
87
    {
88 6
        $handles = [];
89 6
        foreach ($urls as $url) {
90 6
            $ch = curl_init($url);
91 6
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HEADER, false);
92 6
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
93 6
            curl_multi_add_handle($multi, /** @scrutinizer ignore-type */ $ch);
94 6
            $handles[$url] = $ch;
95
        }
96 6
        return [$handles, $multi];
97
    }
98
99 6
    private function mymulticurl($urls)
100
    {
101 6
        $result = [];
102 6
        $multi = curl_multi_init();
103 6
        $output = $this->getHandles($urls, $multi);
104 6
        $handles =  $output[0];
105 6
        $multi = $output[1];
106
        do {
107 6
            $mrc = curl_multi_exec($multi, $active);
108 6
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
109
110 6
        while ($active && $mrc == CURLM_OK) {
111
            do {
112 6
                $mrc = curl_multi_exec($multi, $active);
113 6
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
114
        }
115 6
        foreach ($handles as $channel) {
116 6
            $html = curl_multi_getcontent($channel);
117 6
            $jsonresp = json_decode($html, /** @scrutinizer ignore-type */ JSON_UNESCAPED_UNICODE);
118 6
            $result[] = $jsonresp;
119 6
            curl_multi_remove_handle($multi, $channel);
120
        }
121 6
        curl_multi_close($multi);
122 6
        return $result;
123
    }
124
125 6
    private function generateTimestamps($timestamp = null) : array
126
    {
127 6
        if (!$timestamp) {
128 6
            $timestamp = time();
129
        }
130 6
        $day = 60*60*24;
131 6
        $result = array();
132 6
        for ($i = 0; $i < 5; $i++) {
133 6
            $timestamp -= $day;
134 6
            array_push($result, $timestamp);
135
        }
136 6
        return $result;
137
    }
138
}
139