finish acceptance tests and refactor IotaCodesClient to service container
This commit is contained in:
@@ -38,15 +38,18 @@ class AirportController extends Controller
|
||||
*/
|
||||
public function resourceList(Request $request)
|
||||
{
|
||||
$client = IotaClient::create();
|
||||
$collection = $client->listAirports($request->input('autocomplete'));
|
||||
$iotaCodesClient = app('IotaCodesClient');
|
||||
$airportsCollection = $iotaCodesClient->listAirports($request->input('autocomplete'));
|
||||
if (! $airportsCollection) {
|
||||
return $this->returnErrorMessage('failed to fetch airport list from IOTA api', 400);
|
||||
}
|
||||
|
||||
//add pagination
|
||||
$per_page = $request->input('per_page', 10);
|
||||
$page = $request->input('page', 1);
|
||||
$paginator = new LengthAwarePaginator(
|
||||
$collection->forPage($page, $per_page),
|
||||
$collection->count(),
|
||||
$airportsCollection->forPage($page, $per_page),
|
||||
$airportsCollection->count(),
|
||||
$per_page,
|
||||
$page
|
||||
);
|
||||
@@ -74,9 +77,9 @@ class AirportController extends Controller
|
||||
*/
|
||||
public function getAirport(Request $request, $code)
|
||||
{
|
||||
$client = IotaClient::create();
|
||||
$iotaCodesClient = app('IotaCodesClient');
|
||||
|
||||
$airport = $client->getAirport($code);
|
||||
$airport = $iotaCodesClient->getAirport($code);
|
||||
if (is_null($airport)) {
|
||||
return $this->returnErrorMessage('not a valid airport code', 400);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class Controller extends BaseController
|
||||
*/
|
||||
protected function JsonApiResponse(ResourceInterface $resource, $statusCode, $includes = '')
|
||||
{
|
||||
$manager = new Manager();
|
||||
$manager = app('FractalManager');
|
||||
$manager->setSerializer(new JsonApiSerializer('http://docker.dev:8080'));
|
||||
$manager->parseIncludes($includes);
|
||||
|
||||
|
||||
@@ -46,11 +46,9 @@ class FlightController extends Controller
|
||||
$page
|
||||
);
|
||||
|
||||
$result = (new Collection($paginator, new TripTransformer(), 'trips'))
|
||||
$result = (new Collection($paginator, new FlightTransform(), 'trips'))
|
||||
->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||
|
||||
$result = new Collection($flightCollection, new FlightTransform(), 'flights');
|
||||
|
||||
return $this->JsonApiResponse($result, 200);
|
||||
}
|
||||
|
||||
@@ -98,10 +96,10 @@ class FlightController extends Controller
|
||||
{
|
||||
/** @var \App\Libraries\Trips\Models\Flights $flight */
|
||||
$flight = Flights::find($id);
|
||||
$deleted = $flight->delete();
|
||||
if (! is_null($flight)) {
|
||||
$deleted = $flight->delete();
|
||||
}
|
||||
|
||||
$result = new Item($flight, new FlightTransform(), 'flights');
|
||||
|
||||
return $this->JsonApiResponse($result, 200);
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ class TripController extends Controller
|
||||
return $this->returnErrorMessage('destination not set', 400);
|
||||
}
|
||||
|
||||
$client = IotaClient::create();
|
||||
$client = app('IotaCodesClient');
|
||||
$airport = $client->getAirport($destination);
|
||||
if (is_null($airport)) {
|
||||
return $this->returnErrorMessage('destination not a valid airport code', 400);
|
||||
|
||||
@@ -3,17 +3,33 @@ namespace App\Libraries\IotaCodes;
|
||||
|
||||
class Airport
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $code;
|
||||
|
||||
/**
|
||||
* Airport constructor.
|
||||
* @param $name
|
||||
* @param $code
|
||||
*/
|
||||
protected function __construct($name, $code)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->code = $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Airport factory method
|
||||
* @param $name
|
||||
* @param $code
|
||||
* @return \App\Libraries\IotaCodes\Airport
|
||||
*/
|
||||
public static function create($name, $code)
|
||||
{
|
||||
return new self($name, $code);
|
||||
|
||||
@@ -5,6 +5,12 @@ use League\Fractal\TransformerAbstract;
|
||||
|
||||
class AirportTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* Fractal Transformer for Airport Resource
|
||||
*
|
||||
* @param \App\Libraries\IotaCodes\Airport $airport
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Airport $airport)
|
||||
{
|
||||
return [
|
||||
|
||||
@@ -22,6 +22,11 @@ class Client
|
||||
$this->guzzle = $guzzle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory Method
|
||||
*
|
||||
* @return \App\Libraries\IotaCodes\Client
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
return new self(new Guzzle([
|
||||
@@ -31,6 +36,12 @@ class Client
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of airports
|
||||
*
|
||||
* @param string $autoComplete
|
||||
* @return \Illuminate\Support\Collection|false
|
||||
*/
|
||||
public function listAirports($autoComplete = '')
|
||||
{
|
||||
$uri = '/api/v6/airports';
|
||||
@@ -48,14 +59,20 @@ class Client
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
//todo handle error
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $response->getBody()->getContents();
|
||||
if (empty($result)) {
|
||||
return false;
|
||||
}
|
||||
app('cache')->put($key, $result, $minutes);
|
||||
return $result;
|
||||
});
|
||||
|
||||
if(! $response) {
|
||||
return false;
|
||||
}
|
||||
$result = $this->toAirportCollection($response);
|
||||
if ($autoComplete != '') {
|
||||
$result = $this->autoCompleteMap($result, $autoComplete);
|
||||
@@ -71,7 +88,7 @@ class Client
|
||||
{
|
||||
$uri = '/api/v6/airports';
|
||||
$cacheKey = 'airport.'.$code;
|
||||
$cacheMinutes = 1;
|
||||
$cacheMinutes = 60;
|
||||
|
||||
$response = app('cache')->get($cacheKey, function () use ($cacheKey, $cacheMinutes, $uri, $code) {
|
||||
try {
|
||||
@@ -103,6 +120,9 @@ class Client
|
||||
private function toAirportCollection($json)
|
||||
{
|
||||
$data = json_decode($json);
|
||||
if (! $data) {
|
||||
return new Collection();
|
||||
}
|
||||
$result = (new Collection($data->response))
|
||||
->sortBy('name')
|
||||
->map(function ($item, $key) {
|
||||
@@ -112,6 +132,11 @@ class Client
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Support\Collection $collection
|
||||
* @param $autoComplete
|
||||
* @return Collection
|
||||
*/
|
||||
private function autoCompleteMap(Collection $collection, $autoComplete)
|
||||
{
|
||||
return $collection->filter(function ($value, $key) use ($autoComplete) {
|
||||
|
||||
20
app/Libraries/IotaCodes/IotaCodesServiceProvider.php
Normal file
20
app/Libraries/IotaCodes/IotaCodesServiceProvider.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries\IotaCodes;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class IotaCodesServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton('IotaCodesClient', function ($app) {
|
||||
return Client::create();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use League\Fractal\Manager;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -13,6 +14,8 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
$this->app->singleton('FractalManager', function ($app) {
|
||||
return new Manager();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user