Passed
Push — main ( f0346b...1f692e )
by Aron
02:39
created

OpenWeather   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 63
c 4
b 0
f 0
dl 0
loc 126
ccs 65
cts 65
cp 1
rs 10
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A currentweather() 0 10 1
A forecast() 0 10 1
A historicweather() 0 10 2
A getHandles() 0 11 2
A mymulticurl() 0 24 6
A generateTimestamps() 0 12 3
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 6
    public function currentweather() : array
30
    {
31 6
        $ch = curl_init();
32 6
        $url = "https://api.openweathermap.org/data/2.5/weather?lat=" . $this->lat . "&lon=" . $this->long . "&appid=" . $this->weatherkey . "&units=metric&lang=se";
33 6
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
34 6
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, 1);
35 6
        $apiresponse = curl_exec(/** @scrutinizer ignore-type */ $ch);
36
37 6
        $jsonresp = json_decode($apiresponse, /** @scrutinizer ignore-type */ JSON_UNESCAPED_UNICODE);
38 6
        return $jsonresp;
39
    }
40
41 6
    public function forecast() : array
42
    {
43 6
        $ch = curl_init();
44 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";
45 6
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
46 6
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, 1);
47 6
        $apiresponse = curl_exec(/** @scrutinizer ignore-type */ $ch);
48
49 6
        $jsonresp = json_decode($apiresponse, /** @scrutinizer ignore-type */ JSON_UNESCAPED_UNICODE);
50 6
        return $jsonresp;
51
    }
52
53
    // multiple curls serially
54
    // public function historicweather() : array
55
    // {
56
    //     $days = $this->generateTimestamps();
57
    //     $result = [];
58
    //     $mh = curl_multi_init();
59
    //     for ($i = 0; $i < count($days); $i++) {
60
    //         $ch = curl_init();
61
    //         $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";
62
    //         curl_setopt($ch, CURLOPT_URL, $url);
63
    //         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
64
    //         $apiresponse = curl_exec($ch);
65
    //         $jsonresp = json_decode($apiresponse, JSON_UNESCAPED_UNICODE);
66
    //         $result[] = $jsonresp;
67
    //         curl_close ($ch);
68
    //     }
69
    //     return $result;
70
    // }
71
72
    // multiple curls parallelly
73 6
    public function historicweather() : array
74
    {
75 6
        $days = $this->generateTimestamps();
76 6
        $urls = [];
77 6
        $mycount = count($days);
78 6
        for ($i = 0; $i < $mycount; $i++) {
79 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";
80
        }
81 6
        $result = $this->mymulticurl($urls);
82 6
        return $result;
83
    }
84
85 6
    private function getHandles($urls, $multi)
86
    {
87 6
        $handles = [];
88 6
        foreach ($urls as $url) {
89 6
            $ch = curl_init($url);
90 6
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_HEADER, false);
91 6
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
92 6
            curl_multi_add_handle($multi, /** @scrutinizer ignore-type */ $ch);
93 6
            $handles[$url] = $ch;
94
        }
95 6
        return [$handles, $multi];
96
    }
97
98 6
    private function mymulticurl($urls)
99
    {
100 6
        $result = [];
101 6
        $multi = curl_multi_init();
102 6
        $output = $this->getHandles($urls, $multi);
103 6
        $handles =  $output[0];
104 6
        $multi = $output[1];
105
        do {
106 6
            $mrc = curl_multi_exec($multi, $active);
107 6
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
108
109 6
        while ($active && $mrc == CURLM_OK) {
110
            do {
111 6
                $mrc = curl_multi_exec($multi, $active);
112 6
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
113
        }
114 6
        foreach ($handles as $channel) {
115 6
            $html = curl_multi_getcontent($channel);
116 6
            $jsonresp = json_decode($html, /** @scrutinizer ignore-type */ JSON_UNESCAPED_UNICODE);
117 6
            $result[] = $jsonresp;
118 6
            curl_multi_remove_handle($multi, $channel);
119
        }
120 6
        curl_multi_close($multi);
121 6
        return $result;
122
    }
123
124 6
    private function generateTimestamps($timestamp = null) : array
125
    {
126 6
        if (!$timestamp) {
127 6
            $timestamp = time();
128
        }
129 6
        $day = 60*60*24;
130 6
        $result = array();
131 6
        for ($i = 0; $i < 5; $i++) {
132 6
            $timestamp -= $day;
133 6
            array_push($result, $timestamp);
134
        }
135 6
        return $result;
136
    }
137
}
138