Add Contract interface for airport lookup functionality (#365)
* Add Contract interface for airport lookup functionality * style ci fixes
This commit is contained in:
28
app/Services/AirportLookup/VaCentralLookup.php
Normal file
28
app/Services/AirportLookup/VaCentralLookup.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\AirportLookup;
|
||||
|
||||
use App\Contracts\AirportLookup;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use VaCentral\Airport;
|
||||
use VaCentral\HttpException;
|
||||
|
||||
class VaCentralLookup extends AirportLookup
|
||||
{
|
||||
/**
|
||||
* Lookup the information for an airport
|
||||
*
|
||||
* @param string $icao
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAirport($icao)
|
||||
{
|
||||
try {
|
||||
return Airport::get($icao);
|
||||
} catch (HttpException $e) {
|
||||
Log::error($e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,20 +2,27 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Contracts\AirportLookup as AirportLookupProvider;
|
||||
use App\Contracts\Metar as MetarProvider;
|
||||
use App\Contracts\Service;
|
||||
use App\Support\Metar;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use VaCentral\Airport;
|
||||
|
||||
/**
|
||||
* Class AnalyticsService
|
||||
*/
|
||||
class AirportService extends Service
|
||||
{
|
||||
private $lookupProvider;
|
||||
private $metarProvider;
|
||||
|
||||
public function __construct(
|
||||
AirportLookupProvider $lookupProvider,
|
||||
MetarProvider $metarProvider
|
||||
|
||||
) {
|
||||
$this->lookupProvider = $lookupProvider;
|
||||
$this->metarProvider = $metarProvider;
|
||||
}
|
||||
|
||||
@@ -38,4 +45,35 @@ class AirportService extends Service
|
||||
return new Metar($raw_metar);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user