1 | <?php |
||||
2 | |||||
3 | namespace Anax\Weather; |
||||
4 | |||||
5 | use Anax\Commons\ContainerInjectableInterface; |
||||
6 | use Anax\Commons\ContainerInjectableTrait; |
||||
7 | |||||
8 | /** |
||||
9 | * To ease rendering a page consisting of several views. |
||||
10 | *@SuppressWarnings(PHPMD) |
||||
11 | */ |
||||
12 | class Weather implements ContainerInjectableInterface |
||||
13 | { |
||||
14 | use ContainerInjectableTrait; |
||||
15 | |||||
16 | |||||
17 | public function dates($start, $end) : array |
||||
18 | { |
||||
19 | $old = new \DateTime(); |
||||
20 | if ($start) { |
||||
21 | $old->modify($start); |
||||
22 | } |
||||
23 | $dates = []; |
||||
24 | $day = 0; |
||||
25 | while ($day <= $end) { |
||||
26 | $dates[] = $old->format("Y-m-d"); |
||||
27 | $old->modify('+1 day'); |
||||
28 | $day+=1; |
||||
29 | } |
||||
30 | return $dates; |
||||
31 | } |
||||
32 | |||||
33 | public function newWeather($lat, $long) |
||||
34 | { |
||||
35 | $keys = require(ANAX_INSTALL_PATH . "/config/api.php"); |
||||
36 | // $accessKey = file_get_contents($path[0]); |
||||
37 | |||||
38 | $accessKey = $keys[1]; |
||||
39 | |||||
40 | $chr = curl_init('https://api.darksky.net/forecast/'.$accessKey.'/'.$lat.','.$long.'?exclude=flags,hourly,minutley,currently'); |
||||
41 | curl_setopt($chr, CURLOPT_RETURNTRANSFER, true); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
42 | |||||
43 | // Store the data: |
||||
44 | $json = curl_exec($chr); |
||||
0 ignored issues
–
show
It seems like
$chr 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
![]() |
|||||
45 | curl_close($chr); |
||||
0 ignored issues
–
show
It seems like
$chr 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
![]() |
|||||
46 | $apiResult = json_decode($json, true); |
||||
47 | if (isset($apiResult[0]["code"])) { |
||||
48 | return $apiResult[0]["error"].'. Bad latitude longitude, could not retrive weather. Try again'; |
||||
49 | } |
||||
50 | if (isset($apiResult["error"])) { |
||||
51 | return $apiResult["error"].'. Bad latitude longitude, could not retrive weather. Try again'; |
||||
52 | } |
||||
53 | return $apiResult["daily"]["data"]; |
||||
54 | } |
||||
55 | |||||
56 | public function histWeather($lat, $long) |
||||
57 | { |
||||
58 | $keys = require(ANAX_INSTALL_PATH . "/config/api.php"); |
||||
59 | // $accessKey = file_get_contents($path[0]); |
||||
60 | |||||
61 | $accessKey = $keys[1]; |
||||
62 | |||||
63 | // $currDate = new \DateTime(); |
||||
64 | $old = new \DateTime(); |
||||
65 | $old->modify('-31 day'); |
||||
66 | |||||
67 | // array of curl handles |
||||
68 | $multiCurl = array(); |
||||
69 | // data to be returned |
||||
70 | $result = array(); |
||||
71 | // multi handle |
||||
72 | $umha = curl_multi_init(); |
||||
0 ignored issues
–
show
|
|||||
73 | $day = 0; |
||||
74 | while ($day <=30) { |
||||
75 | // URL from which data will be fetched |
||||
76 | $fetchURL = 'https://api.darksky.net/forecast/'.$accessKey.'/'.$lat.','.$long.','.$old->format("Y-m-d").'T12:00:00?exclude=flags,hourly,minutley,currently'; |
||||
77 | $old->modify('+1 day'); |
||||
78 | $multiCurl[$day] = curl_init(); |
||||
79 | curl_setopt($multiCurl[$day], CURLOPT_URL, $fetchURL); |
||||
80 | curl_setopt($multiCurl[$day], CURLOPT_HEADER, 0); |
||||
81 | curl_setopt($multiCurl[$day], CURLOPT_RETURNTRANSFER, 1); |
||||
82 | curl_multi_add_handle($muha, $multiCurl[$day]); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
83 | $day+=1; |
||||
84 | } |
||||
85 | $index=null; |
||||
86 | do { |
||||
87 | curl_multi_exec($muha, $index); |
||||
88 | } while ($index); |
||||
89 | // get content and remove handles |
||||
90 | foreach ($multiCurl as $k) { |
||||
91 | curl_multi_remove_handle($muha, $k); |
||||
92 | } |
||||
93 | $data = null; |
||||
0 ignored issues
–
show
|
|||||
94 | foreach ($multiCurl as $ch) { |
||||
95 | $data = curl_multi_getcontent($ch); |
||||
96 | $result[] = json_decode($data, true); |
||||
97 | } |
||||
98 | // $i = 0; |
||||
99 | $fixedResult = []; |
||||
100 | if (isset($result[0]["code"])) { |
||||
101 | return $result[0]["error"].'. Bad latitude longitude, could not retrive weather. Try again'; |
||||
102 | } |
||||
103 | if (isset($result[0]["error"])) { |
||||
104 | return $result[0]["error"].'. Bad latitude longitude, could not retrive weather. Try again'; |
||||
105 | } |
||||
106 | foreach ($result as $res) { |
||||
107 | $fixedResult[] = $res["daily"]["data"]; |
||||
108 | // $i+=1; |
||||
109 | } |
||||
110 | // close |
||||
111 | curl_multi_close($muha); |
||||
112 | return $fixedResult; |
||||
113 | } |
||||
114 | public function json($apiResult, $dates, $type) |
||||
115 | { |
||||
116 | if (is_string($apiResult)) { |
||||
117 | return ["Error" => $apiResult]; |
||||
118 | } |
||||
119 | if ($type == "h") { |
||||
120 | foreach ($apiResult as $day) { |
||||
121 | $summaries[] = $day[0]["summary"]; |
||||
122 | } |
||||
123 | $counter = 0; |
||||
124 | foreach ($dates as $day) { |
||||
125 | $res["date"] = $day; |
||||
126 | $res["summary"] = $summaries[$counter]; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
127 | $arr[] = $res; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
128 | // $res[$day] = $summaries[$counter]; |
||||
129 | $counter += 1; |
||||
130 | } |
||||
131 | } elseif ($type == "k") { |
||||
132 | foreach ($apiResult as $day) { |
||||
133 | $summaries[] = $day["summary"]; |
||||
134 | } |
||||
135 | $counter = 0; |
||||
136 | foreach ($dates as $day) { |
||||
137 | $res["date"] = $day; |
||||
138 | $res["summary"] = $summaries[$counter]; |
||||
139 | $arr[] = $res; |
||||
140 | // $res[$day] = $summaries[$counter]; |
||||
141 | $counter += 1; |
||||
142 | } |
||||
143 | } |
||||
144 | return $arr; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
145 | } |
||||
146 | } |
||||
147 |