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

@@ -0,0 +1,41 @@
<?php
namespace App\Services;
use App\Contracts\Metar as MetarProvider;
use App\Contracts\Service;
use App\Support\Metar;
/**
* Class AnalyticsService
*/
class AirportService extends Service
{
private $metarProvider;
public function __construct(
MetarProvider $metarProvider
) {
$this->metarProvider = $metarProvider;
}
/**
* Return the METAR for a given airport
*
* @param $icao
*
* @return Metar|null
*/
public function getMetar($icao): Metar
{
$metar = null;
$wind = null;
$raw_metar = $this->metarProvider->get_metar($icao);
if ($raw_metar && $raw_metar !== '') {
return new Metar($raw_metar);
}
return null;
}
}

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;
}
}
}