Weather::getData()   B
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
nc 4
nop 2
dl 0
loc 39
c 1
b 0
f 0
cc 8
ccs 27
cts 27
cp 1
crap 8
rs 8.4444
1
<?php
2
3
namespace Bjos\Weather;
4
5
/**
6
 * A class to get weather info from an api.
7
 */
8
class Weather
9
{
10
    private $url = "https://api.openweathermap.org/data/2.5/onecall";
11
    private $filePath = "/config/api_owm.php";
12
    private $api;
13
    private $curl = null;
14
15
    /**
16
     * Constructor for the class
17
     *
18
     * @param object $curl curl object
19
     * @return void
20
     */
21 21
    public function __construct(object $curl)
22
    {
23 21
        $apiKey = "apiKey";
24 21
        $file = ANAX_INSTALL_PATH . $this->filePath;
25 21
        $owm = file_exists($file) ? require $file : null;
26 21
        $this->api = $owm ? $owm[$apiKey] : getenv("API_KEY");
27 21
        $this->curl = $curl;
28 21
    }
29
30
    /**
31
     * Get upcoming weather from api.
32
     *
33
     * @return array
34
     */
35 11
    public function getLatLong(string $ipAdr = null)
36
    {
37 11
        $location = null;
38 11
        $lat = null;
39 11
        $long = null;
40 11
        $coords = explode(",", $ipAdr);
41 11
        $coordsLength = count($coords);
42
43 11
        if (is_array($coords) && $coordsLength === 2) {
44 8
            if (is_numeric($coords[0]) && is_numeric($coords[1])) {
45 5
                $lat = floatval($coords[0]);
46 5
                $long = floatval($coords[1]);
47
            }
48
        }
49
50 11
        $location["latitude"] = $lat;
51 11
        $location["longitude"] = $long;
52
53 11
        return $location;
54
    }
55
56
    /**
57
     * Get upcoming weather from api.
58
     *
59
     * @return array
60
     */
61 1
    public function getWeather(string $lat = null, string $lon = null)
62
    {
63 1
        $forecast = null;
64 1
        $api = $this->api;
65 1
        $exclude = "exclude=minutely,hourly,current";
66 1
        $url = $this->url . "?lat={$lat}&lon={$lon}&units=metric&lang=sv&{$exclude}&appid={$api}";
67 1
        if ($lat && $lon) {
68 1
            $forecast = $this->curl->curlApi($url);
69 1
            $forecast = $this->getData($forecast);
70
        }
71
72 1
        return $forecast;
73
    }
74
75
    /**
76
     * Get weather history from 5 days ago.
77
     *
78
     * @return array
79
     */
80 1
    public function getHistory(string $lat = null, string $lon = null)
81
    {
82 1
        $forecast = null;
83 1
        $urls = [];
84 1
        $units = "units=metric";
85 1
        $lang = "lang=sv";
86 1
        $appid = "appid=" . $this->api;
87 1
        if ($lat && $lon) {
88 1
            for ($i=1; $i < 6; $i++) {
89 1
                $date = date(strtotime("-{$i} days"));
90 1
                $urls[] = $this->url . "/timemachine?lat={$lat}&lon={$lon}&dt={$date}&{$units}&{$lang}&{$appid}";
91
            }
92
93 1
            $forecast = $this->curl->multiCurlApi($urls);
94 1
            $forecast = $this->getData($forecast, "current");
95
        }
96 1
        return $forecast;
97
    }
98
99
    /**
100
     * Gets the desured data and converts the time from milliseconds to date d M Y.
101
     *
102
     * @return array
103
     */
104 2
    public function getData(array $weather, string $option = "daily")
105
    {
106 2
        $count = count($weather);
107 2
        $selected = [];
108
109 2
        if ($option === "daily") {
110 1
            if (array_key_exists($option, $weather)) {
111
                // $i = 0;
112 1
                foreach ($weather[$option] as $value) {
113 1
                    $selected[] = [
114 1
                            "lat" => $weather["lat"],
115 1
                            "lon" => $weather["lon"],
116 1
                            "date" => date('d M Y', $value["dt"]),
117 1
                            "temp_min" => $value["temp"]["min"],
118 1
                            "temp_max" => $value["temp"]["max"],
119 1
                            "wind_speed" => $value["wind_speed"],
120 1
                            "weather" => $value["weather"][0]["description"],
121 1
                            "icon" => $value["weather"][0]["icon"]
122
                    ];
123
                    // var_dump($selected);
124
                    // $i++;
125
                }
126
            }
127 1
        } elseif ($option === "current") {
128 1
            for ($i=0; $i < $count; $i++) {
129 1
                if (array_key_exists($i, $weather) && array_key_exists($option, $weather[$i])) {
130 1
                    $selected[] = [
131 1
                            "lat" => $weather[$i]["lat"],
132 1
                            "lon" => $weather[$i]["lon"],
133 1
                            "date" => date('d M Y', $weather[$i][$option]["dt"]),
134 1
                            "temp" => $weather[$i][$option]["temp"],
135 1
                            "wind_speed" => $weather[$i][$option]["wind_speed"],
136 1
                            "weather" => $weather[$i][$option]["weather"][0]["description"],
137 1
                            "icon" => $weather[$i][$option]["weather"][0]["icon"]
138
                    ];
139
                }
140
            }
141
        }
142 2
        return $selected;
143
    }
144
}
145