Move caching into Metar base class, clean up of caller in the widget

This commit is contained in:
Nabeel Shahzad
2018-04-02 22:44:31 -05:00
parent 717118cfb4
commit 2351dbe717
3 changed files with 50 additions and 19 deletions

View File

@@ -2,12 +2,49 @@
namespace App\Interfaces;
use Cache;
use Log;
/**
* Base class for implementing retrieving METARs
* @package App\Interfaces
*/
abstract class Metar
{
/**
* Implement the METAR- Return the string
* Implement retrieving the METAR- Return the string
* Needs to be protected, since this shouldn't be
* directly called. Call `get_metar($icao)` instead
* @param $icao
* @return mixed
*/
abstract public function get($icao);
abstract protected function metar($icao): string;
/**
* @param $icao
* @return string
*/
//abstract protected function taf($icao): string;
/**
* Download the METAR, wrap in caching
* @param $icao
* @return string
*/
public function get_metar($icao): string
{
$cache = config('cache.keys.WEATHER_LOOKUP');
$key = $cache['key'].$icao;
$raw_metar = Cache::remember($key, $cache['time'], function () use ($icao) {
try {
return $this->metar($icao);
} catch (\Exception $e) {
Log::error('Error getting METAR: '. $e->getMessage(), $e->getTrace());
return '';
}
});
return $raw_metar;
}
}