forked from kevin-mitchell/alexa-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlexaApplication.php
87 lines (63 loc) · 1.88 KB
/
AlexaApplication.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php namespace Develpr\AlexaApp;
use Laravel\Lumen\Application;
class AlexaApplication extends Application
{
private $intentRoutes = [];
public function intent($uri, $intent, $action)
{
$this->intentRoutes[] = $uri;
$this->addRoute('INTENT', '*' . $intent, $action);
return $this;
}
public function launch($uri, $action)
{
$this->intentRoutes[] = $uri;
$this->addRoute('INTENT', '**' . 'LAUNCH_REQUEST', $action);
}
public function sessionEnded($uri, $action)
{
$this->intentRoutes[] = $uri;
$this->addRoute('INTENT', '**' . 'SESSION_ENDED_REQUEST', $action);
}
protected function getMethod()
{
$method = parent::getMethod();
if($this->isRouteWithIntent())
{
$method = "INTENT";
}
return $method;
}
public function getPathInfo()
{
$pathInfo = parent::getPathInfo();
if($this->isRouteWithIntent())
{
$intentRelated = $this->getIntentFromRequest();
$pathInfo = '/' . $intentRelated;
}
return $pathInfo;
}
/**
* Has a route been registered to this path with an Intent?
*
* @return bool
*/
private function isRouteWithIntent()
{
return ( parent::getMethod() == "POST" && in_array(parent::getPathInfo(), $this->intentRoutes) );
}
private function getIntentFromRequest()
{
$request = $this->make('request');
$data = json_decode($request->getContent(), true);
switch(array_get($data, 'request.type')){
case 'SessionEndedRequest':
return '**' . "SESSION_ENDED_REQUEST";
case 'LaunchRequest':
return '**' . "LAUNCH_REQUEST";
case 'IntentRequest':
return '*' . ltrim(array_get($data, 'request.intent.name'));
}
}
}