Issues (1933)

src/models/WeatherHandler.php (6 issues)

Labels
Severity
1
<?php
2
3
namespace Ylvan\Models;
4
5
/**
6
 * A class to handle IP
7
 * @SuppressWarnings(PHPMD.LongVariable)
8
 * @SuppressWarnings(PHPMD.ShortVariable)
9
 */
10
class WeatherHandler
11
{
12
    private $accessKey='31e4a45c184fb9ee516a7e276edafb79';
13
14
15
    /**
16
     * get pressent weather data
17
     */
18
    public function getWeather($lat, $long) : array
19
    {
20
        // $searchParam = '?lat=' . $lat . '&lon=' . $long;
21
        return $this->getResponse($lat, $long);
22
    }
23
    
24
    /**
25
     * get historical data for one month back
26
     */
27
    public function getHistoryWeather($lat, $long) : array
28
    {
29
        return $this->getHistoricalResponse($lat, $long);
30
    }
31
32
    
33
    /**
34
     * get forecast data for 5 days
35
     */
36
    public function getForecastWeather($lat, $long) : array
37
    {
38
        return $this->getForecastResponse($lat, $long);
39
    }
40
41
42
    /**
43
     * get weather data in chosen position from API
44
     */
45
    private function getResponse($lat, $long) : array
46
    {
47
        // $accessKey = '31e4a45c184fb9ee516a7e276edafb79';
48
        $key = $this->accessKey;
49
        $url = 'http://api.openweathermap.org/data/2.5/weather';
50
        
51
        try {
52
            $response = file_get_contents($url . '?lat=' . $lat . '&lon=' . $long . '&appid=' . $key . '&units=metric');
53
54
            $apiResult = json_decode($response, true);
55
56
            return $apiResult;
57
        } catch (\Throwable $th) {
58
            return ["could not connect to openweathermap"];
59
        }
60
    }
61
62
    /**
63
     * get weather forecast for one week
64
     */
65
    private function getForecastResponse($lat, $long) : array
66
    {
67
        //openweathermap documentation
68
        // https://openweathermap.org/api/one-call-api
69
70
        $url = 'https://api.openweathermap.org/data/2.5/onecall';
71
        // exclude params: current,minutely,hourly,daily,alerts
72
        $key = $this->accessKey;
73
74
        try {
75
            // Initialize CURL:
76
            $ch = curl_init($url . '?lat=' . $lat . '&lon=' . $long . '&exclude=current,minutely,hourly,alerts' . '&appid=' . $key . '&units=metric');
77
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
78
79
            // Store the data:
80
            $json = curl_exec($ch);
81
            curl_close($ch);
82
83
            // Decode JSON response:
84
            $apiResult = json_decode($json, true);
0 ignored issues
show
It seems like $json can also be of type true; however, parameter $json of json_decode() does only seem to accept string, 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

84
            $apiResult = json_decode(/** @scrutinizer ignore-type */ $json, true);
Loading history...
85
86
            return $apiResult;
87
        } catch (\Throwable $th) {
88
            return ["could not connect to openweathermap"];
89
        }
90
    }
91
92
    /**
93
     * get weather data of the last 5 days with multi-curl
94
     */
95
    private function getHistoricalResponse($lat, $long) : array
96
    {
97
        //openweathermap documentation
98
        // https://openweathermap.org/api/one-call-api#history
99
        $key = $this->accessKey;
100
        $url = 'http://api.openweathermap.org/data/2.5/onecall/timemachine';
101
        $options = [
102
            CURLOPT_RETURNTRANSFER => true,
103
        ];
104
        $currentTime = time();
105
        $day1 = ($currentTime - ((24*60*60)*1));
106
        $day2 = ($currentTime - ((24*60*60)*2));
107
        $day3 = ($currentTime - ((24*60*60)*3));
108
        $day4 = ($currentTime - ((24*60*60)*4));
109
        $day5 = ($currentTime - ((24*60*60)*5));
110
        $timeList = [$currentTime, $day1, $day2, $day3, $day4, $day5];
111
112
113
        // Add all curl handlers and remember them
114
        // Initiate the multi curl handler
115
        $mh = curl_multi_init();
116
        $chAll = [];
117
        try {
118
            foreach ($timeList as $time) {
119
                $ch = curl_init($url . '?lat=' . $lat . '&lon=' . $long . '&dt=' . $time . '&appid=' . $key . '&units=metric');
120
                curl_setopt_array($ch, $options);
121
                curl_multi_add_handle($mh, $ch);
0 ignored issues
show
It seems like $mh can also be of type true; however, parameter $multi_handle of curl_multi_add_handle() does only seem to accept CurlMultiHandle|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

121
                curl_multi_add_handle(/** @scrutinizer ignore-type */ $mh, $ch);
Loading history...
122
                $chAll[] = $ch;
123
            }
124
125
            // Execute all queries simultaneously,
126
            // and continue when all are complete
127
            $running = null;
128
            do {
129
                curl_multi_exec($mh, $running);
0 ignored issues
show
It seems like $mh can also be of type true; however, parameter $multi_handle of curl_multi_exec() does only seem to accept CurlMultiHandle|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

129
                curl_multi_exec(/** @scrutinizer ignore-type */ $mh, $running);
Loading history...
130
            } while ($running);
131
132
            // Close the handles
133
            foreach ($chAll as $ch) {
134
                curl_multi_remove_handle($mh, $ch);
0 ignored issues
show
It seems like $mh can also be of type true; however, parameter $multi_handle of curl_multi_remove_handle() does only seem to accept CurlMultiHandle|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

134
                curl_multi_remove_handle(/** @scrutinizer ignore-type */ $mh, $ch);
Loading history...
135
            }
136
            curl_multi_close($mh);
0 ignored issues
show
It seems like $mh can also be of type true; however, parameter $multi_handle of curl_multi_close() does only seem to accept CurlMultiHandle|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

136
            curl_multi_close(/** @scrutinizer ignore-type */ $mh);
Loading history...
137
138
            // All of our requests are done, access the results
139
            $response = [];
140
            foreach ($chAll as $ch) {
141
                $data = curl_multi_getcontent($ch);
142
                $response[] = json_decode($data, true);
143
            }
144
145
            return $response;
146
        } catch (\Throwable $th) {
147
            return ["could not connect to openweathermap"];
148
        }
149
    }
150
151
152
     
153
    /**
154
     * get pressent weather map
155
     */
156
    public function getWeatherMap($lat, $long) : array
157
    {
158
        // $searchParam = '?lat=' . $lat . '&lon=' . $long;
159
        return $this->requestWeatheMap($lat, $long);
160
    }
161
162
163
    
164
    /**
165
     * get weather forecast for one week
166
     */
167
    private function requestWeatheMap($lat, $long) : array
168
    {
169
        //openweathermap documentation
170
        // https://openweathermap.org/api/weathermaps
171
172
        $url = 'https://tile.openweathermap.org/map/';
173
        $key = $this->accessKey;
174
175
        // {layer}/{z}/{x}/{y}.png?appid={API key}
176
        $params = 'clouds_new/3/'.$lat.'/'.$long.'.png?';
177
178
        try {
179
            // Initialize CURL:
180
            $ch = curl_init($url . $params . '&appid=' . $key);
181
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
182
183
            // Store the data:
184
            $json = curl_exec($ch);
185
            curl_close($ch);
186
187
            // Decode JSON response:
188
            $apiResult = json_decode($json, true);
0 ignored issues
show
It seems like $json can also be of type true; however, parameter $json of json_decode() does only seem to accept string, 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

188
            $apiResult = json_decode(/** @scrutinizer ignore-type */ $json, true);
Loading history...
189
190
            return $apiResult;
191
        } catch (\Throwable $th) {
192
            return ["could not connect to openweathermap"];
193
        }
194
    }
195
}
196