WeatherReport::getHistoricWeather()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
c 1
b 0
f 0
nc 8
nop 3
dl 0
loc 40
ccs 0
cts 21
cp 0
crap 30
rs 9.2568
1
<?php
2
3
namespace Lii\Model;
4
5
/**
6
 * Get weather reports for a specific location.
7
 *
8
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
9
 * @SuppressWarnings(PHPMD.UnusedPrivateField)
10
 * @SuppressWarnings(PHPMD.StaticAccess)
11
*/
12
class WeatherReport
13
{
14
15
    // Global variables
16
    private $accessKey = '';
17
18 11
    public function __construct($apiKey)
19
    {
20 11
        $this->accessKey = $apiKey;
21 11
    }
22
23
//     public function checkWeather($lat, $long)
24
//     {
25
//             // Initialize CURL:
26
//             $cRes = curl_init('api.openweathermap.org/data/2.5/weather?lat='.$lat.'&lon='.$long.'&units=metric&appid='.$this->accessKey.'');
27
//             curl_setopt($cRes, CURLOPT_RETURNTRANSFER, true);
28
//
29
//             // Store the data:
30
//             $json = curl_exec($cRes);
31
//             curl_close($cRes);
32
//
33
//             // Decode JSON response:
34
//             $result = json_decode($json, true);
35
//
36
//         return $result;
37
//     }
38
39
    public function fetchAllWeather($lat, $long)
40
    {
41
        // build the individual requests, but do not execute them
42
        $ch1 = curl_init('api.openweathermap.org/data/2.5/weather?lat='.$lat.'&lon='.$long.'&units=metric&appid='.$this->accessKey.'');
43
        $ch2 = curl_init('https://api.openweathermap.org/data/2.5/onecall?lat='.$lat.'&lon='.$long.'&exclude=hourly,minutely,current&units=metric&appid='.$this->accessKey.'');
44
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $ch1 can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept 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

44
        curl_setopt(/** @scrutinizer ignore-type */ $ch1, CURLOPT_RETURNTRANSFER, true);
Loading history...
45
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
46
47
        // build the multi-curl handle, adding both $ch
48
        $mh1 = curl_multi_init();
49
        curl_multi_add_handle($mh1, $ch1);
0 ignored issues
show
Bug introduced by
It seems like $ch1 can also be of type false; however, parameter $ch of curl_multi_add_handle() does only seem to accept 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

49
        curl_multi_add_handle($mh1, /** @scrutinizer ignore-type */ $ch1);
Loading history...
50
        curl_multi_add_handle($mh1, $ch2);
51
52
        // execute all queries simultaneously, and continue when all are complete
53
        $running = null;
54
        do {
55
            curl_multi_exec($mh1, $running);
56
        } while ($running);
57
58
        //close the handles
59
        curl_multi_remove_handle($mh1, $ch1);
0 ignored issues
show
Bug introduced by
It seems like $ch1 can also be of type false; however, parameter $ch of curl_multi_remove_handle() does only seem to accept 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

59
        curl_multi_remove_handle($mh1, /** @scrutinizer ignore-type */ $ch1);
Loading history...
60
        curl_multi_remove_handle($mh1, $ch2);
61
        curl_multi_close($mh1);
62
63
        // all of our requests are done, we can now access the results
64
        $result1 = curl_multi_getcontent($ch1);
0 ignored issues
show
Bug introduced by
It seems like $ch1 can also be of type false; however, parameter $ch of curl_multi_getcontent() does only seem to accept 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

64
        $result1 = curl_multi_getcontent(/** @scrutinizer ignore-type */ $ch1);
Loading history...
65
        $currentWeather = json_decode($result1, true);
66
        $result2 = curl_multi_getcontent($ch2);
67
        $forecastWeather = json_decode($result2, true);
68
69
        return  [$currentWeather, $forecastWeather];
70
    }
71
72 2
    public function getHistoricDates()
73
    {
74 2
        $dates = [];
75 2
        $date = date_create(date("Y-m-d", time()));
76 2
        date_modify($date, "-1 days");
0 ignored issues
show
Bug introduced by
It seems like $date can also be of type false; however, parameter $object of date_modify() does only seem to accept DateTime, 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

76
        date_modify(/** @scrutinizer ignore-type */ $date, "-1 days");
Loading history...
77 2
        $date1 = date_create(date("Y-m-d", date_format($date, "U")));
0 ignored issues
show
Bug introduced by
date_format($date, 'U') of type string is incompatible with the type integer expected by parameter $timestamp of date(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        $date1 = date_create(date("Y-m-d", /** @scrutinizer ignore-type */ date_format($date, "U")));
Loading history...
78 2
        date_modify($date, "-1 days");
79 2
        $date2 = date_create(date("Y-m-d", date_format($date, "U")));
80 2
        date_modify($date, "-1 days");
81 2
        $date3 = date_create(date("Y-m-d", date_format($date, "U")));
82 2
        date_modify($date, "-1 days");
83 2
        $date4 = date_create(date("Y-m-d", date_format($date, "U")));
84 2
        array_push($dates, date_format($date1, "U"), date_format($date2, "U"), date_format($date3, "U"), date_format($date4, "U"));
85
86 2
        return $dates;
87
    }
88
89
    public function getHistoricWeather($days, $lat, $long)
90
    {
91
        $url = 'https://api.openweathermap.org/data/2.5/onecall/timemachine?lat='.$lat.'&lon='.$long.'&units=metric&appid='.$this->accessKey.'&dt=';
92
93
        $options = [
94
            CURLOPT_RETURNTRANSFER => true,
95
        ];
96
97
        // Add all curl handlers and remember them
98
        // Initiate the multi curl handler
99
        $mh1 = curl_multi_init();
100
        $chAll = [];
101
        foreach ($days as $day) {
102
            $ch1 = curl_init("$url$day");
103
            curl_setopt_array($ch1, $options);
0 ignored issues
show
Bug introduced by
It seems like $ch1 can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept 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

103
            curl_setopt_array(/** @scrutinizer ignore-type */ $ch1, $options);
Loading history...
104
            curl_multi_add_handle($mh1, $ch1);
0 ignored issues
show
Bug introduced by
It seems like $ch1 can also be of type false; however, parameter $ch of curl_multi_add_handle() does only seem to accept 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

104
            curl_multi_add_handle($mh1, /** @scrutinizer ignore-type */ $ch1);
Loading history...
105
            $chAll[] = $ch1;
106
        }
107
108
        // Execute all queries simultaneously,
109
        // and continue when all are complete
110
        $running = null;
111
        do {
112
            curl_multi_exec($mh1, $running);
113
        } while ($running);
114
115
        // Close the handles
116
        foreach ($chAll as $ch1) {
117
            curl_multi_remove_handle($mh1, $ch1);
118
        }
119
        curl_multi_close($mh1);
120
121
        // All of our requests are done, we can now access the results
122
        $response = [];
123
        foreach ($chAll as $ch1) {
124
            $data = curl_multi_getcontent($ch1);
125
            $response[] = json_decode($data, true);
126
        }
127
128
        return $response;
129
    }
130
131
    public function getJsonWeather($days, $lat, $long)
132
    {
133
        $allWeather = $this->fetchAllWeather($lat, $long);
134
        $historicWeather = $this->getHistoricWeather($days, $lat, $long);
135
136
//         var_dump($historicWeather);
137
138
        $json = [
139
            "city" => "{$allWeather[0]['name']}",
140
            "current" => [
141
                            "weather" => "{$allWeather[0]['weather'][0]['description']}",
142
                            "temp" => "{$allWeather[0]['main']['temp']}",
143
                            "wind_speed" => "{$allWeather[0]['wind']['speed']}",
144
                            ],
145
            "forecast" => [
146
                            [
147
                                "date" => "{$allWeather[1]['daily'][0]['dt']}",
148
                                "weather" => "{$allWeather[1]['daily'][0]['weather'][0]['main']}",
149
                                "temp" => "{$allWeather[1]['daily'][0]['temp']['day']}",
150
                                "wind_speed" => "{$allWeather[1]['daily'][0]['wind_speed']}",
151
                            ],
152
                            [
153
                                "date" => "{$allWeather[1]['daily'][1]['dt']}",
154
                                "weather" => "{$allWeather[1]['daily'][1]['weather'][0]['main']}",
155
                                "temp" => "{$allWeather[1]['daily'][1]['temp']['day']}",
156
                                "wind_speed" => "{$allWeather[1]['daily'][1]['wind_speed']}",
157
                            ],
158
                            [
159
                                "date" => "{$allWeather[1]['daily'][2]['dt']}",
160
                                "weather" => "{$allWeather[1]['daily'][2]['weather'][0]['main']}",
161
                                "temp" => "{$allWeather[1]['daily'][2]['temp']['day']}",
162
                                "wind_speed" => "{$allWeather[1]['daily'][2]['wind_speed']}",
163
                            ],
164
                            [
165
                                "date" => "{$allWeather[1]['daily'][3]['dt']}",
166
                                "weather" => "{$allWeather[1]['daily'][3]['weather'][0]['main']}",
167
                                "temp" => "{$allWeather[1]['daily'][3]['temp']['day']}",
168
                                "wind_speed" => "{$allWeather[1]['daily'][3]['wind_speed']}",
169
                            ],
170
                            [
171
                                "date" => "{$allWeather[1]['daily'][4]['dt']}",
172
                                "weather" => "{$allWeather[1]['daily'][4]['weather'][0]['main']}",
173
                                "temp" => "{$allWeather[1]['daily'][4]['temp']['day']}",
174
                                "wind_speed" => "{$allWeather[1]['daily'][4]['wind_speed']}",
175
                            ],
176
                            [
177
                                "date" => "{$allWeather[1]['daily'][5]['dt']}",
178
                                "weather" => "{$allWeather[1]['daily'][5]['weather'][0]['main']}",
179
                                "temp" => "{$allWeather[1]['daily'][5]['temp']['day']}",
180
                                "wind_speed" => "{$allWeather[1]['daily'][5]['wind_speed']}",
181
                            ],
182
                            [
183
                                "date" => "{$allWeather[1]['daily'][6]['dt']}",
184
                                "weather" => "{$allWeather[1]['daily'][6]['weather'][0]['main']}",
185
                                "temp" => "{$allWeather[1]['daily'][6]['temp']['day']}",
186
                                "wind_speed" => "{$allWeather[1]['daily'][6]['wind_speed']}",
187
                            ],
188
                            [
189
                                "date" => "{$allWeather[1]['daily'][7]['dt']}",
190
                                "weather" => "{$allWeather[1]['daily'][7]['weather'][0]['main']}",
191
                                "temp" => "{$allWeather[1]['daily'][7]['temp']['day']}",
192
                                "wind_speed" => "{$allWeather[1]['daily'][7]['wind_speed']}",
193
                            ]
194
                        ],
195
            "historical" => [
196
                            [
197
                                "date" => "{$historicWeather[0]['current']['dt']}",
198
                                "weather" => "{$historicWeather[0]['current']['weather'][0]['main']}",
199
                                "temp" => "{$historicWeather[0]['current']['temp']}",
200
                                "wind_speed" => "{$historicWeather[0]['current']['wind_speed']}",
201
                            ],
202
                            [
203
                                "date" => "{$historicWeather[1]['current']['dt']}",
204
                                "weather" => "{$historicWeather[1]['current']['weather'][0]['main']}",
205
                                "temp" => "{$historicWeather[1]['current']['temp']}",
206
                                "wind_speed" => "{$historicWeather[1]['current']['wind_speed']}",
207
                            ],
208
                            [
209
                                "date" => "{$historicWeather[2]['current']['dt']}",
210
                                "weather" => "{$historicWeather[2]['current']['weather'][0]['main']}",
211
                                "temp" => "{$historicWeather[2]['current']['temp']}",
212
                                "wind_speed" => "{$historicWeather[2]['current']['wind_speed']}",
213
                            ],
214
                            [
215
                                "date" => "{$historicWeather[3]['current']['dt']}",
216
                                "weather" => "{$historicWeather[3]['current']['weather'][0]['main']}",
217
                                "temp" => "{$historicWeather[3]['current']['temp']}",
218
                                "wind_speed" => "{$historicWeather[3]['current']['wind_speed']}",
219
                            ],
220
                        ]
221
        ];
222
223
        return [$json];
224
    }
225
}
226