OpenWeather::historicweather()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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