1 | <?php |
||||
2 | |||||
3 | namespace Anax\Models; |
||||
4 | |||||
5 | class WeatherApi |
||||
6 | { |
||||
7 | /** |
||||
8 | * model class for a 7 day weather forecast or 5 days history for a position |
||||
9 | * using the weather api 'openweather' |
||||
10 | * service container in $di |
||||
11 | */ |
||||
12 | |||||
13 | 11 | public function __construct(\Anax\DI\DIFactoryConfig $di = null, $apiKey = null, $wApi = null, $lApi = null) |
|||
14 | { |
||||
15 | 11 | $this->di = $di; |
|||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
16 | 11 | $this->apiKey = $apiKey; |
|||
0 ignored issues
–
show
|
|||||
17 | 11 | $this->weatherApi = $wApi; |
|||
0 ignored issues
–
show
|
|||||
18 | 11 | $this->locationApi = $lApi; |
|||
0 ignored issues
–
show
|
|||||
19 | 11 | $this->weather = []; |
|||
0 ignored issues
–
show
|
|||||
20 | 11 | $this->coordinates = []; |
|||
0 ignored issues
–
show
|
|||||
21 | 11 | $this->location = []; |
|||
0 ignored issues
–
show
|
|||||
22 | 11 | } |
|||
23 | |||||
24 | // get city and country of location - using nominatim api |
||||
25 | 8 | public function getLocation() |
|||
26 | { |
||||
27 | 8 | if (!empty($this->coordinates)) { |
|||
28 | // for test cases - no great solution i know |
||||
29 | 7 | if (!isset($_SERVER["HTTP_REFERER"])) { |
|||
30 | 7 | $server = "http://google.com"; |
|||
31 | } else { |
||||
32 | $server = $_SERVER["HTTP_REFERER"]; |
||||
33 | } |
||||
34 | |||||
35 | 7 | $ch2 = curl_init($this->locationApi.'reverse?format=geocodejson&lat='.$this->coordinates[0].'&lon='.$this->coordinates[1].''); |
|||
36 | 7 | curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); |
|||
0 ignored issues
–
show
It seems like
$ch2 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
![]() |
|||||
37 | 7 | curl_setopt($ch2, CURLOPT_REFERER, $server); |
|||
38 | |||||
39 | 7 | $json = curl_exec($ch2); |
|||
0 ignored issues
–
show
It seems like
$ch2 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
![]() |
|||||
40 | 7 | curl_close($ch2); |
|||
0 ignored issues
–
show
It seems like
$ch2 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
![]() |
|||||
41 | |||||
42 | 7 | $result = json_decode($json, true); |
|||
43 | |||||
44 | 7 | $this->location = [ |
|||
0 ignored issues
–
show
|
|||||
45 | 7 | $result["features"][0]["properties"]["geocoding"]["city"] ?? "odefinierad", |
|||
46 | 7 | $result["features"][0]["properties"]["geocoding"]["country"] ?? " odefinierad" |
|||
47 | ]; |
||||
48 | |||||
49 | 7 | return $this->location; |
|||
50 | } |
||||
51 | 1 | } |
|||
52 | |||||
53 | // check to see if input coordinates are valid, else return err |
||||
54 | 1 | public function validCoordinates($latitude, $longitude) |
|||
55 | { |
||||
56 | 1 | if ($latitude < 90 && $latitude > -90 && $longitude < 180 && $longitude > -180) { |
|||
57 | 1 | return true; |
|||
58 | } else { |
||||
59 | 1 | $this->weather = "Ogiltiga koordinater, försök igen."; |
|||
0 ignored issues
–
show
|
|||||
60 | 1 | $this->coordinates = []; |
|||
0 ignored issues
–
show
|
|||||
61 | } |
||||
62 | 1 | } |
|||
63 | |||||
64 | // get the past five days date in linux format |
||||
65 | 1 | public function pastFive() |
|||
66 | { |
||||
67 | 1 | $days = []; |
|||
68 | 1 | for ($i = 0; $i > -5; $i--) { |
|||
69 | 1 | $days[] = strtotime("$i days"); |
|||
70 | } |
||||
71 | 1 | return $days; |
|||
72 | } |
||||
73 | |||||
74 | // set coordinates from controller, to be able to get location info in this class |
||||
75 | 7 | public function setCoordinates($lat, $long) |
|||
76 | { |
||||
77 | 7 | $this->coordinates = [$lat, $long]; |
|||
0 ignored issues
–
show
|
|||||
78 | 7 | } |
|||
79 | |||||
80 | // give controller access to current coordinates |
||||
81 | 8 | public function getCoordinates() |
|||
82 | { |
||||
83 | 8 | return $this->coordinates; |
|||
84 | } |
||||
85 | |||||
86 | // for testing - set weather manually |
||||
87 | // public function setWeather($w) |
||||
88 | // { |
||||
89 | // $this->weather = $w; |
||||
90 | // } |
||||
91 | |||||
92 | // fetch for 5 days past weather |
||||
93 | public function pastWeather($latitude, $longitude) |
||||
94 | { |
||||
95 | if ($this->validCoordinates($latitude, $longitude)) { |
||||
96 | $pastFive = $this->pastFive(); |
||||
97 | $fetch = $this->weatherApi.'data/2.5/onecall/timemachine?lat='.$latitude.'&lon='.$longitude.'&lang=sv&units=metric&dt='; |
||||
98 | |||||
99 | $mcurl = curl_multi_init(); |
||||
100 | $fiveDays = []; |
||||
101 | foreach ($pastFive as $day) { |
||||
102 | $ch3 = curl_init($fetch.$day.'&APPID='.$this->apiKey.''); |
||||
103 | curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1); |
||||
0 ignored issues
–
show
It seems like
$ch3 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
![]() |
|||||
104 | curl_multi_add_handle($mcurl, $ch3); |
||||
0 ignored issues
–
show
It seems like
$ch3 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
![]() |
|||||
105 | $fiveDays[] = $ch3; |
||||
106 | } |
||||
107 | |||||
108 | $run = null; |
||||
109 | |||||
110 | do { |
||||
111 | curl_multi_exec($mcurl, $run); |
||||
112 | } while ($run); |
||||
113 | |||||
114 | foreach ($fiveDays as $curl) { |
||||
115 | curl_multi_remove_handle($mcurl, $curl); |
||||
116 | } |
||||
117 | |||||
118 | curl_multi_close($mcurl); |
||||
119 | |||||
120 | foreach ($fiveDays as $day) { |
||||
121 | $output = curl_multi_getcontent($day); |
||||
122 | $exploded = json_decode($output, true); |
||||
123 | |||||
124 | $current = [ |
||||
125 | "date" => gmdate("Y-m-d", $exploded["current"]["dt"]), |
||||
126 | "temp" => $exploded["current"]["temp"], |
||||
127 | "description" => $exploded["current"]["weather"][0]["description"] |
||||
128 | ]; |
||||
129 | $this->weather[] = $current; |
||||
0 ignored issues
–
show
|
|||||
130 | } |
||||
131 | } |
||||
132 | return $this->weather; |
||||
133 | } |
||||
134 | |||||
135 | // fetch for 7 days coming weather |
||||
136 | public function comingWeather($latitude, $longitude) |
||||
137 | { |
||||
138 | if ($this->validCoordinates($latitude, $longitude)) { |
||||
139 | $exclude = "current,minutely,hourly,alerts"; |
||||
140 | |||||
141 | $ch1 = curl_init($this->weatherApi.'data/2.5/onecall?lat='.$latitude.'&lon='.$longitude.'&cnt={}&exclude='.$exclude.'&units=metric&lang=sv&appid='.$this->apiKey.''); |
||||
142 | |||||
143 | curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); |
||||
0 ignored issues
–
show
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
![]() |
|||||
144 | |||||
145 | $json = curl_exec($ch1); |
||||
0 ignored issues
–
show
It seems like
$ch1 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
![]() |
|||||
146 | curl_close($ch1); |
||||
0 ignored issues
–
show
It seems like
$ch1 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
![]() |
|||||
147 | |||||
148 | $result = json_decode($json, true); |
||||
149 | |||||
150 | $sevenDays = $result["daily"]; |
||||
151 | |||||
152 | foreach ($sevenDays as $value) { |
||||
153 | $current = [ |
||||
154 | "date" => gmdate("Y-m-d", $value["dt"]), |
||||
155 | "temp" => "mellan ".$value["temp"]["min"] . " - " . $value["temp"]["max"], |
||||
156 | "description" => $value["weather"][0]["description"] |
||||
157 | ]; |
||||
158 | $this->weather[] = $current; |
||||
0 ignored issues
–
show
|
|||||
159 | } |
||||
160 | } |
||||
161 | return $this->weather; |
||||
162 | } |
||||
163 | } |
||||
164 |