Add Contract interface for airport lookup functionality (#365)
* Add Contract interface for airport lookup functionality * style ci fixes
This commit is contained in:
27
app/Contracts/AirportLookup.php
Normal file
27
app/Contracts/AirportLookup.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Contracts;
|
||||
|
||||
use App\Models\Airport;
|
||||
|
||||
abstract class AirportLookup
|
||||
{
|
||||
/**
|
||||
* Lookup the information for an airport. Needs to return an instance of the
|
||||
* Airport model, or an array with the properties listed in the Airport model.
|
||||
*
|
||||
* The in-use implementation can be changed in the config/phpvms.php file, so
|
||||
* different services can be used, in-case vaCentral one isn't working or there
|
||||
* is a better one available. Don't handle any caching in this layer, that happens
|
||||
* at the service layer
|
||||
*
|
||||
* Return null if there's an error or nothing was found
|
||||
*
|
||||
* @example App\Services\AirportLookup\VaCentralLookup
|
||||
*
|
||||
* @param string $icao
|
||||
*
|
||||
* @return Airport|null
|
||||
*/
|
||||
abstract public function getAirport($icao);
|
||||
}
|
||||
@@ -5,10 +5,8 @@ namespace App\Http\Controllers\Api;
|
||||
use App\Contracts\Controller;
|
||||
use App\Http\Resources\Airport as AirportResource;
|
||||
use App\Repositories\AirportRepository;
|
||||
use App\Services\AirportService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Log;
|
||||
use VaCentral\Airport as AirportLookup;
|
||||
|
||||
/**
|
||||
* Class AirportController
|
||||
@@ -16,16 +14,20 @@ use VaCentral\Airport as AirportLookup;
|
||||
class AirportController extends Controller
|
||||
{
|
||||
private $airportRepo;
|
||||
private $airportSvc;
|
||||
|
||||
/**
|
||||
* AirportController constructor.
|
||||
*
|
||||
* @param AirportRepository $airportRepo
|
||||
* @param AirportService $airportSvc
|
||||
*/
|
||||
public function __construct(
|
||||
AirportRepository $airportRepo
|
||||
AirportRepository $airportRepo,
|
||||
AirportService $airportSvc
|
||||
) {
|
||||
$this->airportRepo = $airportRepo;
|
||||
$this->airportSvc = $airportSvc;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,19 +90,7 @@ class AirportController extends Controller
|
||||
*/
|
||||
public function lookup($id)
|
||||
{
|
||||
$airport = Cache::remember(
|
||||
config('cache.keys.AIRPORT_VACENTRAL_LOOKUP.key').$id,
|
||||
config('cache.keys.AIRPORT_VACENTRAL_LOOKUP.time'),
|
||||
function () use ($id) {
|
||||
try {
|
||||
return AirportLookup::get($id);
|
||||
} catch (\VaCentral\HttpException $e) {
|
||||
Log::error($e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
$airport = $this->airportSvc->lookupAirport($id);
|
||||
return new AirportResource(collect($airport));
|
||||
}
|
||||
}
|
||||
|
||||
29
app/Providers/BindServiceProviders.php
Executable file
29
app/Providers/BindServiceProviders.php
Executable file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Contracts\AirportLookup;
|
||||
use App\Contracts\Metar;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class BindServiceProviders extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
/*
|
||||
* Bind the class used to fullfill the Metar class contract
|
||||
*/
|
||||
$this->app->bind(
|
||||
Metar::class,
|
||||
config('phpvms.metar_lookup')
|
||||
);
|
||||
|
||||
/*
|
||||
* Bind the class used to fullfill the AirportLookup class contract
|
||||
*/
|
||||
$this->app->bind(
|
||||
AirportLookup::class,
|
||||
config('phpvms.airport_lookup')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Contracts\Metar;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class WeatherServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
$this->app->bind(
|
||||
Metar::class,
|
||||
config('phpvms.metar')
|
||||
);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ return [
|
||||
App\Providers\vaCentralServiceProvider::class,
|
||||
App\Providers\ExtendedTimezonelistProvider::class,
|
||||
App\Providers\MeasurementsProvider::class,
|
||||
App\Providers\WeatherServiceProvider::class,
|
||||
App\Providers\BindServiceProviders::class,
|
||||
],
|
||||
|
||||
'aliases' => [
|
||||
|
||||
@@ -47,7 +47,13 @@ return [
|
||||
* Point to the class to use to retrieve the METAR string. If this
|
||||
* goes inactive at some date, it can be replaced
|
||||
*/
|
||||
'metar' => App\Services\Metar\AviationWeather::class,
|
||||
'metar_lookup' => App\Services\Metar\AviationWeather::class,
|
||||
|
||||
/*
|
||||
* Point to the class used to retrieve the airport information.
|
||||
* If this goes inactive at some date, it can be replaced
|
||||
*/
|
||||
'airport_lookup' => App\Services\AirportLookup\VaCentralLookup::class,
|
||||
|
||||
/*
|
||||
* Your vaCentral API key
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
<env name="APP_ENV" value="unittest"/>
|
||||
<env name="APP_KEY" value="base64:ve66Z5Kt/zTN3p++0zOPu854PHfZkwJE5VuoFAlzHtI="/>
|
||||
<env name="APP_DEBUG" value="true"/>
|
||||
<env name="APP_LOG_LEVEL" value="info"/>
|
||||
<env name="APP_LOG_LEVEL" value="error"/>
|
||||
<env name="DB_CONNECTION" value="memory"/>
|
||||
<env name="CACHE_DRIVER" value="array"/>
|
||||
<env name="SESSION_DRIVER" value="array"/>
|
||||
|
||||
Reference in New Issue
Block a user