Completed
Push — master ( 7a4076...5ffb7f )
by Magnus
07:47
created

VaderHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Test Coverage

Coverage 97.73%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 87
c 1
b 0
f 0
dl 0
loc 265
ccs 86
cts 88
cp 0.9773
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkWeather2() 0 30 2
A checkCoordinates() 0 56 5
B checkWeather() 0 88 7
1
<?php
2
3
namespace Malm18\Vader;
4
5
class VaderHandler
6
{
7
8
9
    // public function parenting()
10
    // {
11
    //     return "I am your father";
12
    // }
13
14
15
16
17 6
    public function checkCoordinates($theIP)
18
    {
19 6
        if (filter_var($theIP, FILTER_VALIDATE_IP)) {
20 2
            $coordinates = [];
21 2
            $url = 'http://api.ipstack.com/';
22 2
            $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
23 2
            $apiKey = $keys["ipstackKey"];
24 2
            $requestUrl = $url . $theIP . '?access_key=' . $apiKey;
25 2
            $curl = curl_init($requestUrl);
26 2
            if ($curl) {
0 ignored issues
show
introduced by
$curl is of type false|resource, thus it always evaluated to false.
Loading history...
27 2
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
28
        // curl_setopt($curl, CURLOPT_HTTPHEADER, [
29
        //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
30
        //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
31
        //   'Content-Type: application/json'
32
        // ]);
33 2
            $response = curl_exec($curl);
34 2
            $response2 = json_decode($response, true);
35 2
            curl_close($curl);
36
37 2
            $coordinates['latitude'] = strval($response2['latitude']);
38 2
            $coordinates['longitude'] = strval($response2['longitude']);
39
            // echo $response . PHP_EOL;
40 2
            return $coordinates;
41
        }
42
        } else {
43 4
            $coordinates = [];
44
            // $response = array("type" => "not valid ip", "ip" => "", "latitude"=> "", "longitude"=> "",
45
            // "city" => "", "country_name" => "", "region_name" => "", "continent_name" => "", "location['country_code']" => "");
46
            // // $response2 = json_decode($response, true);
47
            // return $response;
48 4
            $url1 = 'https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=';
49
50
            // $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
51
            // $this->ipstackKey = $keys["ipstackKey"];
52
            // $apiKey = $this->ipstackKey;
53 4
            $requestUrl = $url1 . $theIP . '&format=json&limit=1&[email protected]';
54
            // $requestUrl = 'https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=bakery+in+berlin+wedding&format=json&limit=1&[email protected]';
55 4
            $curl = curl_init($requestUrl);
56 4
            if ($curl) {
0 ignored issues
show
introduced by
$curl is of type false|resource, thus it always evaluated to false.
Loading history...
57 4
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
58
        // curl_setopt($curl, CURLOPT_HTTPHEADER, [
59
        //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
60
        //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
61
        //   'Content-Type: application/json'
62
        // ]);
63 4
            $response = curl_exec($curl);
64
65 4
            if (strlen($response) > 10) {
66 2
                $response2 = json_decode($response, true);
67 2
                curl_close($curl);
68 2
                $coordinates['latitude'] = $response2[0]['lat'];
69 2
                $coordinates['longitude'] = $response2[0]['lon'];
70
            }
71
            // echo $response . PHP_EOL;
72 4
            return $coordinates;
73
        }
74
        }
75
    }
76
77
78
79
80
    // public function checkWeather($latitude, $longitude)
81
    // {
82
    //
83
    //     $url1 = 'https://api.darksky.net/forecast/';
84
    //
85
    //     $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
86
    //     $apiKey = $keys["darkskyKey"];
87
    //     $endStuff = '?exclude=minutely,hourly,currently,alerts,flags&extend=daily&lang=sv&units=si';
88
    //     $requestUrl = $url1 . $apiKey . "/" . $latitude . "," . $longitude . $endStuff;
89
    //     // $requestUrl = 'https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=bakery+in+berlin+wedding&format=json&limit=1&[email protected]';
90
    //     $curl = curl_init($requestUrl);
91
    //     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
92
    // // curl_setopt($curl, CURLOPT_HTTPHEADER, [
93
    // //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
94
    // //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
95
    // //   'Content-Type: application/json'
96
    // // ]);
97
    //     $response = curl_exec($curl);
98
    //     $response2 = json_decode($response, true);
99
    //     curl_close($curl);
100
    //     // $coordinates['latitude'] = $response2[0]['lat'];
101
    //     // $coordinates['longitude'] = $response2[0]['lon'];
102
    //     // echo $response . PHP_EOL;
103
    //     return $response2;
104
    //
105
    //     // $coordinates = "Latitud: " . $latitude . ", longitud: " . $longitude;
106
    //     // return $coordinates;
107
    // }
108
109
110
111 4
    public function checkWeather($latitude, $longitude, $pastOrFuture)
112
    {
113
114 4
        $url1 = 'https://api.darksky.net/forecast/';
115
116 4
        $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
117 4
        $apiKey = $keys["darkskyKey"];
118 4
        $endStuff = '?exclude=minutely,hourly,currently,alerts,flags&extend=daily&lang=sv&units=si';
119
120 4
        if ($pastOrFuture=="future") {
121 1
            $requestUrl = $url1 . $apiKey . "/" . $latitude . "," . $longitude . $endStuff;
122
            // $requestUrl = 'https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=bakery+in+berlin+wedding&format=json&limit=1&[email protected]';
123 1
            $curl = curl_init($requestUrl);
124 1
            if ($curl) {
0 ignored issues
show
introduced by
$curl is of type false|resource, thus it always evaluated to false.
Loading history...
125 1
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
126
        // curl_setopt($curl, CURLOPT_HTTPHEADER, [
127
        //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
128
        //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
129
        //   'Content-Type: application/json'
130
        // ]);
131 1
            $response = curl_exec($curl);
132 1
            $response2 = json_decode($response, true);
133 1
            curl_close($curl);
134
            // $coordinates['latitude'] = $response2[0]['lat'];
135
            // $coordinates['longitude'] = $response2[0]['lon'];
136
            // echo $response . PHP_EOL;
137 1
            return $response2['daily']['data'];
138
        }
139
        } else {
140
        // array of curl handles
141 3
            $multiCurl = array();
142
            // data to be returned
143 3
            $response = array();
144
            // multi handle
145 3
            $mhbutlonger = curl_multi_init();
146
147
148
149
150
        // for ($i=0; $i < 30; $i++) {
151
        //     $unixTime = time() - ($i * 24 * 60 * 60);
152
        //     $multiRequests[] = $url1 . $apiKey . "/" . $latitude . "," . $longitude . ','.$unixTime . $endStuff;
153
        // }
154
155
156
157
158
159 3
            for ($i=0; $i < 30; $i++) {
160 3
                $unixTime = time() - ($i * 24 * 60 * 60);
161 3
                $requestUrl = $url1 . $apiKey . "/" . $latitude . "," . $longitude . ','. $unixTime . $endStuff;
162 3
                $multiCurl[$i] = curl_init();
163 3
                curl_setopt($multiCurl[$i], CURLOPT_URL, $requestUrl);
164 3
                curl_setopt($multiCurl[$i], CURLOPT_HEADER, 0);
165 3
                curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER, 1);
166 3
                curl_multi_add_handle($mhbutlonger, $multiCurl[$i]);
167
            }
168 3
            $index=null;
169
            do {
170 3
                curl_multi_exec($mhbutlonger, $index);
171 3
            } while ($index > 0);
172
        // get content and remove handles
173 3
            foreach ($multiCurl as $k => $ch) {
174 3
                $response[$k] = curl_multi_getcontent($ch);
175 3
                curl_multi_remove_handle($mhbutlonger, $ch);
176
            }
177
            // close
178 3
            curl_multi_close($mhbutlonger);
179
180
            // $response2 = json_decode($response, true);
181
182
            // $daily = [];
183
            // $anArray = [];
184
            // $data = [];
185 3
            $data2 = [];
186 3
            $i=0;
187 3
            foreach ($response as $item) {
188 3
                $data = json_decode($item, true);
189 3
                $data2[$i] = $data['daily']['data'][0];
190
                // $data[$i] = $item;
191 3
                $i=$i+1;
192
            }
193
            // $daily['data'] = $data;
194
            // $anArray['daily'] = $daily;
195
196
            // return $data[0]['daily']['data'];
197
            // $response2 = json_decode($data, true);
198 3
            return $data2;
199
        }
200
    }
201
202
203
204
205
206
//     public function checkPastWeather($latitude, $longitude)
207
// {
208
//     $url1 = 'https://api.darksky.net/forecast/';
209
//
210
//     $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
211
//     $apiKey = $keys["darkskyKey"];
212
//     $endStuff = '?exclude=minutely,hourly,currently,alerts,flags&extend=daily&lang=sv&units=si';
213
//
214
//     $multiRequests = [];
215
//     #future weather
216
//     if ($this->time === "future") {
217
//         for ($i=0; $i < 7; $i++) {
218
//             $unixTime = time() + ($i * 24 * 60 * 60);
219
//             $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags';
220
//         }
221
//     }
222
//     #previous weather
223
//     if ($this->time === "past") {
224
//         for ($i=0; $i < 30; $i++) {
225
//             $unixTime = time() - ($i * 24 * 60 * 60);
226
//             $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags';
227
//         }
228
//     }
229
//     $weather = $this->requester->multiRequest($multiRequests);
230
//     foreach ($weather as $key => $value) {
231
//         $weather[$key] = json_decode(stripslashes($value), true);
232
//     }
233
//     return $weather;
234
// }
235
236
237
238
239
240 4
    public function checkWeather2($weather)
241
    {
242 4
        $weather2 = [];
243 4
        $locale = 'sv_SE.utf8';
244 4
        setlocale(LC_TIME, $locale);
245 4
        $i=0;
246 4
        foreach ($weather as $day) {
247
            // array_push($weather2, $day['time']);
248 4
            $unixTimestamp = $day['time'];
249 4
            $datetime = date('Y-m-d l', $unixTimestamp);
250 4
            $datetime2 = strftime('%A %d %B', strtotime($datetime));
251
252
            // $unixTimestamp = $day['time'];
253
            // $datetime = date('Y-m-d l', $unixTimestamp);
254
            // $datetime2 = strftime('%A %d %B', strtotime($datetime));
255
256
            // $datetime2 = $datetime->format('d/m');
257
            // $weather2[$i] = (['day'] => [$day]);
258 4
            $weather2[$i]['time'] = $datetime2;
259 4
            $weather2[$i]['summary'] = $day['summary'];
260 4
            $weather2[$i]['temperatureMin'] = round($day['temperatureMin']);
261 4
            $weather2[$i]['temperatureMax'] = round($day['temperatureMax']);
262 4
            $weather2[$i]['precipProbability'] = 100 * ($day['precipProbability']);
263
            // $weather2[$i]['precipType'] = $day['precipType'];
264 4
            $weather2[$i]['windSpeed'] = round($day['windSpeed']);
265 4
            $weather2[$i]['windBearing'] = $day['windBearing'];
266
            // array_push($weather2, $datetime2);
267 4
            $i=$i+1;
268
        }
269 4
        return $weather2;
270
    }
271
272
//     foreach($inputs['test']['order'] as $test){
273
//         echo $test;
274
//
275
// }
276
277
    // echo $yummy->toppings[2]->id
278
279
//     foreach($arr as $key => &$val){
280
//     $val['color'] = 'red';
281
// }
282
283
284
    // $unixTimestamp = $_POST['timestamp'];
285
    // $datetime = new DateTime("@$unixTimestamp");
286
    // // Display GMT datetime
287
    // echo $datetime->format('d-m-Y H:i:s');
288
289
290
    // public function minLong($longitude)
291
    // {
292
    //
293
    //     $minLong = floatval($longitude)-0.6427;
294
    //     return $minLong;
295
    // }
296
    //
297
    // public function maxLong($longitude)
298
    // {
299
    //
300
    //     $maxLong = floatval($longitude)+0.6427;
301
    //     return $maxLong;
302
    // }
303
    //
304
    // public function minLat($latitude)
305
    // {
306
    //
307
    //     $minLat = (floatval($latitude)) - 0.260;
308
    //     return $minLat;
309
    // }
310
    //
311
    // public function maxLat($latitude)
312
    // {
313
    //
314
    //     $maxLat = (floatval($latitude)) + 0.260;
315
    //     return $maxLat;
316
    // }
317
318
319
    // public function mapLink($latitude, $longitude, $minLat, $maxLat, $minLong, $maxLong)
320
    // {
321
    //                 https://www.openstreetmap.org/export/embed.html?bbox=-6.8860333396912%2C53.093889465332%2C-5.6006333396912%2C53.613889465332&amp;layer=mapnik&amp;marker=53.353889465332%2-6.2433333396912
322
    //     // $link = "https://www.openstreetmap.org/export/embed.html?bbox=12.669982910156252%2C55.56592203025787%2C13.955383300781252%2C56.08506381314523&amp;layer=mapnik&amp;marker=55.82635894724891%2C13.31268310546875"
323
    //
324
    //     $link = "https://www.openstreetmap.org/export/embed.html?bbox=" . $minLong . "%2C" . $minLat . "%2C" . $maxLong . "%2C" . $maxLat . "&amp;layer=mapnik&amp;marker=" . $latitude . "%2C" . $longitude;
325
    //
326
    //     return $link;
327
    // }
328
329
    // public function mapLink($latitude, $longitude, $minLat, $maxLat, $minLong, $maxLong)
330
    // {
331
    //     if ($latitude) {
332
    //         $link = "https://www.openstreetmap.org/export/embed.html?bbox=" . $minLong . "%2C" . $minLat . "%2C" . $maxLong . "%2C" . $maxLat . "&amp;layer=mapnik&amp;marker=" . $latitude . "%2C" . $longitude;
333
    //     } else {
334
    //         $link = "https://www.openstreetmap.org/export/embed.html?bbox=-0.64%2C85%2C0.64%2C90&amp;layer=mapnik&amp;marker=87.5%2C0";
335
    //     }
336
    //
337
    //     return $link;
338
    // }
339
    //
340
    // public function largeMapLink($latitude, $longitude)
341
    // {
342
    //     if ($latitude) {
343
    //         $link = "https://www.openstreetmap.org/?mlat=" . $latitude . "&amp;mlon=" . $longitude . "#map=10/" . $latitude . "/" . $longitude;
344
    //     // <a href="https://www.openstreetmap.org/?mlat=55.8264&amp;mlon=13.3127#map=10/55.8264/13.3127">
345
    //     } else {
346
    //         $link = "https://www.openstreetmap.org";
347
    //     }
348
    //     return $link;
349
    // }
350
351
352
    // public function checkOwnIP()
353
    // {
354
    //     $remoteAddr = isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR']:'127.0.0.1';
355
    //     return $remoteAddr;
356
    // }
357
}
358