1 | <?php |
||||
2 | |||||
3 | namespace H4MSK1\Curl\Traits; |
||||
4 | |||||
5 | trait WeatherApiTrait |
||||
6 | { |
||||
7 | public function getWeatherData($lat, $lng, $type) |
||||
8 | { |
||||
9 | if ($type === 'forecast') { |
||||
10 | return $this->normalizeForecast($this->fetchWeather($lat, $lng)); |
||||
11 | } else { |
||||
12 | return $this->normalizeForecast($this->fetchWeatherHistory($lat, $lng)); |
||||
13 | } |
||||
14 | } |
||||
15 | public function fetchWeather($lat, $lng) |
||||
16 | { |
||||
17 | $service = $this->getServiceByName('darksky'); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
18 | $endpoint = "{$service['endpoint']}{$service['key']}/{$lat},{$lng}?lang=sv&units=si"; |
||||
19 | |||||
20 | return $this->fetchCurl($endpoint); |
||||
0 ignored issues
–
show
It seems like
fetchCurl() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
21 | } |
||||
22 | |||||
23 | public function fetchWeatherHistory($lat, $lng, $pastDays = 30) |
||||
24 | { |
||||
25 | $service = $this->getServiceByName('darksky'); |
||||
26 | $endpoint = "{$service['endpoint']}{$service['key']}/{$lat},{$lng},{{day}}?lang=sv&units=si"; |
||||
27 | |||||
28 | $curlHandlers = []; |
||||
29 | $curlCollection = curl_multi_init(); |
||||
30 | |||||
31 | for ($i = $pastDays; $i > 0; $i--) { |
||||
32 | $days[$i] = strtotime("-$i days"); |
||||
33 | } |
||||
34 | |||||
35 | for ($i = 0; $i < $pastDays; $i++) { |
||||
36 | $curlHandlers[$i] = curl_init(); |
||||
37 | |||||
38 | curl_setopt($curlHandlers[$i], CURLOPT_URL, str_replace('{{day}}', $days[($i + 1)], $endpoint)); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
39 | curl_setopt($curlHandlers[$i], CURLOPT_HEADER, 0); |
||||
40 | curl_setopt($curlHandlers[$i], CURLOPT_RETURNTRANSFER, 1); |
||||
41 | |||||
42 | curl_multi_add_handle($curlCollection, $curlHandlers[$i]); |
||||
43 | } |
||||
44 | |||||
45 | $running = null; |
||||
46 | |||||
47 | do { |
||||
48 | curl_multi_exec($curlCollection, $running); |
||||
49 | } while ($running > 0); |
||||
50 | |||||
51 | $result = []; |
||||
52 | |||||
53 | foreach ($curlHandlers as $index => $handler) { |
||||
54 | $result[$index] = curl_multi_getcontent($handler); |
||||
55 | $result[$index] = json_decode($result[$index], true); |
||||
56 | curl_multi_remove_handle($curlCollection, $handler); |
||||
57 | } |
||||
58 | |||||
59 | curl_multi_close($curlCollection); |
||||
60 | |||||
61 | return $result; |
||||
62 | } |
||||
63 | |||||
64 | public function normalizeForecast($data) |
||||
65 | { |
||||
66 | $newData = []; |
||||
67 | |||||
68 | if (array_key_exists('daily', $data)) { |
||||
69 | foreach ($data['daily']['data'] as $day) { |
||||
70 | $newData[] = [ |
||||
71 | 'summary' => $day['summary'], |
||||
72 | 'temperatureMin' => $day['temperatureMin'], |
||||
73 | 'temperatureMax' => $day['temperatureMax'], |
||||
74 | 'sunriseTime' => gmdate('H:i', $day['sunriseTime']), |
||||
75 | 'sunsetTime' => gmdate('H:i', $day['sunsetTime']), |
||||
76 | 'date' => gmdate('Y-m-d', $day['time']), |
||||
77 | ]; |
||||
78 | } |
||||
79 | } else { |
||||
80 | foreach ($data as $day) { |
||||
81 | $day = $day['daily']['data'][0]; |
||||
82 | $newData[] = [ |
||||
83 | 'summary' => $day['summary'], |
||||
84 | 'temperatureMin' => $day['temperatureMin'], |
||||
85 | 'temperatureMax' => $day['temperatureMax'], |
||||
86 | 'sunriseTime' => gmdate('H:i', $day['sunriseTime']), |
||||
87 | 'sunsetTime' => gmdate('H:i', $day['sunsetTime']), |
||||
88 | 'date' => gmdate('Y-m-d', $day['time']), |
||||
89 | ]; |
||||
90 | } |
||||
91 | } |
||||
92 | |||||
93 | return $newData; |
||||
94 | } |
||||
95 | } |
||||
96 |