Fix/metar reading (#353)

* Fix reading of the METAR information for AviationWeather. Fix the DI

* StyleCI fixes
This commit is contained in:
Nabeel S
2019-08-08 14:52:34 -04:00
committed by GitHub
parent becf6c95f0
commit 5cafebe4d6
11 changed files with 121 additions and 100 deletions

View File

@@ -3,8 +3,7 @@
namespace App\Services\Metar;
use App\Contracts\Metar;
use App\Support\Http;
use Illuminate\Support\Facades\Cache;
use App\Support\HttpClient;
use Illuminate\Support\Facades\Log;
/**
@@ -17,35 +16,39 @@ class AviationWeather extends Metar
.'dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3'
.'&mostRecent=true&fields=raw_text&stationString=';
private $httpClient;
public function __construct(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}
/**
* Implement the METAR - Return the string
*
* @param $icao
*
* @throws \Exception
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @return string
*/
protected function metar($icao): string
{
$metar = Cache::remember(
config('cache.keys.WEATHER_LOOKUP.key').$icao,
config('cache.keys.WEATHER_LOOKUP.time'),
function () use ($icao) {
$url = static::METAR_URL.$icao;
$url = static::METAR_URL.$icao;
try {
$res = Http::get($url, []);
$xml = simplexml_load_string($res);
if (\count($xml->data->METAR->raw_text) === 0) {
return '';
}
return $xml->data->METAR->raw_text->__toString();
} catch (\Exception $e) {
Log::error('Error reading METAR: '.$e->getMessage());
return '';
}
try {
$res = $this->httpClient->get($url, []);
$xml = simplexml_load_string($res);
if (\count($xml->data->METAR->raw_text) === 0) {
return '';
}
);
return $metar;
return $xml->data->METAR->raw_text->__toString();
} catch (\Exception $e) {
Log::error('Error reading METAR: '.$e->getMessage());
throw $e;
}
}
}