Passed
Push — master ( 848507...b56281 )
by Ehsan
01:37
created

Darksky   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 81.36%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 0
dl 0
loc 173
ccs 48
cts 59
cp 0.8136
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A forecast() 0 8 2
A getKey() 0 4 1
A setKey() 0 4 1
A getLatitude() 0 4 1
A setLatitude() 0 4 1
A getLongitude() 0 4 1
A setLongitude() 0 4 1
B generateRequestUrl() 0 25 4
A validateExcludes() 0 15 4
A getLanguage() 0 4 1
A setLanguage() 0 4 1
A getUnits() 0 4 1
A setUnits() 0 9 2
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 4
    public function __construct($key, $latitude, $longitude, $lang = 'en', $units = 'auto')
32
    {
33 4
        $this->setKey($key);
34 4
        $this->setLatitude($latitude);
35 4
        $this->setLongitude($longitude);
36 4
        $this->setLanguage($lang);
37 4
        $this->setUnits($units);
38 4
    }
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 4
    public function setKey($key)
61
    {
62 4
        $this->key = $key;
63 4
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getLatitude()
69
    {
70
        return $this->latitude;
71
    }
72
73
    /**
74
     * @param string $latitude
75
     */
76 4
    public function setLatitude($latitude)
77
    {
78 4
        $this->latitude = $latitude;
79 4
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getLongitude()
85
    {
86
        return $this->longitude;
87
    }
88
89
    /**
90
     * @param string $longitude
91
     */
92 4
    public function setLongitude($longitude)
93
    {
94 4
        $this->longitude = $longitude;
95 4
    }
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
            try {
107 1
                $this->validateExcludes($exclude);
108 1
            } catch (\Exception $e) {
109 1
                throw $e;
110
            }
111
112
            $queryString['exclude'] = implode(',', $exclude);
113
        }
114
115
        if ($extend === true) {
116
            $queryString['extend'] = 'hourly';
117
        }
118
119
        return self::API_BASE_URL.'/'.$this->getKey().'/'.$this->getLatitude().','.$this->getLongitude()
120
            .'?'.http_build_query($queryString);
121
    }
122
123 1
    private function validateExcludes($exclude)
124
    {
125 1
        if (empty($exclude)) {
126
            return true;
127
        }
128
129 1
        foreach ($exclude as $anExclude) {
130 1
            if (!in_array($anExclude, self::VALID_EXCLUDE)) {
131 1
                $validExcludes = implode(',', self::VALID_EXCLUDE);
132 1
                throw new \Exception("'{$anExclude}' is not a valid exclude. Valid excludes: {$validExcludes}'");
133
            }
134
        }
135
136
        return true;
137
    }
138
139
    /**
140
     * @return string
141
     */
142 2
    public function getLanguage()
143
    {
144 2
        return $this->language;
145
    }
146
147
    /**
148
     * @param string $language
149
     */
150 4
    public function setLanguage($language)
151
    {
152 4
        $this->language = $language;
153 4
    }
154
155
    /**
156
     * @return string
157
     */
158 2
    public function getUnits()
159
    {
160 2
        return $this->units;
161
    }
162
163
    /**
164
     * @param $units
165
     *
166
     * @throws \Exception
167
     */
168 4
    public function setUnits($units)
169
    {
170 4
        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 4
        $this->units = $units;
176 4
    }
177
}
178