1616
1717namespace Cmfcmf ;
1818
19+ use Cmfcmf \OpenWeatherMap \AbstractCache ;
1920use Cmfcmf \OpenWeatherMap \CurrentWeather ;
2021use Cmfcmf \OpenWeatherMap \Exception as OWMException ;
2122use Cmfcmf \OpenWeatherMap \Fetcher \CurlFetcher ;
@@ -77,20 +78,19 @@ class OpenWeatherMap
7778 * Constructs the OpenWeatherMap object.
7879 *
7980 * @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
8182 * 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.
8585 * @param int $seconds How long weather data shall be cached. Default 10 minutes.
8686 *
8787 * @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
8888 * @api
8989 */
9090 public function __construct ($ fetcher = null , $ cacheClass = false , $ seconds = 600 )
9191 {
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! " );
9494 }
9595 if (!is_numeric ($ seconds )) {
9696 throw new \Exception ("\$seconds must be numeric. " );
@@ -348,7 +348,7 @@ public function getRawWeatherData($query, $units = 'imperial', $lang = 'en', $ap
348348 {
349349 $ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherUrl );
350350
351- return $ this ->cacheOrFetchResult (' weather ' , $ query , $ units , $ lang , $ mode , $ url );
351+ return $ this ->cacheOrFetchResult ($ url );
352352 }
353353
354354 /**
@@ -394,7 +394,7 @@ public function getRawHourlyForecastData($query, $units = 'imperial', $lang = 'e
394394 {
395395 $ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherHourlyForecastUrl );
396396
397- return $ this ->cacheOrFetchResult (' hourlyForecast ' , $ query , $ units , $ lang , $ mode , $ url );
397+ return $ this ->cacheOrFetchResult ($ url );
398398 }
399399
400400 /**
@@ -445,7 +445,7 @@ public function getRawDailyForecastData($query, $units = 'imperial', $lang = 'en
445445 }
446446 $ url = $ this ->buildUrl ($ query , $ units , $ lang , $ appid , $ mode , $ this ->weatherDailyForecastUrl ) . "&cnt= $ cnt " ;
447447
448- return $ this ->cacheOrFetchResult (' dailyForecast ' , $ query , $ units , $ lang , $ mode , $ url );
448+ return $ this ->cacheOrFetchResult ($ url );
449449 }
450450
451451 /**
@@ -515,7 +515,7 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
515515 $ queryUrl .= "&APPID= $ appid " ;
516516 }
517517
518- return $ this ->cacheOrFetchResult (' weatherHistory ' , $ query , $ units , $ lang , $ type , $ queryUrl );
518+ return $ this ->cacheOrFetchResult ($ queryUrl );
519519 }
520520
521521 /**
@@ -532,17 +532,17 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1,
532532 *
533533 * @internal
534534 */
535- private function cacheOrFetchResult ($ type , $ query , $ units , $ lang , $ mode , $ url )
535+ private function cacheOrFetchResult ($ url )
536536 {
537537 if ($ this ->cacheClass !== false ) {
538538 /** @var \Cmfcmf\OpenWeatherMap\AbstractCache $cache */
539- $ cache = new $ this ->cacheClass ;
539+ $ cache = $ this ->cacheClass ;
540540 $ 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 );
543543 }
544544 $ result = $ this ->fetcher ->fetch ($ url );
545- $ cache ->setCached ($ type , $ result, $ query , $ units , $ lang , $ mode );
545+ $ cache ->setCached ($ url , $ result );
546546 } else {
547547 $ result = $ this ->fetcher ->fetch ($ url );
548548 }
0 commit comments