Add Contract interface for airport lookup functionality (#365)

* Add Contract interface for airport lookup functionality

* style ci fixes
This commit is contained in:
Nabeel S
2019-08-22 14:32:49 -04:00
committed by GitHub
parent 94ba5d8680
commit 6018a6dcaa
9 changed files with 138 additions and 37 deletions

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

View File

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