Passed
Push — master ( 2e83a8...782262 )
by Ehsan
01:31
created

Darksky::setLatitude()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
        $queryString = [
100 1
            'lang'  => $this->getLanguage(),
101 1
            'units' => $this->getUnits(),
102
        ];
103
104 1
        if (!empty($exclude)) {
105
            // validate $exclude
106 1
            if ($this->validateExcludes($exclude) !== true) {
107 1
                $validExcludes = implode(',', self::VALID_EXCLUDE);
108 1
                throw new \Exception("Invalid excludes. Provide valid excludes: {$validExcludes}'");
109
            }
110
111
            $queryString['exclude'] = implode(',', $exclude);
112
        }
113
114
        if ($extend === true) {
115
            $queryString['extend'] = 'hourly';
116
        }
117
118
        return self::API_BASE_URL.'/'.$this->getKey().'/'.$this->getLatitude().','.$this->getLongitude()
119
            .'?'.http_build_query($queryString);
120
    }
121
122 1
    private function validateExcludes($exclude)
123
    {
124 1
        if (empty($exclude)) {
125
            return true;
126
        }
127
128 1
        foreach ($exclude as $anExclude) {
129 1
            if (!in_array($anExclude, self::VALID_EXCLUDE)) {
130 1
                return false;
131
            }
132
        }
133
134
        return true;
135
    }
136
137
    /**
138
     * @return string
139
     */
140 2
    public function getLanguage()
141
    {
142 2
        return $this->language;
143
    }
144
145
    /**
146
     * @param string $language
147
     */
148 6
    public function setLanguage($language)
149
    {
150 6
        $this->language = $language;
151 6
    }
152
153
    /**
154
     * @return string
155
     */
156 2
    public function getUnits()
157
    {
158 2
        return $this->units;
159
    }
160
161
    /**
162
     * @param $units
163
     *
164
     * @throws \Exception
165
     */
166 6
    public function setUnits($units)
167
    {
168 6
        if (!in_array($units, self::VALID_UNITS)) {
169 1
            $validUnits = implode(',', self::VALID_UNITS);
170 1
            throw new \Exception("'{$units}' is not a valid unit. Valid units: {$validUnits}");
171
        }
172
173 6
        $this->units = $units;
174 6
    }
175
}
176