Weather   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 125
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setIp() 0 3 1
B validateCoord() 0 20 8
A weatherHistory() 0 20 2
A weatherForecast() 0 14 1
1
<?php
2
3
namespace Mahw17\Weather;
4
5
/**
6
 * Weather class, get weather info based on coordinates
7
 */
8
class Weather
9
{
10
11
    /**
12
     * API KEY.
13
     */
14
    private $apiKey = null;
15
16
    /**
17
     * API URL.
18
     */
19
    private $apiUrl = null;
20
21
    /**
22
     * API EXTENSION.
23
     */
24
    private $apiExtension = null;
25
26
    /**
27
     * IP object
28
     */
29
    public $ipObj = null;
30
31
    /**
32
     * Constructor fetching api key and api url from specific supplier.
33
     *
34
     * @param array $config containing details for connecting to the supplier API.
35
     */
36 9
    public function __construct($config)
37
    {
38 9
        $this->apiKey       = $config['darksky']['apiKey'];
39 9
        $this->apiUrl       = $config['darksky']['apiUrl'];
40 9
        $this->apiExtension = $config['darksky']['apiExtension'];
41 9
    }
42
43
    /**
44
     * Inject Ip-object into weather.
45
     *
46
     * @param object $ipObj
47
     */
48 9
    public function setIp($ipObj)
49
    {
50 9
        $this->ipObj = $ipObj;
51 9
    }
52
53
    /**
54
     * Fetch weather forecast
55
     *
56
     * @param array $coordinates contains geolocation latitude and longitude
57
     *
58
     * @return string
59
     */
60 3
    public function weatherForecast($coordinates = ["lat" => 57, "lon" => 18])
61
    {
62
        // Default value on return attributes
63 3
        $url = $this->apiUrl . $this->apiKey . '/' . $coordinates["lat"] . "," . $coordinates["lon"] . $this->apiExtension;
64
65
        // Initiate new curl object
66 3
        $curl = new \Mahw17\Weather\Curl();
67
68
        // Get response
69 3
        $content = $curl->fetchSingleUrl($url);
70 3
        $result = json_decode($content);
71
72
        // Collect and return results
73 3
        return $result;
74
    }
75
76
    /**
77
     * Fetch weather forecast
78
     *
79
     * @param int   $day Current day in unix timestamp
80
     * @param array $coordinates geo coordinates
81
     *
82
     * @return array
83
     */
84 3
    public function weatherHistory($day = 1542818124, $coordinates = ["lat" => 57, "lon" => 18])
85
    {
86
        // Create $urlArray
87 3
        $urlArray = array();
88 3
        for ($i = 0; $i < 30; $i++) {
89
            // Day to collect weather data for
90 3
            $day = $day - 24 * 60 * 60;
91
92
            // API url
93 3
            $urlArray[$i] = $this->apiUrl . $this->apiKey . '/' . $coordinates["lat"] . "," . $coordinates["lon"] . "," . $day . $this->apiExtension;
94
        }
95
96
        // Initiate new curl object
97 3
        $curl = new \Mahw17\Weather\Curl();
98
99
        // Get response
100 3
        $result = $curl->fetchMultiUrl($urlArray);
101
102
        // Collect and return results
103 3
        return $result;
104
    }
105
106
    /**
107
     * Validate coordinates
108
     *
109
     * @param string $coordinates
110
     *
111
     * @return array|boolean|string with coordinates, false if not valid
112
     */
113 4
    public function validateCoord($coordinates)
114
    {
115
        // Convert string to array
116 4
        $coord = explode(",", $coordinates);
117
118
        // Validate coord
119 4
        $coordValid = false;
120 4
        if (isset($coord[0]) && isset($coord[1])) {
121
            $coordinates = [
122 3
                "lat" => $coord[0],
123 3
                "lon" => $coord[1]
124
            ];
125 3
            $coordValid = $coordinates["lat"] >= -90 &&
126 3
                          $coordinates["lat"] <= 90 &&
127 3
                          $coordinates["lon"] >= -180 &&
128 3
                          $coordinates["lon"] <= 180
129 3
                          ? true : false;
130
        }
131
132 4
        return $coordValid ? $coordinates : false;
133
    }
134
}
135