finish acceptance tests and refactor IotaCodesClient to service container
This commit is contained in:
@@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user