Passed
Push — master ( f9e900...3da772 )
by Ehsan
55s
created

Darksky::getFileContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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