Issues (42)

src/Weather/Weather.php (32 issues)

1
<?php
2
3
// namespace Anax\Weather;
4
namespace Kris3XIQ\Weather;
5
6
/**
7
 * A model class retrieving data from an external server.
8
 *
9
 * @SuppressWarnings(PHPMD.ShortVariable)
10
 */
11
class Weather
12
{
13
    protected $key;
14
    protected $keyChain;
15
16
    /**
17
     * Set API-key value.
18
     *
19
     * @return void
20
     */
21
    public function setApiKey(string $apiKey)
22
    {
23
        $this->key = $apiKey;
24
    }
25
26
    /**
27
     * Set a new API-keychain (multiple API-keys), if you
28
     * need to fetch multiple API's.
29
     *
30
     * @return void
31
     */
32
    public function setKeyChain(array $keyChain)
33
    {
34
        $this->keyChain["keys"] = $keyChain;
35
    }
36
37
    public function verifyLocation(string $input)
38
    {
39
        $apiKey;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiKey seems to be never defined.
Loading history...
40
        $keyChain = $this->keyChain;
41
        if (array_key_exists("keys", $keyChain)) {
42
            $apiKey = $keyChain["keys"]["openweathermap"];
43
        }
44
        $apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=$input&units=metric&appid=$apiKey";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiKey does not seem to be defined for all execution paths leading up to this point.
Loading history...
45
46
        $ch = curl_init();
47
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
It seems like $ch 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

47
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
Loading history...
48
        curl_setopt($ch, CURLOPT_URL, $apiUrl);
49
50
        $data = curl_exec($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() 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

50
        $data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
51
52
        curl_close($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_close() 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

52
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
53
    
54
        $status = json_decode($data, true);
55
56
        return $status["cod"];
57
    }
58
59
    public function getWeatherCurrentFromAPI(string $input)
60
    {
61
        $apiKey;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiKey seems to be never defined.
Loading history...
62
        $keyChain = $this->keyChain;
63
        if (array_key_exists("keys", $keyChain)) {
64
            $apiKey = $keyChain["keys"]["openweathermap"];
65
        }
66
        $apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=$input&units=metric&appid=$apiKey";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiKey does not seem to be defined for all execution paths leading up to this point.
Loading history...
67
68
        $ch = curl_init();
69
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
It seems like $ch 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

69
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
Loading history...
70
        curl_setopt($ch, CURLOPT_URL, $apiUrl);
71
72
        $data = curl_exec($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() 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

72
        $data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
73
74
        curl_close($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_close() 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

74
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
75
    
76
        $weatherInfo = json_decode($data, true);
77
78
        return $weatherInfo;
79
    }
80
81
    public function getWeatherHistoryFromAPI($lat, $lon)
82
    {
83
        $apiKey;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiKey seems to be never defined.
Loading history...
84
        $keyChain = $this->keyChain;
85
        if (array_key_exists("keys", $keyChain)) {
86
            $apiKey = $keyChain["keys"]["openweathermap"];
87
        }
88
    
89
        // Create all cURL resources
90
        $dayOne = curl_init();
91
        $dayTwo = curl_init();
92
        $dayThree = curl_init();
93
        $dayFour = curl_init();
94
        $dayFive = curl_init();
95
96
        // Set dates
97
        $dayMinusOne = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
0 ignored issues
show
date('Y') of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

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

97
        $dayMinusOne = mktime(0, 0, 0, date("m"), date("d"), /** @scrutinizer ignore-type */ date("Y"));
Loading history...
date('m') of type string is incompatible with the type integer expected by parameter $month of mktime(). ( Ignorable by Annotation )

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

97
        $dayMinusOne = mktime(0, 0, 0, /** @scrutinizer ignore-type */ date("m"), date("d"), date("Y"));
Loading history...
date('d') of type string is incompatible with the type integer expected by parameter $day of mktime(). ( Ignorable by Annotation )

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

97
        $dayMinusOne = mktime(0, 0, 0, date("m"), /** @scrutinizer ignore-type */ date("d"), date("Y"));
Loading history...
98
        $dayMinusTwo = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
99
        $dayMinusThree = mktime(0, 0, 0, date("m"), date("d")-2, date("Y"));
100
        $dayMinusFour = mktime(0, 0, 0, date("m"), date("d")-3, date("Y"));
101
        $dayMinusFive = mktime(0, 0, 0, date("m"), date("d")-4, date("Y"));
102
103
        // set URL and other appropriate options
104
        curl_setopt($dayOne, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusOne&units=metric&appid=$apiKey");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiKey does not seem to be defined for all execution paths leading up to this point.
Loading history...
It seems like $dayOne 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

104
        curl_setopt(/** @scrutinizer ignore-type */ $dayOne, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusOne&units=metric&appid=$apiKey");
Loading history...
105
        curl_setopt($dayOne, CURLOPT_RETURNTRANSFER, true);
106
        curl_setopt($dayTwo, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusTwo&units=metric&appid=$apiKey");
107
        curl_setopt($dayTwo, CURLOPT_RETURNTRANSFER, true);
108
        curl_setopt($dayThree, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusThree&units=metric&appid=$apiKey");
109
        curl_setopt($dayThree, CURLOPT_RETURNTRANSFER, true);
110
        curl_setopt($dayFour, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusFour&units=metric&appid=$apiKey");
111
        curl_setopt($dayFour, CURLOPT_RETURNTRANSFER, true);
112
        curl_setopt($dayFive, CURLOPT_URL, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=$lat&lon=$lon&dt=$dayMinusFive&units=metric&appid=$apiKey");
113
        curl_setopt($dayFive, CURLOPT_RETURNTRANSFER, true);
114
115
        // Create the multiple cURL handle
116
        $mh = curl_multi_init();
117
118
        // Add the five handles
119
        curl_multi_add_handle($mh, $dayOne);
0 ignored issues
show
It seems like $dayOne 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

119
        curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $dayOne);
Loading history...
120
        curl_multi_add_handle($mh, $dayTwo);
121
        curl_multi_add_handle($mh, $dayThree);
122
        curl_multi_add_handle($mh, $dayFour);
123
        curl_multi_add_handle($mh, $dayFive);
124
125
        do {
126
            $status = curl_multi_exec($mh, $active);
127
            if ($active) {
128
                curl_multi_select($mh);
129
            }
130
        } while ($active && $status == CURLM_OK);
131
132
        curl_multi_remove_handle($mh, $dayOne);
0 ignored issues
show
It seems like $dayOne 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

132
        curl_multi_remove_handle($mh, /** @scrutinizer ignore-type */ $dayOne);
Loading history...
133
        curl_multi_remove_handle($mh, $dayTwo);
134
        curl_multi_remove_handle($mh, $dayThree);
135
        curl_multi_remove_handle($mh, $dayFour);
136
        curl_multi_remove_handle($mh, $dayFive);
137
        curl_multi_close($mh);
138
139
        $data;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
140
        $response1 = curl_multi_getcontent($dayOne);
0 ignored issues
show
It seems like $dayOne 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

140
        $response1 = curl_multi_getcontent(/** @scrutinizer ignore-type */ $dayOne);
Loading history...
141
        $response2 = curl_multi_getcontent($dayTwo);
142
        $response3 = curl_multi_getcontent($dayThree);
143
        $response4 = curl_multi_getcontent($dayFour);
144
        $response5 = curl_multi_getcontent($dayFive);
145
        $data = [
146
            "cod" => 200,
147
            "past_days" => [
148
                "past_01" => $response1,
149
                "past_02" => $response2,
150
                "past_03" => $response3,
151
                "past_04" => $response4,
152
                "past_05" => $response5
153
            ],
154
        ];
155
        
156
        return $data;
157
    }
158
159
    public function ipTrackWithGeolocation(string $input)
160
    {
161
        $apiKey;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiKey seems to be never defined.
Loading history...
162
        $keyChain = $this->keyChain;
163
        if (array_key_exists("keys", $keyChain)) {
164
            $apiKey = $keyChain["keys"]["ipstack"];
165
        }
166
        $apiUrl = "http://api.ipstack.com/$input?access_key=$apiKey";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $apiKey does not seem to be defined for all execution paths leading up to this point.
Loading history...
167
168
        $ch = curl_init();
169
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
It seems like $ch 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

169
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
Loading history...
170
        curl_setopt($ch, CURLOPT_URL, $apiUrl);
171
172
        $data = curl_exec($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() 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

172
        $data = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
173
174
        curl_close($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_close() 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

174
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
175
    
176
        $data = json_decode($data, true);
177
178
        return $data;
179
    }
180
181
    /**
182
     * Confirm validity of ipAddress to be either
183
     * IPv4 or IPv6.
184
     *
185
     * @return string
186
     */
187
    public function isIpAddress($ip)
188
    {
189
        $ipv4 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
190
        $ipv6 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
191
        if ($ipv4 || $ipv6) {
192
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type string.
Loading history...
193
        } else {
194
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
195
        }
196
    }
197
198
    /**
199
     * Converts current weather- and history data to JSON
200
     *
201
     * @return array
202
     */
203
    public function convertToJSON($currentWeather, $historyWeather)
204
    {
205
        if ($historyWeather) {
206
            $pastOne = json_decode($historyWeather["past_days"]["past_01"], true);
207
            $pastTwo = json_decode($historyWeather["past_days"]["past_02"], true);
208
            $pastThree = json_decode($historyWeather["past_days"]["past_03"], true);
209
            $pastFour = json_decode($historyWeather["past_days"]["past_04"], true);
210
            $pastFive = json_decode($historyWeather["past_days"]["past_05"], true);
211
        }
212
213
        $dataJSON = [
214
            "status code" => $currentWeather["cod"],
215
            "name" => $currentWeather["name"] . ", " . $currentWeather["sys"]["country"],
216
            "current_weather" => [
217
                "main" => $currentWeather["weather"][0]["main"],
218
                "description" => $currentWeather["weather"][0]["description"],
219
                "temperature" => $currentWeather["main"]["temp"],
220
                "feels_like" =>  $currentWeather["main"]["feels_like"],
221
            ],
222
            "yesterday" => [
223
                "date" => gmdate('r', $pastOne["hourly"][14]["dt"]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pastOne does not seem to be defined for all execution paths leading up to this point.
Loading history...
224
                "main" => $pastOne["hourly"][14]["weather"][0]["main"],
225
                "description" => $pastOne["hourly"][14]["weather"][0]["description"],
226
                "temperature" => $pastOne["hourly"][14]["temp"],
227
                "feels_like" => $pastOne["hourly"][14]["feels_like"],
228
            ],
229
            "2_days_ago" => [
230
                "date" => gmdate('r', $pastTwo["hourly"][14]["dt"]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pastTwo does not seem to be defined for all execution paths leading up to this point.
Loading history...
231
                "main" => $pastTwo["hourly"][14]["weather"][0]["main"],
232
                "description" => $pastTwo["hourly"][14]["weather"][0]["description"],
233
                "temperature" => $pastTwo["hourly"][14]["temp"],
234
                "feels_like" => $pastTwo["hourly"][14]["feels_like"],
235
            ],
236
            "3_days_ago" => [
237
                "date" => gmdate('r', $pastThree["hourly"][14]["dt"]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pastThree does not seem to be defined for all execution paths leading up to this point.
Loading history...
238
                "main" => $pastThree["hourly"][14]["weather"][0]["main"],
239
                "description" => $pastThree["hourly"][14]["weather"][0]["description"],
240
                "temperature" => $pastThree["hourly"][14]["temp"],
241
                "feels_like" => $pastThree["hourly"][14]["feels_like"],
242
            ],
243
            "4_days_ago" => [
244
                "date" => gmdate('r', $pastFour["hourly"][14]["dt"]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pastFour does not seem to be defined for all execution paths leading up to this point.
Loading history...
245
                "main" => $pastFour["hourly"][14]["weather"][0]["main"],
246
                "description" => $pastFour["hourly"][14]["weather"][0]["description"],
247
                "temperature" => $pastFour["hourly"][14]["temp"],
248
                "feels_like" => $pastFour["hourly"][14]["feels_like"],
249
            ],
250
            "5_days_ago" => [
251
                "date" => gmdate('r', $pastFive["hourly"][14]["dt"]),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pastFive does not seem to be defined for all execution paths leading up to this point.
Loading history...
252
                "main" => $pastFive["hourly"][14]["weather"][0]["main"],
253
                "description" => $pastFive["hourly"][14]["weather"][0]["description"],
254
                "temperature" => $pastFive["hourly"][14]["temp"],
255
                "feels_like" => $pastFive["hourly"][14]["feels_like"],
256
            ]
257
        ];
258
259
        return $dataJSON;
260
    }
261
}
262