Passed
Push — master ( 86479e...2843a8 )
by Marcus
02:54
created

Weather::weatherForecast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 15
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
     * API EXTENSION.
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 $ccoordinates contains geolocation lat and long
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
66
        // Initiate new curl object
67 3
        $curl = new \Mahw17\Weather\Curl();
68
69
        // Get response
70 3
        $content = $curl->fetchSingleUrl($url);
71 3
        $result = json_decode($content);
72
73
        // Collect and return results
74 3
        return $result;
75
    }
76
77
    /**
78
     * Fetch weather forecast
79
     *
80
     * @param int $day Current day in unix timestamp
81
     * @param array $coordinates geo coordinates
82
     *
83
     * @return array
84
     */
85 3
    public function weatherHistory($day = 1542818124, $coordinates = ["lat" => 57, "lon" => 18])
86
    {
87
        // Create $urlArray
88 3
        $urlArray = array();
89 3
        for ($i = 0; $i < 30; $i++) {
90
            // Day to collect weather data for
91 3
            $day = $day - 24 * 60 * 60;
92
93
            // API url
94 3
            $urlArray[$i] = $this->apiUrl . $this->apiKey . '/' . $coordinates["lat"] . "," . $coordinates["lon"] . "," . $day . $this->apiExtension;
95
        }
96
97
        // Initiate new curl object
98 3
        $curl = new \Mahw17\Weather\Curl();
99
100
        // Get response
101 3
        $result = $curl->fetchMultiUrl($urlArray);
102
        // $result = json_decode($result);
103
104
        // Collect and return results
105 3
        return $result;
106
    }
107
108
    /**
109
     * Validate coordinates
110
     *
111
     * @param string $coordinates
112
     *
113
     * @return array|boolean|string with coordinates, false if not valid
114
     */
115 4
    public function validateCoord($coordinates)
116
    {
117
        // Convert string to array
118 4
        $coord = explode(",", $coordinates);
119
120
        // Validate coord
121 4
        $coordValid = false;
122 4
        if (isset($coord[0]) && isset($coord[1])) {
123
            $coordinates = [
124 3
                "lat" => $coord[0],
125 3
                "lon" => $coord[1]
126
            ];
127 3
            $coordValid = $coordinates["lat"] >= -90 &&
128 3
                          $coordinates["lat"] <= 90 &&
129 3
                          $coordinates["lon"] >= -180 &&
130 3
                          $coordinates["lon"] <= 180
131 3
                          ? true : false;
132
        }
133
134 4
        return $coordValid ? $coordinates : false;
135
    }
136
}
137