Passed
Push — master ( 0b97e0...dfe777 )
by Ehsan
01:32
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 string $lang
39
     * @param string $units
40
     */
41 7
    public function __construct(
42
        string $key,
43
        string $lang = 'en',
44
        string $units = 'auto'
45
    ) {
46 7
        $this->setKey($key);
47 7
        $this->setLanguage($lang);
48 7
        $this->setUnits($units);
49 7
    }
50
51
    /**
52
     * @param       $latitude
53
     * @param       $longitude
54
     * @param array $exclude
55
     * @param bool  $extend
56
     *
57
     * @throws \Exception
58
     *
59
     * @return string
60
     */
61 3
    public function forecast($latitude, $longitude, array $exclude = [], $extend = false)
62
    {
63
        try {
64 3
            return file_get_contents($this->generateRequestUrl($latitude, $longitude, $exclude, $extend));
65 3
        } catch (\Exception $e) {
66 3
            throw $e;
67
        }
68
    }
69
70
    /**
71
     * @param $latitude
72
     * @param $longitude
73
     * @param $time
74
     * @param array $exclude
75
     *
76
     * @throws \Exception
77
     *
78
     * @return bool|string
79
     */
80 1
    public function timeMachine($latitude, $longitude, string $time, array $exclude = [])
81
    {
82
        try {
83 1
            return file_get_contents($this->generateRequestUrl($latitude, $longitude, $exclude, false, $time));
84 1
        } catch (\Exception $e) {
85 1
            throw $e;
86
        }
87
    }
88
89
    /**
90
     * @return string
91
     */
92 5
    public function getKey(): string
93
    {
94 5
        return $this->key;
95
    }
96
97
    /**
98
     * @param string $key
99
     */
100 7
    public function setKey(string $key)
101
    {
102 7
        $this->key = $key;
103 7
    }
104
105
    /**
106
     * @param array  $exclude
107
     * @param bool   $extend
108
     * @param string $time
109
     *
110
     * @return string
111
     */
112 4
    private function generateRequestUrl($latitude, $longitude, array $exclude = [], $extend = false, string $time = ''): string
113
    {
114 4
        if (!empty($time)) {
115 1
            $time = ",{$time}";
116
        }
117
118 4
        return self::API_BASE_URL.'/'.$this->getKey().'/'.$latitude.','.$longitude.$time
119 4
            .'?'.$this->generateUrlQueryString($exclude, $extend);
120
    }
121
122
    /**
123
     * @param array $exclude
124
     * @param bool  $extend
125
     *
126
     * @throws \Exception
127
     *
128
     * @return string
129
     */
130 4
    private function generateUrlQueryString(array $exclude = [], $extend = false): string
131
    {
132 4
        $queryString = ['lang' => $this->getLanguage(), 'units' => $this->getUnits()];
133
134
        // validate $exclude
135 4
        if ($this->validateExcludes($exclude) !== true) {
136 1
            $validExcludes = implode(',', self::VALID_EXCLUDE);
137
138 1
            throw new DarkskyException("Invalid excludes. Provide valid excludes: {$validExcludes}'");
139
        }
140
141 3
        if (!empty($exclude)) {
142 1
            $queryString['exclude'] = implode(',', $exclude);
143
        }
144
145 3
        if ($extend === true) {
146 2
            $queryString['extend'] = 'hourly';
147
        }
148
149 3
        return http_build_query($queryString);
150
    }
151
152
    /**
153
     * @param $exclude
154
     *
155
     * @return bool
156
     */
157 4
    private function validateExcludes(array $exclude): bool
158
    {
159 4
        if (empty($exclude)) {
160 2
            return true;
161
        }
162
163 2
        foreach ($exclude as $anExclude) {
164 2
            if (!in_array($anExclude, self::VALID_EXCLUDE)) {
165 2
                return false;
166
            }
167
        }
168
169 1
        return true;
170
    }
171
172
    /**
173
     * @return string
174
     */
175 5
    public function getLanguage(): string
176
    {
177 5
        return $this->language;
178
    }
179
180
    /**
181
     * @param string $language
182
     */
183 7
    public function setLanguage(string $language)
184
    {
185 7
        $this->language = $language;
186 7
    }
187
188
    /**
189
     * @return string
190
     */
191 5
    public function getUnits(): string
192
    {
193 5
        return $this->units;
194
    }
195
196
    /**
197
     * @param $units
198
     *
199
     * @throws \Exception
200
     */
201 7
    public function setUnits(string $units)
202
    {
203 7
        if (!in_array($units, self::VALID_UNITS)) {
204 1
            $validUnits = implode(',', self::VALID_UNITS);
205
206 1
            throw new DarkskyException("'{$units}' is not a valid unit. Valid units: {$validUnits}");
207
        }
208
209 7
        $this->units = $units;
210 7
    }
211
}
212