Completed
Push — master ( 2a0a89...468a99 )
by Christian
02:41
created

OpenWeatherMap   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 583
Duplicated Lines 2.74 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 12
Bugs 6 Features 2
Metric Value
wmc 43
c 12
b 6
f 2
lcom 1
cbo 7
dl 16
loc 583
ccs 0
cts 153
cp 0
rs 8.3158

13 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 7
A getRawData() 0 4 1
A getRawWeatherData() 0 6 1
A getRawHourlyForecastData() 0 6 1
A getRawDailyForecastData() 0 9 2
A cacheOrFetchResult() 0 19 3
A buildUrl() 0 11 2
B buildQueryUrlParameter() 0 13 8
A wasCached() 0 4 1
A getWeather() 5 23 3
B getWeatherForecast() 5 29 5
A getWeatherHistory() 3 14 3
B getRawWeatherHistory() 3 23 6

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like OpenWeatherMap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OpenWeatherMap, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
4
 *
5
 * @license MIT
6
 *
7
 * Please see the LICENSE file distributed with this source code for further
8
 * information regarding copyright and licensing.
9
 *
10
 * Please visit the following links to read about the usage policies and the license of
11
 * OpenWeatherMap before using this class:
12
 *
13
 * @see http://www.OpenWeatherMap.org
14
 * @see http://www.OpenWeatherMap.org/terms
15
 * @see http://openweathermap.org/appid
16
 */
17
18
namespace Cmfcmf;
19
20
use Cmfcmf\OpenWeatherMap\AbstractCache;
21
use Cmfcmf\OpenWeatherMap\CurrentWeather;
22
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
23
use Cmfcmf\OpenWeatherMap\Fetcher\CurlFetcher;
24
use Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface;
25
use Cmfcmf\OpenWeatherMap\Fetcher\FileGetContentsFetcher;
26
use Cmfcmf\OpenWeatherMap\WeatherForecast;
27
use Cmfcmf\OpenWeatherMap\WeatherHistory;
28
29
/**
30
 * Main class for the OpenWeatherMap-PHP-API. Only use this class.
31
 *
32
 * @api
33
 */
34
class OpenWeatherMap
35
{
36
    /**
37
     * @var string $weatherUrl The basic api url to fetch weather data from.
38
     */
39
    private $weatherUrl = "http://api.openweathermap.org/data/2.5/weather?";
40
41
    /**
42
     * @var string $url The basic api url to fetch weekly forecast data from.
43
     */
44
    private $weatherHourlyForecastUrl = "http://api.openweathermap.org/data/2.5/forecast?";
45
46
    /**
47
     * @var string $url The basic api url to fetch daily forecast data from.
48
     */
49
    private $weatherDailyForecastUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?";
50
51
    /**
52
     * @var string $url The basic api url to fetch history weather data from.
53
     */
54
    private $weatherHistoryUrl = "http://api.openweathermap.org/data/2.5/history/city?";
55
56
    /**
57
     * The copyright notice. This is no official text, this hint was created regarding to http://openweathermap.org/copyright.
58
     *
59
     * @var string $copyright
60
     */
61
    const COPYRIGHT = "Weather data from <a href=\"http://www.openweathermap.org\">OpenWeatherMap.org</a>";
62
63
    /**
64
     * @var \Cmfcmf\OpenWeatherMap\AbstractCache|bool $cacheClass The cache class.
65
     */
66
    private $cacheClass = false;
67
68
    /**
69
     * @var int
70
     */
71
    private $seconds;
72
73
    /**
74
     * @var bool
75
     */
76
    private $wasCached = false;
77
    
78
    /**
79
     * @var FetcherInterface The url fetcher.
80
     */
81
    
82
    private $fetcher;
83
84
    /**
85
     * Constructs the OpenWeatherMap object.
86
     *
87
     * @param null|FetcherInterface $fetcher    The interface to fetch the data from OpenWeatherMap. Defaults to
88
     *                                          CurlFetcher() if cURL is available. Otherwise defaults to
89
     *                                          FileGetContentsFetcher() using 'file_get_contents()'.
90
     * @param bool|string           $cacheClass If set to false, caching is disabled. Otherwise this must be a class
91
     *                                          extending AbstractCache. Defaults to false.
92
     * @param int                   $seconds    How long weather data shall be cached. Default 10 minutes.
93
     *
94
     * @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
95
     * @api
96
     */
97
    public function __construct($fetcher = null, $cacheClass = false, $seconds = 600)
98
    {
99
        if ($cacheClass !== false && !($cacheClass instanceof AbstractCache)) {
100
            throw new \Exception("The cache class must implement the FetcherInterface!");
101
        }
102
        if (!is_numeric($seconds)) {
103
            throw new \Exception("\$seconds must be numeric.");
104
        }
105
        if (!isset($fetcher)) {
106
            $fetcher = (function_exists('curl_version')) ? new CurlFetcher() : new FileGetContentsFetcher();
107
        }
108
        if ($seconds == 0) {
109
            $cacheClass = false;
110
        }
111
112
        $this->cacheClass = $cacheClass;
113
        $this->seconds = $seconds;
0 ignored issues
show
Documentation Bug introduced by
It seems like $seconds can also be of type double or string. However, the property $seconds is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
114
        $this->fetcher = $fetcher;
115
    }
116
117
    /**
118
     * Returns the current weather at the place you specified as an object.
119
     *
120
     * @param array|int|string $query The place to get weather information for. For possible values see below.
121
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
122
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see below.
123
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
124
     *
125
     * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
126
     * @throws \InvalidArgumentException If an argument error occurs.
127
     *
128
     * @return CurrentWeather The weather object.
129
     *
130
     * There are three ways to specify the place to get weather information for:
131
     * - Use the city name: $query must be a string containing the city name.
132
     * - Use the city id: $query must be an integer containing the city id.
133
     * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
134
     *
135
     * Available languages are (as of 17. July 2013):
136
     * - English - en
137
     * - Russian - ru
138
     * - Italian - it
139
     * - Spanish - sp
140
     * - Ukrainian - ua
141
     * - German - de
142
     * - Portuguese - pt
143
     * - Romanian - ro
144
     * - Polish - pl
145
     * - Finnish - fi
146
     * - Dutch - nl
147
     * - French - fr
148
     * - Bulgarian - bg
149
     * - Swedish - se
150
     * - Chinese Traditional - zh_tw
151
     * - Chinese Simplified - zh_cn
152
     * - Turkish - tr
153
     *
154
     * @api
155
     */
156
    public function getWeather($query, $units = 'imperial', $lang = 'en', $appid = '')
157
    {
158
        // Disable default error handling of SimpleXML (Do not throw E_WARNINGs).
159
        libxml_use_internal_errors(true);
160
        libxml_clear_errors();
161
162
        $answer = $this->getRawWeatherData($query, $units, $lang, $appid, 'xml');
163
164
        try {
165
            $xml = new \SimpleXMLElement($answer);
166
        } catch (\Exception $e) {
167
            // Invalid xml format. This happens in case OpenWeatherMap returns an error.
168
            // OpenWeatherMap always uses json for errors, even if one specifies xml as format.
169
            $error = json_decode($answer, true);
170 View Code Duplication
            if (isset($error['message'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
                throw new OWMException($error['message'], $error['cod']);
172
            } else {
173
                throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $answer);
174
            }
175
        }
176
177
        return new CurrentWeather($xml, $units);
178
    }
179
180
    /**
181
     * Returns the current weather at the place you specified as an object.
182
     *
183
     * @param array|int|string $query The place to get weather information for. For possible values see below.
184
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
185
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see below.
186
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
187
     * @param int              $days  For how much days you want to get a forecast. Default 1, maximum: 16.
188
     *
189
     * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
190
     * @throws \InvalidArgumentException If an argument error occurs.
191
     *
192
     * @return WeatherForecast The WeatherForecast object.
193
     *
194
     * There are three ways to specify the place to get weather information for:
195
     * - Use the city name: $query must be a string containing the city name.
196
     * - Use the city id: $query must be an integer containing the city id.
197
     * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
198
     *
199
     * Available languages are (as of 17. July 2013):
200
     * - English - en
201
     * - Russian - ru
202
     * - Italian - it
203
     * - Spanish - sp
204
     * - Ukrainian - ua
205
     * - German - de
206
     * - Portuguese - pt
207
     * - Romanian - ro
208
     * - Polish - pl
209
     * - Finnish - fi
210
     * - Dutch - nl
211
     * - French - fr
212
     * - Bulgarian - bg
213
     * - Swedish - se
214
     * - Chinese Traditional - zh_tw
215
     * - Chinese Simplified - zh_cn
216
     * - Turkish - tr
217
     *
218
     * @api
219
     */
220
    public function getWeatherForecast($query, $units = 'imperial', $lang = 'en', $appid = '', $days = 1)
221
    {
222
        // Disable default error handling of SimpleXML (Do not throw E_WARNINGs).
223
        libxml_use_internal_errors(true);
224
        libxml_clear_errors();
225
226
        if ($days <= 5) {
227
            $answer = $this->getRawHourlyForecastData($query, $units, $lang, $appid, 'xml');
228
        } elseif ($days <= 16) {
229
            $answer = $this->getRawDailyForecastData($query, $units, $lang, $appid, 'xml', $days);
230
        } else {
231
            throw new \InvalidArgumentException('Error: forecasts are only available for the next 16 days. $days must be lower than 17.');
232
        }
233
234
        try {
235
            $xml = new \SimpleXMLElement($answer);
236
        } catch (\Exception $e) {
237
            // Invalid xml format. This happens in case OpenWeatherMap returns an error.
238
            // OpenWeatherMap always uses json for errors, even if one specifies xml as format.
239
            $error = json_decode($answer, true);
240 View Code Duplication
            if (isset($error['message'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
                throw new OWMException($error['message'], $error['cod']);
242
            } else {
243
                throw new OWMException('Unknown fatal error: OpenWeatherMap returned the following json object: ' . $answer);
244
            }
245
        }
246
247
        return new WeatherForecast($xml, $units, $days);
248
    }
249
250
    /**
251
     * Returns the weather history for the place you specified as an object.
252
     *
253
     * @param array|int|string $query The place to get weather information for. For possible values see below.
254
     * @param \DateTime        $start
255
     * @param int              $endOrCount
256
     * @param string           $type
257
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
258
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see below.
259
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
260
     *
261
     * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error.
262
     * @throws \InvalidArgumentException If an argument error occurs.
263
     *
264
     * @return WeatherHistory The WeatherHistory object.
265
     *
266
     * There are three ways to specify the place to get weather information for:
267
     * - Use the city name: $query must be a string containing the city name.
268
     * - Use the city id: $query must be an integer containing the city id.
269
     * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
270
     *
271
     * Available languages are (as of 17. July 2013):
272
     * - English - en
273
     * - Russian - ru
274
     * - Italian - it
275
     * - Spanish - sp
276
     * - Ukrainian - ua
277
     * - German - de
278
     * - Portuguese - pt
279
     * - Romanian - ro
280
     * - Polish - pl
281
     * - Finnish - fi
282
     * - Dutch - nl
283
     * - French - fr
284
     * - Bulgarian - bg
285
     * - Swedish - se
286
     * - Chinese Traditional - zh_tw
287
     * - Chinese Simplified - zh_cn
288
     * - Turkish - tr
289
     *
290
     * @api
291
     */
292
    public function getWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $units = 'imperial', $lang = 'en', $appid = '')
293
    {
294 View Code Duplication
        if (!in_array($type, array('tick', 'hour', 'day'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
295
            throw new \InvalidArgumentException('$type must be either "tick", "hour" or "day"');
296
        }
297
298
        $xml = json_decode($this->getRawWeatherHistory($query, $start, $endOrCount, $type, $units, $lang, $appid), true);
299
300
        if ($xml['cod'] != 200) {
301
            throw new OWMException($xml['message'], $xml['cod']);
302
        }
303
304
        return new WeatherHistory($xml, $query);
305
    }
306
307
    /**
308
     * @deprecated Use {@link self::getRawWeatherData()} instead.
309
     */
310
    public function getRawData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
311
    {
312
        return $this->getRawWeatherData($query, $units, $lang, $appid, $mode);
313
    }
314
315
    /**
316
     * Directly returns the xml/json/html string returned by OpenWeatherMap for the current weather.
317
     *
318
     * @param array|int|string $query The place to get weather information for. For possible values see below.
319
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
320
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see below.
321
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
322
     * @param string           $mode  The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default).
323
     *
324
     * @return string Returns false on failure and the fetched data in the format you specified on success.
325
     *
326
     * Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
327
     *
328
     * There are three ways to specify the place to get weather information for:
329
     * - Use the city name: $query must be a string containing the city name.
330
     * - Use the city id: $query must be an integer containing the city id.
331
     * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
332
     *
333
     * Available languages are (as of 17. July 2013):
334
     * - English - en
335
     * - Russian - ru
336
     * - Italian - it
337
     * - Spanish - sp
338
     * - Ukrainian - ua
339
     * - German - de
340
     * - Portuguese - pt
341
     * - Romanian - ro
342
     * - Polish - pl
343
     * - Finnish - fi
344
     * - Dutch - nl
345
     * - French - fr
346
     * - Bulgarian - bg
347
     * - Swedish - se
348
     * - Chinese Traditional - zh_tw
349
     * - Chinese Simplified - zh_cn
350
     * - Turkish - tr
351
     *
352
     * @api
353
     */
354
    public function getRawWeatherData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
355
    {
356
        $url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherUrl);
357
358
        return $this->cacheOrFetchResult($url);
359
    }
360
361
    /**
362
     * Directly returns the xml/json/html string returned by OpenWeatherMap for the hourly forecast.
363
     *
364
     * @param array|int|string $query The place to get weather information for. For possible values see below.
365
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
366
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see below.
367
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
368
     * @param string           $mode  The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default).
369
     *
370
     * @return string Returns false on failure and the fetched data in the format you specified on success.
371
     *
372
     * Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
373
     *
374
     * There are three ways to specify the place to get weather information for:
375
     * - Use the city name: $query must be a string containing the city name.
376
     * - Use the city id: $query must be an integer containing the city id.
377
     * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
378
     *
379
     * Available languages are (as of 17. July 2013):
380
     * - English - en
381
     * - Russian - ru
382
     * - Italian - it
383
     * - Spanish - sp
384
     * - Ukrainian - ua
385
     * - German - de
386
     * - Portuguese - pt
387
     * - Romanian - ro
388
     * - Polish - pl
389
     * - Finnish - fi
390
     * - Dutch - nl
391
     * - French - fr
392
     * - Bulgarian - bg
393
     * - Swedish - se
394
     * - Chinese Traditional - zh_tw
395
     * - Chinese Simplified - zh_cn
396
     * - Turkish - tr
397
     *
398
     * @api
399
     */
400
    public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml')
401
    {
402
        $url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherHourlyForecastUrl);
403
404
        return $this->cacheOrFetchResult($url);
405
    }
406
407
    /**
408
     * Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast.
409
     *
410
     * @param array|int|string $query The place to get weather information for. For possible values see below.
411
     * @param string           $units Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
412
     * @param string           $lang  The language to use for descriptions, default is 'en'. For possible values see below.
413
     * @param string           $appid Your app id, default ''. See http://openweathermap.org/appid for more details.
414
     * @param string           $mode  The format of the data fetched. Possible values are 'json', 'html' and 'xml' (default)
415
     * @param int              $cnt   How many days of forecast shall be returned? Maximum (and default): 16
416
     *
417
     * @throws \InvalidArgumentException If $cnt is higher than 16.
418
     * @return string Returns false on failure and the fetched data in the format you specified on success.
419
     *
420
     * Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
421
     *
422
     * There are three ways to specify the place to get weather information for:
423
     * - Use the city name: $query must be a string containing the city name.
424
     * - Use the city id: $query must be an integer containing the city id.
425
     * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
426
     *
427
     * Available languages are (as of 17. July 2013):
428
     * - English - en
429
     * - Russian - ru
430
     * - Italian - it
431
     * - Spanish - sp
432
     * - Ukrainian - ua
433
     * - German - de
434
     * - Portuguese - pt
435
     * - Romanian - ro
436
     * - Polish - pl
437
     * - Finnish - fi
438
     * - Dutch - nl
439
     * - French - fr
440
     * - Bulgarian - bg
441
     * - Swedish - se
442
     * - Chinese Traditional - zh_tw
443
     * - Chinese Simplified - zh_cn
444
     * - Turkish - tr
445
     *
446
     * @api
447
     */
448
    public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en', $appid = '', $mode = 'xml', $cnt = 16)
449
    {
450
        if ($cnt > 16) {
451
            throw new \InvalidArgumentException('$cnt must be 16 or below!');
452
        }
453
        $url = $this->buildUrl($query, $units, $lang, $appid, $mode, $this->weatherDailyForecastUrl) . "&cnt=$cnt";
454
455
        return $this->cacheOrFetchResult($url);
456
    }
457
458
    /**
459
     * Directly returns the xml/json/html string returned by OpenWeatherMap for the daily forecast.
460
     *
461
     * @param array|int|string $query           The place to get weather information for. For possible values see below.
462
     * @param \DateTime        $start           The \DateTime object of the date to get the first weather information from.
463
     * @param \DateTime|int    $endOrCount      Can be either a \DateTime object representing the end of the period to
464
     *                                          receive weather history data for or an integer counting the number of
465
     *                                          reports requested.
466
     * @param string           $type            The period of the weather history requested. Can be either be either "tick",
467
     *                                          "hour" or "day".
468
     * @param string           $units           Can be either 'metric' or 'imperial' (default). This affects almost all units returned.
469
     * @param string           $lang            The language to use for descriptions, default is 'en'. For possible values see below.
470
     * @param string           $appid           Your app id, default ''. See http://openweathermap.org/appid for more details.
471
     *
472
     * @throws \InvalidArgumentException
473
     *
474
     * @return string Returns false on failure and the fetched data in the format you specified on success.
475
     *
476
     * Warning If an error occurred, OpenWeatherMap returns data in json format ALWAYS
477
     *
478
     * There are three ways to specify the place to get weather information for:
479
     * - Use the city name: $query must be a string containing the city name.
480
     * - Use the city id: $query must be an integer containing the city id.
481
     * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values.
482
     *
483
     * Available languages are (as of 17. July 2013):
484
     * - English - en
485
     * - Russian - ru
486
     * - Italian - it
487
     * - Spanish - sp
488
     * - Ukrainian - ua
489
     * - German - de
490
     * - Portuguese - pt
491
     * - Romanian - ro
492
     * - Polish - pl
493
     * - Finnish - fi
494
     * - Dutch - nl
495
     * - French - fr
496
     * - Bulgarian - bg
497
     * - Swedish - se
498
     * - Chinese Traditional - zh_tw
499
     * - Chinese Simplified - zh_cn
500
     * - Turkish - tr
501
     *
502
     * @api
503
     */
504
    public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1, $type = 'hour', $units = 'imperial', $lang = 'en', $appid = '')
505
    {
506 View Code Duplication
        if (!in_array($type, array('tick', 'hour', 'day'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
507
            throw new \InvalidArgumentException('$type must be either "tick", "hour" or "day"');
508
        }
509
510
        $queryUrl = $this->weatherHistoryUrl . $this->buildQueryUrlParameter($query) . "&start={$start->format('U')}";
511
512
        if ($endOrCount instanceof \DateTime) {
513
            $queryUrl .= "&end={$endOrCount->format('U')}";
514
        } elseif (is_numeric($endOrCount) && $endOrCount > 0) {
515
            $queryUrl .= "&cnt=$endOrCount";
516
        } else {
517
            throw new \InvalidArgumentException('$endOrCount must be either a \DateTime or a positive integer.');
518
        }
519
        $queryUrl .= "&type=$type&units=$units&lang=$lang";
520
521
        if (!empty($appid)) {
522
            $queryUrl .= "&APPID=$appid";
523
        }
524
525
        return $this->cacheOrFetchResult($queryUrl);
526
    }
527
528
    /**
529
     * Fetches the result or delivers a cached version of the result.
530
     *
531
     * @param $url
532
     *
533
     * @return string
534
     *
535
     * @internal
536
     */
537
    private function cacheOrFetchResult($url)
538
    {
539
        if ($this->cacheClass !== false) {
540
            /** @var \Cmfcmf\OpenWeatherMap\AbstractCache $cache */
541
            $cache = $this->cacheClass;
542
            $cache->setSeconds($this->seconds);
543
            $this->wasCached=false;
544
            if ($cache->isCached($url)) {
545
                $this->wasCached=true;
546
                return $cache->getCached($url);
547
            }
548
            $result = $this->fetcher->fetch($url);
549
            $cache->setCached($url, $result);
550
        } else {
551
            $result = $this->fetcher->fetch($url);
552
        }
553
554
        return $result;
555
    }
556
557
    /**
558
     * Build the url to fetch weather data from.
559
     *
560
     * @param        $query
561
     * @param        $units
562
     * @param        $lang
563
     * @param        $appid
564
     * @param        $mode
565
     * @param string $url The url to prepend.
566
     *
567
     * @return bool|string The fetched url, false on failure.
568
     *
569
     * @internal
570
     */
571
    private function buildUrl($query, $units, $lang, $appid, $mode, $url)
572
    {
573
        $queryUrl = $this->buildQueryUrlParameter($query);
574
575
        $url = $url . "$queryUrl&units=$units&lang=$lang&mode=$mode";
576
        if (!empty($appid)) {
577
            $url .= "&APPID=$appid";
578
        }
579
580
        return $url;
581
    }
582
583
    /**
584
     * Builds the query string for the url.
585
     *
586
     * @param $query
587
     *
588
     * @return string The built query string for the url.
589
     * @throws \InvalidArgumentException If the query parameter is invalid.
590
     *
591
     * @internal
592
     */
593
    private function buildQueryUrlParameter($query)
594
    {
595
        switch ($query) {
596
            case (is_array($query) && isset($query['lat']) && isset($query['lon']) && is_numeric($query['lat']) && is_numeric($query['lon'])):
597
                return "lat={$query['lat']}&lon={$query['lon']}";
598
            case (is_numeric($query)):
599
                return "id=$query";
600
            case (is_string($query)):
601
                return "q=" . urlencode($query);
602
            default:
603
                throw new \InvalidArgumentException('Error: $query has the wrong format. See the documentation of OpenWeatherMap::getRawData() to read about valid formats.');
604
        }
605
    }
606
    
607
    /**
608
     * Returns whether or not the last result was fetched from the cache.
609
     * 
610
     * @return bool true if last result was fetched from cache, otherwise false.
611
     */
612
    public function wasCached()
613
    {
614
        return $this->wasCached;
615
    }
616
}
617