diff options
| author | Fbenas <philbeansburton@gmail.com> | 2020-11-04 18:25:16 +0000 | 
|---|---|---|
| committer | Fbenas <philbeansburton@gmail.com> | 2020-11-04 18:25:16 +0000 | 
| commit | 6824dd8cbc1ff5c3a4072be4a0536b6d003bf1d4 (patch) | |
| tree | 7ac3a83fb0cf1f90f6e0d924da928b41bc524e3c /src/Flight | |
Diffstat (limited to 'src/Flight')
| -rw-r--r-- | src/Flight/AviationStackClient.php | 58 | ||||
| -rw-r--r-- | src/Flight/Helper.php | 52 | 
2 files changed, 110 insertions, 0 deletions
diff --git a/src/Flight/AviationStackClient.php b/src/Flight/AviationStackClient.php new file mode 100644 index 0000000..739fa27 --- /dev/null +++ b/src/Flight/AviationStackClient.php @@ -0,0 +1,58 @@ +<?php + +namespace FBeans\Blaflight\Flight; + +use GuzzleHttp\Client; + +class AviationStackClient +{ +    protected $access_key; + +    public function __construct(array $guzzle_params = []) +    { +        $this->client = new Client($guzzle_params); +    } + +    public function withAccessKey($access_key) +    { +        $this->access_key = $access_key; +        return $this; +    } + +    protected function restCall(string $api_url, string $verb, array $guzzle_params) +    { +        if (!isset($guzzle_params['access_key'])) { +            $guzzle_params['access_key'] = $this->access_key; +        } + +        $guzzle_params['headers'] = [ +            'Accept' => 'application/json' +        ]; + +        $guzzle_params['headers'] = ['timeout' => 60]; + +        try { +            $response = $this->client->request($verb, $api_url, $guzzle_params); +            return $response; +        } catch (\Exception $e) { +            var_dump($e->getMessage()); +            die(); +        } +    } + +    protected function getBaseUrl() +    { +        return 'http://api.aviationstack.com/v1'; +    } + +    protected function buildUrl(string $uri) +    { +        return $this->getBaseUrl() . $uri . '?access_key=' . $this->access_key; +    } + +    public function get(string $uri) +    { +        $response = $this->restCall($this->buildUrl($uri), 'GET', []); +        return json_decode($response->getBody()); +    } +} diff --git a/src/Flight/Helper.php b/src/Flight/Helper.php new file mode 100644 index 0000000..91a2cab --- /dev/null +++ b/src/Flight/Helper.php @@ -0,0 +1,52 @@ +<?php + +namespace FBeans\Blaflight\Flight; + +class Helper +{ +    protected $client; + +    protected $flights; + +    public function __construct(string $access_key) +    { +        $this->client = $this->createClient()->withAccessKey($access_key); +    } + +    protected function createClient() +    { +        return new AviationStackClient(); +    } + +    public function getFlights() +    { +        if (!$this->flights) { +            $this->flights = $this->client->get('/flights'); +        } + +        return $this->flights; +    } + +    public function getFlightByIata(string $iata) +    { +        $flights = $this->getFlights(); + +        foreach ($flights->data as $flight) { +            if ($flight->flight->iata == $iata) { +                return $this->getNiceFlightData($flight); +            } +        } + +        return false; +    } + +    protected function getNiceFlightData($flight) +    { +        return '(' . $flight->flight_status . ')' +            . ' ' . $flight->departure->iata +            . '->' . $flight->arrival->iata +            . '. Dept: ' . $flight->departure->scheduled +            . '. Arr: ' . $flight->arrival->scheduled +            . '. ' . $flight->airline->name; +    } +}  | 
