2018-04-03 11:35:25 +08:00
|
|
|
<?php
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
namespace App\Contracts;
|
2018-04-03 11:35:25 +08:00
|
|
|
|
2018-04-03 11:44:31 +08:00
|
|
|
use Cache;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for implementing retrieving METARs
|
|
|
|
*/
|
2018-04-03 11:35:25 +08:00
|
|
|
abstract class Metar
|
|
|
|
{
|
|
|
|
/**
|
2018-04-03 11:44:31 +08:00
|
|
|
* Implement retrieving the METAR- Return the string
|
|
|
|
* Needs to be protected, since this shouldn't be
|
|
|
|
* directly called. Call `get_metar($icao)` instead
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 11:35:25 +08:00
|
|
|
* @param $icao
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 11:35:25 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-04-03 11:44:31 +08:00
|
|
|
abstract protected function metar($icao): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $icao
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 11:44:31 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
//abstract protected function taf($icao): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download the METAR, wrap in caching
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 11:44:31 +08:00
|
|
|
* @param $icao
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 11:44:31 +08:00
|
|
|
* @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);
|
2018-04-04 22:04:14 +08:00
|
|
|
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
|
|
|
|
Log::error('Error getting METAR: '.$e->getMessage(), $e->getTrace());
|
|
|
|
return '';
|
2018-04-03 11:44:31 +08:00
|
|
|
} catch (\Exception $e) {
|
2018-08-27 00:40:04 +08:00
|
|
|
Log::error('Error getting METAR: '.$e->getMessage(), $e->getTrace());
|
2018-04-03 11:44:31 +08:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return $raw_metar;
|
|
|
|
}
|
2018-04-03 11:35:25 +08:00
|
|
|
}
|