-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
49 lines (36 loc) · 1.5 KB
/
index.php
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
<?php
require_once 'vendor/autoload.php';
use Nomisoft\Alexa\Response\AlexaResponse;
use CABDesigns\AlexaNextBus\TransportAPI;
use CABDesigns\AlexaNextBus\BusList;
use CABDesigns\AlexaNextBus\BusListMapper;
use CABDesigns\AlexaNextBus\BusListSorter;
use CABDesigns\AlexaNextBus\AlexaResponseMapper;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
$dotenv->required('ATCO_CODES')->notEmpty();
$dotenv->required('SKILL_ID')->notEmpty();
$dotenv->required('TRANSPORT_API_APP_KEY')->notEmpty();
$dotenv->required('TRANSPORT_API_APP_ID')->notEmpty();
$transportApi = new TransportAPI(new Client(), $_ENV['TRANSPORT_API_APP_ID'], $_ENV['TRANSPORT_API_APP_KEY']);
$atcoCodes = json_decode($_ENV['ATCO_CODES']);
$promises = $transportApi->fetchNextBuses($atcoCodes);
Promise\all($promises)->then(function (array $httpResponses) {
$busList = new BusList();
$busListMapper = new BusListMapper();
// Map our API responses to a list of buses
foreach ($httpResponses as $httpResponse) {
$busListMapper->map($httpResponse, $busList);
}
// Sort the buses
$busList = (new BusListSorter())->sort($busList);
// Limit the buses
$busList = new BusList(array_slice($busList->getArrayCopy(), 0, 3));
// Generate an alexa response from the list of buses
$alexaResponseMapper = new AlexaResponseMapper();
$alexaResponse = new AlexaResponse();
$alexaResponseMapper->map($busList, $alexaResponse);
$alexaResponse->render();
})->wait();