1 | <?php |
||
2 | namespace Blixter\Weather; |
||
3 | |||
4 | use DateInterval; |
||
5 | use DateTime; |
||
6 | |||
7 | /** |
||
8 | * |
||
9 | * Model for WeatherController |
||
10 | * @SuppressWarnings(PHPMD.ShortVariable) |
||
11 | */ |
||
12 | class WeatherModel |
||
13 | { |
||
14 | /** |
||
15 | * |
||
16 | * @var string @apiKey Init the procted API key |
||
17 | * @var object @curlhandler Init the object |
||
18 | * |
||
19 | */ |
||
20 | protected $apiKey; |
||
21 | protected $curlhandler; |
||
22 | protected $darkSkyUrl; |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * Get and Set the API key |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | 7 | public function __construct() |
|
31 | { |
||
32 | // Get the file where they key is stored |
||
33 | 7 | $this->darkSkyUrl = "https://api.darksky.net/forecast"; |
|
34 | 7 | } |
|
35 | |||
36 | /** |
||
37 | * |
||
38 | * Set the curlhandler |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | 6 | public function setCurl($ch) |
|
43 | { |
||
44 | 6 | $this->curlhandler = $ch; |
|
45 | 6 | } |
|
46 | |||
47 | /** |
||
48 | * |
||
49 | * Set the darkSkyUrl |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | 7 | public function setUrl($url) |
|
54 | { |
||
55 | 7 | $this->darkSkyUrl = $url; |
|
56 | 7 | } |
|
57 | |||
58 | /** |
||
59 | * |
||
60 | * Set the ApiKey |
||
61 | * |
||
62 | * @return void |
||
63 | */ |
||
64 | 7 | public function setApi($key) |
|
65 | { |
||
66 | 7 | $this->apiKey = $key; |
|
67 | 7 | } |
|
68 | |||
69 | /** |
||
70 | * Send request to Dark sky given coordinates |
||
71 | * |
||
72 | * @return array with weather information |
||
73 | */ |
||
74 | 2 | public function fetchData($coordinates) |
|
75 | { |
||
76 | 2 | $lat = $coordinates["lat"]; |
|
77 | 2 | $lon = $coordinates["lon"]; |
|
78 | |||
79 | 2 | $exclude = "exclude=minutely,hourly,currently,alerts,flags"; |
|
80 | 2 | $extend = "extend=daily&lang=sv&units=auto"; |
|
81 | 2 | $url = "$this->darkSkyUrl/$this->apiKey/$lat,$lon?$exclude&$extend"; |
|
82 | |||
83 | 2 | $json = true; |
|
84 | // curl the url and return the weather data |
||
85 | 2 | $jsonResponse = $this->curlhandler->curl($url, $json); |
|
86 | |||
87 | 2 | $weatherData = []; |
|
88 | 2 | foreach ((array) $jsonResponse["daily"]["data"] as $weather) { |
|
89 | 2 | array_push($weatherData, [ |
|
90 | 2 | "date" => gmdate("y-m-d", $weather["time"]), |
|
91 | 2 | "summary" => $weather["summary"], |
|
92 | 2 | "icon" => $weather["icon"], |
|
93 | 2 | "temperatureMin" => $weather["temperatureMin"], |
|
94 | 2 | "temperatureMax" => $weather["temperatureMax"], |
|
95 | 2 | "windSpeed" => $weather["windSpeed"], |
|
96 | 2 | "windGust" => $weather["windGust"], |
|
97 | 2 | "sunriseTime" => $weather["sunriseTime"], |
|
98 | 2 | "sunsetTime" => $weather["sunsetTime"], |
|
99 | ]); |
||
100 | } |
||
101 | |||
102 | // Remove first element in $weatherData, because it's yesterdays weather |
||
103 | 2 | array_shift($weatherData); |
|
104 | |||
105 | 2 | return $weatherData; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Send request to Dark sky given coordinates |
||
110 | * |
||
111 | * @return array with weather information |
||
112 | */ |
||
113 | 2 | public function fetchDataMulti($coordinates) |
|
114 | { |
||
115 | |||
116 | 2 | $lat = $coordinates["lat"]; |
|
117 | 2 | $lon = $coordinates["lon"]; |
|
118 | 2 | $time = new DateTime(); |
|
119 | 2 | $unixTime = $time->getTimestamp(); |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
120 | |||
121 | 2 | $exclude = "exclude=minutely,hourly,currently,alerts,flags"; |
|
122 | 2 | $extend = "extend=daily&lang=sv&units=auto"; |
|
123 | |||
124 | 2 | for ($i = 0; $i < 30; $i++) { |
|
125 | 2 | $unixTime = $time->getTimestamp(); |
|
126 | 2 | $time->sub(new DateInterval("P1D")); |
|
127 | 2 | $url = "$this->darkSkyUrl/$this->apiKey/$lat,$lon,$unixTime?$exclude&$extend"; |
|
128 | 2 | $urls[$i] = $url; |
|
129 | } |
||
130 | |||
131 | 2 | $json = true; |
|
132 | |||
133 | // curl the urls and return the weather data |
||
134 | 2 | $jsonResponse = $this->curlhandler->multiCurl($urls, $json); |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
135 | |||
136 | 2 | $weatherData = []; |
|
137 | 2 | foreach ($jsonResponse as $weatherDay) { |
|
138 | 2 | foreach ((array) $weatherDay["daily"]["data"] as $weather) { |
|
139 | 2 | array_push($weatherData, [ |
|
140 | 2 | "date" => gmdate("y-m-d", $weather["time"]), |
|
141 | 2 | "summary" => $weather["summary"], |
|
142 | 2 | "icon" => $weather["icon"], |
|
143 | 2 | "temperatureMin" => $weather["temperatureMin"], |
|
144 | 2 | "temperatureMax" => $weather["temperatureMax"], |
|
145 | 2 | "windSpeed" => $weather["windSpeed"], |
|
146 | 2 | "windGust" => $weather["windGust"], |
|
147 | 2 | "sunriseTime" => $weather["sunriseTime"], |
|
148 | 2 | "sunsetTime" => $weather["sunsetTime"], |
|
149 | ]); |
||
150 | } |
||
151 | } |
||
152 | 2 | return $weatherData; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Get location information from given query. |
||
157 | * |
||
158 | * @return array with coordinates |
||
159 | */ |
||
160 | 6 | public function getCoordinates($query) |
|
161 | { |
||
162 | 6 | $json = true; |
|
163 | // Curl this url with the query and return the coordinates. |
||
164 | 6 | $url = "https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=$query&limit=1&[email protected]"; |
|
165 | 6 | $jsonResponse = $this->curlhandler->curl($url, $json) ?? null; |
|
166 | |||
167 | 6 | if ($jsonResponse) { |
|
168 | $coords = [ |
||
169 | 4 | "lat" => $jsonResponse[0]["lat"], |
|
170 | 4 | "lon" => $jsonResponse[0]["lon"], |
|
171 | ]; |
||
172 | } else { |
||
173 | 2 | $coords = null; |
|
174 | } |
||
175 | |||
176 | 6 | return $coords; |
|
177 | } |
||
178 | } |
||
179 |