summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFbenas <philbeansburton@gmail.com>2020-11-04 18:25:16 +0000
committerFbenas <philbeansburton@gmail.com>2020-11-04 18:25:16 +0000
commit6824dd8cbc1ff5c3a4072be4a0536b6d003bf1d4 (patch)
tree7ac3a83fb0cf1f90f6e0d924da928b41bc524e3c /src
Inital CommitHEADmaster
Diffstat (limited to 'src')
-rw-r--r--src/App.php58
-rw-r--r--src/Flight/AviationStackClient.php58
-rw-r--r--src/Flight/Helper.php52
3 files changed, 168 insertions, 0 deletions
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 @@
+<?php
+
+namespace FBeans\Blaflight;
+
+use FBeans\Blaflight\Flight\Helper;
+use FBeans\BlaIRC\Command;
+use FBeans\BlaIRC\Application as BlaApplication;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+// XXX: make a nice config thing
+// XXX: Make nice output sugar
+class App extends BlaApplication
+{
+ protected $config;
+
+ protected function getConfig(string $value = null)
+ {
+ if (!$this->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 @@
+<?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;
+ }
+}