Passed
Push — master ( 241666...99cffd )
by Ehsan
01:41
created

Darksky::validateExcludes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 4.3731
1
<?php
2
3
namespace Darksky;
4
5
class Darksky
6
{
7
    const API_BASE_URL = 'https://api.darksky.net/forecast';
8
    const VALID_UNITS = [
9
        'auto',
10
        'ca',
11
        'uk2',
12
        'us',
13
        'si',
14
    ];
15
16
    const VALID_EXCLUDE = [
17
        'currently',
18
        'minutely',
19
        'hourly',
20
        'daily',
21
        'alerts',
22
        'flags',
23
    ];
24
25
    private $key;
26
    private $latitude;
27
    private $longitude;
28
    private $language;
29
    private $units;
30
31 6
    public function __construct($key, $latitude, $longitude, $lang = 'en', $units = 'auto')
32
    {
33 6
        $this->setKey($key);
34 6
        $this->setLatitude($latitude);
35 6
        $this->setLongitude($longitude);
36 6
        $this->setLanguage($lang);
37 6
        $this->setUnits($units);
38 6
    }
39
40 1
    public function forecast(array $exclude = [], $extend = false)
41
    {
42
        try {
43 1
            return file_get_contents($this->generateRequestUrl($exclude, $extend));
44 1
        } catch (\Exception $e) {
45 1
            throw $e;
46
        }
47
    }
48
49
    /**
50
     * @return string
51
     */
52 1
    public function getKey()
53
    {
54 1
        return $this->key;
55
    }
56
57
    /**
58
     * @param string $key
59
     */
60 6
    public function setKey($key)
61
    {
62 6
        $this->key = $key;
63 6
    }
64
65
    /**
66
     * @return string
67
     */
68 1
    public function getLatitude()
69
    {
70 1
        return $this->latitude;
71
    }
72
73
    /**
74
     * @param string $latitude
75
     */
76 6
    public function setLatitude($latitude)
77
    {
78 6
        $this->latitude = $latitude;
79 6
    }
80
81
    /**
82
     * @return string
83
     */
84 1
    public function getLongitude()
85
    {
86 1
        return $this->longitude;
87
    }
88
89
    /**
90
     * @param string $longitude
91
     */
92 6
    public function setLongitude($longitude)
93
    {
94 6
        $this->longitude = $longitude;
95 6
    }
96
97 1
    private function generateRequestUrl(array $exclude = [], $extend = false)
98
    {
99 1
        $queryString = ['lang'  => $this->getLanguage(), 'units' => $this->getUnits()];
100
101 1
        if (!empty($exclude)) {
102
            // validate $exclude
103 1
            if ($this->validateExcludes($exclude) !== true) {
104 1
                $validExcludes = implode(',', self::VALID_EXCLUDE);
105 1
                throw new \Exception("Invalid excludes. Provide valid excludes: {$validExcludes}'");
106
            }
107
108
            $queryString['exclude'] = implode(',', $exclude);
109
        }
110
111
        if ($extend === true) {
112
            $queryString['extend'] = 'hourly';
113
        }
114
115
        return self::API_BASE_URL.'/'.$this->getKey().'/'.$this->getLatitude().','.$this->getLongitude()
116
            .'?'.http_build_query($queryString);
117
    }
118
119 1
    private function validateExcludes($exclude)
120
    {
121 1
        if (empty($exclude)) {
122
            return true;
123
        }
124
125 1
        foreach ($exclude as $anExclude) {
126 1
            if (!in_array($anExclude, self::VALID_EXCLUDE)) {
127 1
                return false;
128
            }
129
        }
130
131
        return true;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 2
    public function getLanguage()
138
    {
139 2
        return $this->language;
140
    }
141
142
    /**
143
     * @param string $language
144
     */
145 6
    public function setLanguage($language)
146
    {
147 6
        $this->language = $language;
148 6
    }
149
150
    /**
151
     * @return string
152
     */
153 2
    public function getUnits()
154
    {
155 2
        return $this->units;
156
    }
157
158
    /**
159
     * @param $units
160
     *
161
     * @throws \Exception
162
     */
163 6
    public function setUnits($units)
164
    {
165 6
        if (!in_array($units, self::VALID_UNITS)) {
166 1
            $validUnits = implode(',', self::VALID_UNITS);
167 1
            throw new \Exception("'{$units}' is not a valid unit. Valid units: {$validUnits}");
168
        }
169
170 6
        $this->units = $units;
171 6
    }
172
}
173