diff --git a/app/Interfaces/Metar.php b/app/Interfaces/Metar.php index 2c21dac8..29d57d72 100644 --- a/app/Interfaces/Metar.php +++ b/app/Interfaces/Metar.php @@ -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; + } } diff --git a/app/Services/Metar/AviationWeather.php b/app/Services/Metar/AviationWeather.php index 6385b3ef..fb7d5f7f 100644 --- a/app/Services/Metar/AviationWeather.php +++ b/app/Services/Metar/AviationWeather.php @@ -11,19 +11,19 @@ use App\Support\Http; */ class AviationWeather extends Metar { - private const URL = + private const METAR_URL = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?' .'dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3' .'&mostRecent=true&fields=raw_text&stationString='; /** - * Implement the METAR- Return the string + * Implement the METAR - Return the string * @param $icao - * @return mixed + * @return string */ - public function get($icao) + protected function metar($icao): string { - $url = static::URL.$icao; + $url = static::METAR_URL.$icao; $res = Http::get($url, []); $xml = simplexml_load_string($res); return $xml->data->METAR->raw_text; diff --git a/app/Widgets/Weather.php b/app/Widgets/Weather.php index 578e9e4f..104bdb3d 100644 --- a/app/Widgets/Weather.php +++ b/app/Widgets/Weather.php @@ -51,18 +51,12 @@ class Weather extends Widget */ public function run() { - // Cache the request so we don't need to repeatedly call out - $cache = config('cache.keys.WEATHER_LOOKUP'); - $key = $cache['key'].$this->config['icao']; - - $raw_metar = Cache::remember($key, $cache['time'], function () { - /** - * @var \App\Interfaces\Metar $klass - */ - $klass = config('phpvms.metar'); - $metar_class = new $klass; - return $metar_class->get($this->config['icao']); - }); + /** + * @var \App\Interfaces\Metar + */ + $klass = config('phpvms.metar'); + $metar_class = new $klass; + $raw_metar = $metar_class->get_metar($this->config['icao']); // Run through this parser $decoder = new MetarDecoder();