From 6824dd8cbc1ff5c3a4072be4a0536b6d003bf1d4 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Wed, 4 Nov 2020 18:25:16 +0000 Subject: Inital Commit --- src/App.php | 58 ++++++++++++++++++++++++++++++++++++++ src/Flight/AviationStackClient.php | 58 ++++++++++++++++++++++++++++++++++++++ src/Flight/Helper.php | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 src/App.php create mode 100644 src/Flight/AviationStackClient.php create mode 100644 src/Flight/Helper.php (limited to 'src') diff --git a/src/App.php b/src/App.php new file mode 100644 index 0000000..bb92b87 --- /dev/null +++ b/src/App.php @@ -0,0 +1,58 @@ +config) { + $config = include __DIR__ . '/../config.php'; + $this->config = $config; + } + + if (!$value) { + return $config; + } + + if (!array_key_exists($value, $config)) { + throw new Exception('Failed loading config value `' . $value . '`'); + } + + return $config[$value]; + } + + public function initCommands() + { + $api_key = $this->getConfig('api_key'); + + $this->addCommand( + 'flight:check {flight}', + function (InputInterface $input, OutputInterface $output) use ($api_key) { + $flight_data = (new Helper($api_key))->getFlightByIata($input->getArgument('flight')); + $output->writeLn($flight_data); + return Command::SUCCESS; + } + ); + + $this->addCommand( + 'flight {flight}', + function (InputInterface $input, OutputInterface $output) use ($api_key) { + $flight_data = (new Helper($api_key))->getFlightByIata($input->getArgument('flight')); + $output->writeLn($flight_data); + return Command::SUCCESS; + } + ); + } +} 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 @@ +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 @@ +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; + } +} -- cgit v1.2.3