16
16
17
17
namespace Cmfcmf ;
18
18
19
+ use Cmfcmf \OpenWeatherMap \AbstractCache ;
19
20
use Cmfcmf \OpenWeatherMap \CurrentWeather ;
20
21
use Cmfcmf \OpenWeatherMap \Exception as OWMException ;
21
22
use Cmfcmf \OpenWeatherMap \Fetcher \CurlFetcher ;
@@ -77,20 +78,19 @@ class OpenWeatherMap
77
78
* Constructs the OpenWeatherMap object.
78
79
*
79
80
* @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
80
- * CurlFetcher() if if cURL is available. Otherwise defaults to
81
+ * CurlFetcher() if cURL is available. Otherwise defaults to
81
82
* FileGetContentsFetcher() using 'file_get_contents()'.
82
- * @param bool|string $cacheClass If set to false, caching is disabled. If this is a valid class
83
- * extending Cmfcmf\OpenWeatherMap\Util\Cache, caching will be enabled.
84
- * Default false.
83
+ * @param bool|string $cacheClass If set to false, caching is disabled. Otherwise this must be a class
84
+ * extending AbstractCache. Defaults to false.
85
85
* @param int $seconds How long weather data shall be cached. Default 10 minutes.
86
86
*
87
87
* @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
88
88
* @api
89
89
*/
90
90
public function __construct ($ fetcher = null , $ cacheClass = false , $ seconds = 600 )
91
91
{
92
- if ($ cacheClass !== false && !class_exists ($ cacheClass )) {
93
- throw new \Exception ("Class $ cacheClass does not exist. " );
92
+ if ($ cacheClass !== false && !($ cacheClass instanceof AbstractCache )) {
93
+ throw new \Exception ("The cache class must implement the FetcherInterface! " );
94
94
}
95
95
if (!is_numeric ($ seconds )) {
96
96
throw new \Exception ("\$seconds must be numeric. " );
@@ -348,7 +348,7 @@ public function getRawWeatherData($query, $units = 'imperial', $lang = 'en', $ap
348
348
{
349
349
$ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherUrl );
350
350
351
- return $ this ->cacheOrFetchResult (' weather ' , $ query , $ units , $ lang , $ mode , $ url );
351
+ return $ this ->cacheOrFetchResult ($ url );
352
352
}
353
353
354
354
/**
@@ -394,7 +394,7 @@ public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'e
394
394
{
395
395
$ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherHourlyForecastUrl );
396
396
397
- return $ this ->cacheOrFetchResult (' hourlyForecast ' , $ query , $ units , $ lang , $ mode , $ url );
397
+ return $ this ->cacheOrFetchResult ($ url );
398
398
}
399
399
400
400
/**
@@ -445,7 +445,7 @@ public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en
445
445
}
446
446
$ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherDailyForecastUrl ) . "&cnt= $ cnt " ;
447
447
448
- return $ this ->cacheOrFetchResult (' dailyForecast ' , $ query , $ units , $ lang , $ mode , $ url );
448
+ return $ this ->cacheOrFetchResult ($ url );
449
449
}
450
450
451
451
/**
@@ -515,7 +515,7 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
515
515
$ queryUrl .= "&APPID= $ appid " ;
516
516
}
517
517
518
- return $ this ->cacheOrFetchResult (' weatherHistory ' , $ query , $ units , $ lang , $ type , $ queryUrl );
518
+ return $ this ->cacheOrFetchResult ($ queryUrl );
519
519
}
520
520
521
521
/**
@@ -532,17 +532,17 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
532
532
*
533
533
* @internal
534
534
*/
535
- private function cacheOrFetchResult ($ type , $ query , $ units , $ lang , $ mode , $ url )
535
+ private function cacheOrFetchResult ($ url )
536
536
{
537
537
if ($ this ->cacheClass !== false ) {
538
538
/** @var \Cmfcmf\OpenWeatherMap\AbstractCache $cache */
539
- $ cache = new $ this ->cacheClass ;
539
+ $ cache = $ this ->cacheClass ;
540
540
$ cache ->setSeconds ($ this ->seconds );
541
- if ($ cache ->isCached ($ type , $ query , $ units , $ lang , $ mode )) {
542
- return $ cache ->getCached ($ type , $ query , $ units , $ lang , $ mode );
541
+ if ($ cache ->isCached ($ url )) {
542
+ return $ cache ->getCached ($ url );
543
543
}
544
544
$ result = $ this ->fetcher ->fetch ($ url );
545
- $ cache ->setCached ($ type , $ result, $ query , $ units , $ lang , $ mode );
545
+ $ cache ->setCached ($ url , $ result );
546
546
} else {
547
547
$ result = $ this ->fetcher ->fetch ($ url );
548
548
}
0 commit comments