Passed
Push — master ( 109997...1fb8bf )
by Ehsan
01:35
created

Darksky::getUnits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Darksky;
4
5
/**
6
 * Class Darksky.
7
 */
8
class Darksky
9
{
10
    const API_BASE_URL = 'https://api.darksky.net/forecast';
11
    const VALID_UNITS = [
12
        'auto',
13
        'ca',
14
        'uk2',
15
        'us',
16
        'si',
17
    ];
18
19
    const VALID_EXCLUDE = [
20
        'currently',
21
        'minutely',
22
        'hourly',
23
        'daily',
24
        'alerts',
25
        'flags',
26
    ];
27
28
    private $key;
29
    private $latitude;
30
    private $longitude;
31
    private $language;
32
    private $units;
33
34
    /**
35
     * Darksky constructor.
36
     *
37
     * @param        $key
38
     * @param        $latitude
39
     * @param        $longitude
40
     * @param string $lang
41
     * @param string $units
42
     */
43 9
    public function __construct($key, $latitude, $longitude, $lang = 'en', $units = 'auto')
44
    {
45 9
        $this->setKey($key);
46 9
        $this->setLatitude($latitude);
47 9
        $this->setLongitude($longitude);
48 9
        $this->setLanguage($lang);
49 9
        $this->setUnits($units);
50 9
    }
51
52
    /**
53
     * @param array $exclude
54
     * @param bool  $extend
55
     *
56
     * @throws \Exception
57
     *
58
     * @return string
59
     */
60 3
    public function forecast(array $exclude = [], $extend = false)
61
    {
62
        try {
63 3
            return file_get_contents($this->generateRequestUrl($exclude, $extend));
64 3
        } catch (\Exception $e) {
65 3
            throw $e;
66
        }
67
    }
68
69
    /**
70
     * @param $time
71
     * @param array $exclude
72
     *
73
     * @throws \Exception
74
     *
75
     * @return bool|string
76
     */
77 1
    public function timeMachine($time, array $exclude = [])
78
    {
79
        try {
80 1
            return file_get_contents($this->generateRequestUrl($exclude, false, $time));
81 1
        } catch (\Exception $e) {
82 1
            throw $e;
83
        }
84
    }
85
86
    /**
87
     * @return string
88
     */
89 5
    public function getKey()
90
    {
91 5
        return $this->key;
92
    }
93
94
    /**
95
     * @param string $key
96
     */
97 9
    public function setKey($key)
98
    {
99 9
        $this->key = $key;
100 9
    }
101
102
    /**
103
     * @return string
104
     */
105 5
    public function getLatitude()
106
    {
107 5
        return $this->latitude;
108
    }
109
110
    /**
111
     * @param string $latitude
112
     */
113 9
    public function setLatitude($latitude)
114
    {
115 9
        $this->latitude = $latitude;
116 9
    }
117
118
    /**
119
     * @return string
120
     */
121 5
    public function getLongitude()
122
    {
123 5
        return $this->longitude;
124
    }
125
126
    /**
127
     * @param string $longitude
128
     */
129 9
    public function setLongitude($longitude)
130
    {
131 9
        $this->longitude = $longitude;
132 9
    }
133
134
    /**
135
     * @param array $exclude
136
     * @param bool  $extend
137
     *
138
     * @return string
139
     */
140 4
    private function generateRequestUrl(array $exclude = [], $extend = false, $time = '')
141
    {
142 4
        if (!empty($time)) {
143 1
            $time = ",{$time}";
144
        }
145
146 4
        return self::API_BASE_URL.'/'.$this->getKey().'/'.$this->getLatitude().','.$this->getLongitude().$time
147 4
            .'?'.$this->generateUrlQueryString($exclude, $extend);
148
    }
149
150
    /**
151
     * @param array $exclude
152
     * @param bool  $extend
153
     *
154
     * @throws \Exception
155
     *
156
     * @return string
157
     */
158 4
    private function generateUrlQueryString(array $exclude = [], $extend = false)
159
    {
160 4
        $queryString = ['lang'  => $this->getLanguage(), 'units' => $this->getUnits()];
161
162
        // validate $exclude
163 4
        if ($this->validateExcludes($exclude) !== true) {
164 1
            $validExcludes = implode(',', self::VALID_EXCLUDE);
165
166 1
            throw new \Exception("Invalid excludes. Provide valid excludes: {$validExcludes}'");
167
        }
168
169 3
        if (!empty($exclude)) {
170 1
            $queryString['exclude'] = implode(',', $exclude);
171
        }
172
173 3
        if ($extend === true) {
174 2
            $queryString['extend'] = 'hourly';
175
        }
176
177 3
        return http_build_query($queryString);
178
    }
179
180
    /**
181
     * @param $exclude
182
     *
183
     * @return bool
184
     */
185 4
    private function validateExcludes($exclude)
186
    {
187 4
        if (empty($exclude)) {
188 2
            return true;
189
        }
190
191 2
        foreach ($exclude as $anExclude) {
192 2
            if (!in_array($anExclude, self::VALID_EXCLUDE)) {
193 2
                return false;
194
            }
195
        }
196
197 1
        return true;
198
    }
199
200
    /**
201
     * @return string
202
     */
203 5
    public function getLanguage()
204
    {
205 5
        return $this->language;
206
    }
207
208
    /**
209
     * @param string $language
210
     */
211 9
    public function setLanguage($language)
212
    {
213 9
        $this->language = $language;
214 9
    }
215
216
    /**
217
     * @return string
218
     */
219 5
    public function getUnits()
220
    {
221 5
        return $this->units;
222
    }
223
224
    /**
225
     * @param $units
226
     *
227
     * @throws \Exception
228
     */
229 9
    public function setUnits($units)
230
    {
231 9
        if (!in_array($units, self::VALID_UNITS)) {
232 1
            $validUnits = implode(',', self::VALID_UNITS);
233
234 1
            throw new \Exception("'{$units}' is not a valid unit. Valid units: {$validUnits}");
235
        }
236
237 9
        $this->units = $units;
238 9
    }
239
}
240