2019-08-09 02:52:34 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-08-23 02:32:49 +08:00
|
|
|
use App\Contracts\AirportLookup as AirportLookupProvider;
|
2019-08-09 02:52:34 +08:00
|
|
|
use App\Contracts\Metar as MetarProvider;
|
|
|
|
use App\Contracts\Service;
|
|
|
|
use App\Support\Metar;
|
2019-08-23 02:32:49 +08:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use VaCentral\Airport;
|
2019-08-09 02:52:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AnalyticsService
|
|
|
|
*/
|
|
|
|
class AirportService extends Service
|
|
|
|
{
|
2019-08-23 02:32:49 +08:00
|
|
|
private $lookupProvider;
|
2019-08-09 02:52:34 +08:00
|
|
|
private $metarProvider;
|
|
|
|
|
|
|
|
public function __construct(
|
2019-08-23 02:32:49 +08:00
|
|
|
AirportLookupProvider $lookupProvider,
|
2019-08-09 02:52:34 +08:00
|
|
|
MetarProvider $metarProvider
|
2019-08-23 02:32:49 +08:00
|
|
|
|
2019-08-09 02:52:34 +08:00
|
|
|
) {
|
2019-08-23 02:32:49 +08:00
|
|
|
$this->lookupProvider = $lookupProvider;
|
2019-08-09 02:52:34 +08:00
|
|
|
$this->metarProvider = $metarProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the METAR for a given airport
|
|
|
|
*
|
|
|
|
* @param $icao
|
|
|
|
*
|
|
|
|
* @return Metar|null
|
|
|
|
*/
|
2019-08-09 05:41:53 +08:00
|
|
|
public function getMetar($icao)
|
2019-08-09 02:52:34 +08:00
|
|
|
{
|
2019-08-09 05:41:53 +08:00
|
|
|
$icao = trim($icao);
|
|
|
|
if ($icao === '') {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-09 02:52:34 +08:00
|
|
|
|
2019-08-09 05:41:53 +08:00
|
|
|
$raw_metar = $this->metarProvider->get_metar($icao);
|
2019-08-09 02:52:34 +08:00
|
|
|
if ($raw_metar && $raw_metar !== '') {
|
|
|
|
return new Metar($raw_metar);
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 02:32:49 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lookup an airport's information from a remote provider. This handles caching
|
|
|
|
* the data internally
|
|
|
|
*
|
|
|
|
* @param string $icao ICAO
|
|
|
|
*
|
|
|
|
* @return Airport|array
|
|
|
|
*/
|
|
|
|
public function lookupAirport($icao)
|
|
|
|
{
|
|
|
|
$key = config('cache.keys.AIRPORT_VACENTRAL_LOOKUP.key').$icao;
|
|
|
|
|
|
|
|
$airport = Cache::get($key);
|
|
|
|
if ($airport) {
|
|
|
|
return $airport;
|
|
|
|
}
|
|
|
|
|
|
|
|
$airport = $this->lookupProvider->getAirport($icao);
|
|
|
|
if ($airport === null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache::add(
|
|
|
|
$key,
|
|
|
|
$airport,
|
|
|
|
config('cache.keys.AIRPORT_VACENTRAL_LOOKUP.time')
|
|
|
|
);
|
|
|
|
|
|
|
|
return $airport;
|
|
|
|
}
|
2019-08-09 02:52:34 +08:00
|
|
|
}
|