christoffer-bolin /
weathermodule
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Anax\Models; |
||||
| 4 | |||||
| 5 | class WeatherForecast |
||||
| 6 | { |
||||
| 7 | |||||
| 8 | 5 | public function __construct() |
|||
| 9 | { |
||||
| 10 | 5 | $this->oneWeek = []; |
|||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 11 | 5 | $this->weather = []; |
|||
|
0 ignored issues
–
show
|
|||||
| 12 | 5 | } |
|||
| 13 | |||||
| 14 | |||||
| 15 | public function fetchApiKey() |
||||
| 16 | { |
||||
| 17 | $access_key = require ANAX_INSTALL_PATH . "/config/weatherapi.php"; |
||||
| 18 | $access_key = $access_key["weatherKeyHolder"]["weatherKey"]; |
||||
| 19 | return $access_key; |
||||
| 20 | } |
||||
| 21 | |||||
| 22 | |||||
| 23 | public function checkWeather($lat, $lon) |
||||
| 24 | { |
||||
| 25 | if ($this->validateNumbersCords($lat, $lon) && $this->validateCords($lat, $lon)) { |
||||
| 26 | $exclude = "current,minutely,hourly,alerts"; |
||||
| 27 | |||||
| 28 | $access_key = $this->fetchApiKey(); |
||||
| 29 | |||||
| 30 | $ch = curl_init('https://api.openweathermap.org/data/2.5/onecall?lat='.$lat.'&lon='.$lon.'&exclude='.$exclude.'&units=metric&lang=sv&appid='.$access_key.''); |
||||
| 31 | 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
Loading history...
|
|||||
| 32 | |||||
| 33 | // Store the data: |
||||
| 34 | $json = 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
Loading history...
|
|||||
| 35 | 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
Loading history...
|
|||||
| 36 | |||||
| 37 | // Decode JSON response: |
||||
| 38 | $api_result = json_decode($json, true); |
||||
| 39 | |||||
| 40 | $week = $api_result["daily"]; |
||||
| 41 | |||||
| 42 | foreach ($week as $value) { |
||||
| 43 | $current = [ |
||||
| 44 | "date" => gmdate("Y-m-d", $value["dt"]), |
||||
| 45 | "temp" => $value["temp"]["min"] . " - " . $value["temp"]["max"], |
||||
| 46 | "description" => $value["weather"][0]["description"] |
||||
| 47 | ]; |
||||
| 48 | $this->oneWeek[] = $current; |
||||
|
0 ignored issues
–
show
|
|||||
| 49 | } |
||||
| 50 | } |
||||
| 51 | return $this->oneWeek; |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | public function getFiveDays() |
||||
| 55 | { |
||||
| 56 | $days = []; |
||||
| 57 | for ($i = 0; $i > -5; $i--) { |
||||
| 58 | $days[] = strtotime("$i days"); |
||||
| 59 | } |
||||
| 60 | return $days; |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | 4 | public function validateCords($lat, $lon) |
|||
| 64 | { |
||||
| 65 | 4 | if ($lat < 90 && $lat > -90 && $lon < 180 && $lon > -180) { |
|||
| 66 | 3 | return true; |
|||
| 67 | } else { |
||||
| 68 | 1 | $this->weather = "Felaktig input, försök igen."; |
|||
|
0 ignored issues
–
show
|
|||||
| 69 | } |
||||
| 70 | 1 | } |
|||
| 71 | |||||
| 72 | 5 | public function validateNumbersCords($lat, $lon) |
|||
| 73 | { |
||||
| 74 | 5 | if (!ctype_alpha($lat) && !ctype_alpha($lon)) { |
|||
| 75 | 4 | return true; |
|||
| 76 | } else { |
||||
| 77 | 1 | $this->weather = "Felaktig input, försök igen."; |
|||
|
0 ignored issues
–
show
|
|||||
| 78 | } |
||||
| 79 | 1 | } |
|||
| 80 | |||||
| 81 | public function checkHistory($lat, $lon) |
||||
| 82 | { |
||||
| 83 | if ($this->validateNumbersCords($lat, $lon) && $this->validateCords($lat, $lon)) { |
||||
| 84 | $pastFiveDays = $this->getFivedays(); |
||||
| 85 | $fetch = 'https://api.openweathermap.org/data/2.5/onecall/timemachine?lat='.$lat.'&lon='.$lon.'&lang=sv&units=metric&dt='; |
||||
| 86 | |||||
| 87 | $mcurl = curl_multi_init(); |
||||
| 88 | |||||
| 89 | $fiveDays = []; |
||||
| 90 | foreach ($pastFiveDays as $day) { |
||||
| 91 | $ch3 = curl_init($fetch.$day.'&APPID='.$this->access_key.''); |
||||
|
0 ignored issues
–
show
|
|||||
| 92 | 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
Loading history...
|
|||||
| 93 | 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
Loading history...
|
|||||
| 94 | $fiveDays[] = $ch3; |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | $run = null; |
||||
| 98 | |||||
| 99 | do { |
||||
| 100 | curl_multi_exec($mcurl, $run); |
||||
| 101 | } while ($run); |
||||
| 102 | |||||
| 103 | foreach ($fiveDays as $curl) { |
||||
| 104 | curl_multi_remove_handle($mcurl, $curl); |
||||
| 105 | } |
||||
| 106 | |||||
| 107 | curl_multi_close($mcurl); |
||||
| 108 | |||||
| 109 | foreach ($fiveDays as $day) { |
||||
| 110 | $output = curl_multi_getcontent($day); |
||||
| 111 | $exploded = json_decode($output, true); |
||||
| 112 | |||||
| 113 | $current = [ |
||||
| 114 | "date" => gmdate("Y-m-d", $exploded["current"]["dt"]), |
||||
| 115 | "temp" => $exploded["current"]["temp"], |
||||
| 116 | "description" => $exploded["current"]["weather"][0]["description"] |
||||
| 117 | ]; |
||||
| 118 | $this->weather[] = $current; |
||||
|
0 ignored issues
–
show
|
|||||
| 119 | } |
||||
| 120 | } |
||||
| 121 | return $this->weather; |
||||
| 122 | } |
||||
| 123 | } |
||||
| 124 |