Passed
Push — master ( e8bd47...20d0e4 )
by Ehsan
04:19
created

Darksky::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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