summaryrefslogtreecommitdiff
path: root/src/Flight/Helper.php
blob: 91a2cab0d7f9c63140423f04779640ba9b8f2396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;
    }
}