Completed
Push — master ( a92bec...652d33 )
by Paul
02:23
created

Weather   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
c 2
b 0
f 0
dl 0
loc 110
ccs 34
cts 36
cp 0.9444
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getAllData() 0 29 9
A build() 0 6 2
A init() 0 23 3
1
<?php
2
3
namespace Pamo\Weather;
4
5
use Pamo\MultiCurl\MultiCurl;
6
7
/**
8
 * IP validator
9
 */
10
class Weather
11
{
12
    /**
13
     * @var array $data to store weather data.
14
     * @var object $multiCurl tool.
15
     * @var array $api all available API's.
16
     * @var string $baseAddress for the API.
17
     * @var string $apiKey for authentication.
18
     * @var array $config for API url.
19
     */
20
    private $data;
21
    private $multiCurl;
22
    private $api;
0 ignored issues
show
introduced by
The private property $api is not used, and could be removed.
Loading history...
23
    private $baseAddress;
24
    private $apiKey;
25
    private $forecastConfig;
26
    private $historyConfig;
27
28
29
30
    /**
31
     * Geotag current IP
32
     *
33
     * @param string $baseAddress for the API.
34
     * @param string $apiKey for authentication.
35
     *
36
     */
37 1
    public function init()
38
    {
39 1
        $this->data = [];
40 1
        $this->multiCurl = new MultiCurl();
41
42 1
        $filename = ANAX_INSTALL_PATH . "/config/api.php";
43 1
        $api =  file_exists($filename) ? require $filename : null;
44
45 1
        if ($api) {
46
            $this->baseAddress = $api["url"]["weather"];
47
            $this->apiKey = $api["key"]["weather"];
48
        } else {
49 1
            $this->baseAddress = getenv("API_URL_WEATHER");
50 1
            $this->apiKey = getenv("API_KEY_WEATHER");
51
        }
52
53 1
        $this->forecastConfig = [
54
            "exclude" => "exclude=[currently,minutely,hourly,alerts,flags]",
55
            "units" => "&units=si"
56
        ];
57 1
        $this->historyConfig = [
58
            "exclude" => "exclude=[minutely,hourly,currently,flags]",
59
            "units" => "&units=si"
60
        ];
61 1
    }
62
63
64
65
    /**
66
     * Get data with curl.
67
     *
68
     * @param string $lattitude for location.
69
     * @param string $longitude for location.
70
     * @param string $type for weather forecast or weather history.
71
     *
72
     * @return array
73
     */
74 1
    public function getAllData(string $lattitude = null, string $longitude = null, string $type = "forecast") : array
75
    {
76 1
        $forecastConfig = implode("", $this->forecastConfig);
77 1
        $historyConfig = implode("", $this->historyConfig);
78 1
        $apiRequest = [];
79 1
        $day = time();
80
81 1
        if ($type === "forecast") {
82 1
            $apiRequest[] = "$this->baseAddress/forecast/$this->apiKey/$lattitude,$longitude?$forecastConfig";
83 1
        } else if ($type === "history") {
84 1
            for ($i = 1; $i <= 30; $i++) {
85 1
                $day -= 86400;
86 1
                $apiRequest[] = "$this->baseAddress/forecast/$this->apiKey/$lattitude,$longitude,$day?$historyConfig";
87
            }
88
        }
89
90 1
        if ($apiRequest) {
91 1
            $data = $this->multiCurl->get($apiRequest);
92
93 1
            if ($data) {
94 1
                foreach ($data as $row) {
95 1
                    if (array_key_exists("daily", $row) && array_key_exists("data", $row["daily"])) {
96 1
                        array_walk($row["daily"]["data"], array('self', 'build'));
97
                    }
98
                }
99
            }
100
        }
101
102 1
        return $this->data;
103
    }
104
105
106
107
    /**
108
     * Build weather array.
109
     *
110
     * @param string $value
111
     * @param string $key
112
     *
113
     */
114 1
    public function build($data)
115
    {
116 1
        if (array_key_exists("time", $data)) {
117 1
            $time = date('d M Y', $data["time"]);
118 1
            $data["time"] = $time;
119 1
            $this->data[$time] = $data;
120
        }
121 1
    }
122
}
123