Completed
Push — master ( 8ade27...a92bec )
by Paul
01:50
created

Weather::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
nc 2
nop 1
dl 0
loc 6
c 1
b 0
f 0
cc 2
ccs 5
cts 5
cp 1
crap 2
rs 10
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;
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(string $baseAddress = null, string $apiKey = null)
38
    {
39 1
        $this->data = [];
40 1
        $this->multiCurl = new MultiCurl();
41 1
        $this->api = require ANAX_INSTALL_PATH . "/config/api.php";
42 1
        $this->baseAddress = $baseAddress ?? $this->api["url"]["weather"];
43 1
        $this->apiKey = $apiKey ?? $this->api["key"]["weather"];
44 1
        $this->forecastConfig = [
45
            "exclude" => "exclude=[currently,minutely,hourly,alerts,flags]",
46
            "units" => "&units=si"
47
        ];
48 1
        $this->historyConfig = [
49
            "exclude" => "exclude=[minutely,hourly,currently,flags]",
50
            "units" => "&units=si"
51
        ];
52 1
    }
53
54
55
56
    /**
57
     * Get data with curl.
58
     *
59
     * @param string $lattitude for location.
60
     * @param string $longitude for location.
61
     * @param string $type for weather forecast or weather history.
62
     *
63
     * @return array
64
     */
65 1
    public function getAllData(string $lattitude = null, string $longitude = null, string $type = "forecast") : array
66
    {
67 1
        $forecastConfig = implode("", $this->forecastConfig);
68 1
        $historyConfig = implode("", $this->historyConfig);
69 1
        $apiRequest = [];
70 1
        $day = time();
71
72 1
        if ($type === "forecast") {
73 1
            $apiRequest[] = "$this->baseAddress/forecast/$this->apiKey/$lattitude,$longitude?$forecastConfig";
74 1
        } else if ($type === "history") {
75 1
            for ($i = 1; $i <= 30; $i++) {
76 1
                $day -= 86400;
77 1
                $apiRequest[] = "$this->baseAddress/forecast/$this->apiKey/$lattitude,$longitude,$day?$historyConfig";
78
            }
79
        }
80
81 1
        if ($apiRequest) {
82 1
            $data = $this->multiCurl->get($apiRequest);
83
84 1
            if ($data) {
85 1
                foreach ($data as $row) {
86 1
                    if (array_key_exists("daily", $row) && array_key_exists("data", $row["daily"])) {
87 1
                        array_walk($row["daily"]["data"], array('self', 'build'));
88
                    }
89
                }
90
            }
91
        }
92
93 1
        return $this->data;
94
    }
95
96
97
98
    /**
99
     * Build weather array.
100
     *
101
     * @param string $value
102
     * @param string $key
103
     *
104
     */
105 1
    public function build($data)
106
    {
107 1
        if (array_key_exists("time", $data)) {
108 1
            $time = date('d M Y', $data["time"]);
109 1
            $data["time"] = $time;
110 1
            $this->data[$time] = $data;
111
        }
112 1
    }
113
}
114