Passed
Pull Request — master (#22)
by
unknown
03:50
created

ResponseHelper::addResponseSsml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace MaxBeckers\AmazonAlexa\Helper;
4
5
use MaxBeckers\AmazonAlexa\Response\Card;
6
use MaxBeckers\AmazonAlexa\Response\Directives\Directive;
7
use MaxBeckers\AmazonAlexa\Response\OutputSpeech;
8
use MaxBeckers\AmazonAlexa\Response\Reprompt;
9
use MaxBeckers\AmazonAlexa\Response\Response;
10
11
/**
12
 * This helper class can create simple responses for the most needed intents.
13
 *
14
 * @author Maximilian Beckers <[email protected]>
15
 */
16
class ResponseHelper
17
{
18
    /**
19
     * @var Response
20
     */
21
    public $response;
22
23
    /**
24
     * ResponseHelper constructor creates a new response object.
25
     */
26
    public function __construct()
27
    {
28
        $this->response = new Response();
29
    }
30
31
    /**
32
     * Add a plaintext respond to response.
33
     *
34
     * @param string $text
35
     * @param bool   $endSession
36
     *
37
     * @return Response
38
     */
39
    public function respond(string $text, $endSession = false): Response
40
    {
41
        $this->addResponseText($text, $endSession);
42
43
        return $this->response;
44
    }
45
46
    /**
47
     * Add a plaintext respond to response.
48
     *
49
     * @param string $text
50
     * @param bool   $endSession
51
     *
52
     * @return ResponseHelper
53
     */
54
    public function addResponseText(string $text, $endSession = false): ResponseHelper
55
    {
56
        $outputSpeech = OutputSpeech::createByText($text);
57
58
        $this->response->response->outputSpeech     = $outputSpeech;
59
        $this->response->response->shouldEndSession = $endSession;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Add a ssml respond to response.
66
     *
67
     * @param string $ssml
68
     * @param bool   $endSession
69
     *
70
     * @return Response
71
     */
72
    public function respondSsml(string $ssml, $endSession = false): Response
73
    {
74
        $this->addResponseSsml($ssml, $endSession);
75
76
        return $this->response;
77
    }
78
79
    /**
80
     * Add a ssml respond to response.
81
     *
82
     * @param string $ssml
83
     * @param bool   $endSession
84
     *
85
     * @return ResponseHelper
86
     */
87
    public function addResponseSsml(string $ssml, $endSession = false): ResponseHelper
88
    {
89
        $outputSpeech = OutputSpeech::createBySsml($ssml);
90
91
        $this->response->response->outputSpeech     = $outputSpeech;
92
        $this->response->response->shouldEndSession = $endSession;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Add a plaintext reprompt to response.
99
     *
100
     * @param string $text
101
     *
102
     * @return Response
103
     */
104
    public function reprompt(string $text)
105
    {
106
        $this->addRepromptText($text);
107
108
        return $this->response;
109
    }
110
111
    /**
112
     * Add a plaintext reprompt to response.
113
     *
114
     * @param string $text
115
     *
116
     * @return ResponseHelper
117
     */
118
    public function addRepromptText(string $text): ResponseHelper
119
    {
120
        $outputSpeech = OutputSpeech::createByText($text);
121
        $reprompt     = new Reprompt($outputSpeech);
122
123
        $this->response->response->reprompt = $reprompt;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Add a ssml reprompt to response.
130
     *
131
     * @param string $ssml
132
     *
133
     * @return Response
134
     */
135
    public function repromptSsml(string $ssml)
136
    {
137
        $this->addRepromptSsml($ssml);
138
139
        return $this->response;
140
    }
141
142
    /**
143
     * Add a ssml reprompt to response.
144
     *
145
     * @param string $ssml
146
     *
147
     * @return ResponseHelper
148
     */
149
    public function addRepromptSsml(string $ssml): ResponseHelper
150
    {
151
        $outputSpeech = OutputSpeech::createBySsml($ssml);
152
        $reprompt     = new Reprompt($outputSpeech);
153
154
        $this->response->response->reprompt = $reprompt;
155
156
        return $this;
157
    }
158
159
    /**
160
     * Add a card to response.
161
     *
162
     * @param Card $card
163
     *
164
     * @return Response
165
     */
166
    public function card(Card $card)
167
    {
168
        $this->response->response->card = $card;
169
170
        return $this->response;
171
    }
172
173
    /**
174
     * Add a card to response.
175
     *
176
     * @param Card $card
177
     *
178
     * @return ResponseHelper
179
     */
180
    public function addCard(Card $card): ResponseHelper
181
    {
182
        $this->response->response->card = $card;
183
184
        return $this;
185
    }
186
187
    /**
188
     * Add a directive to response.
189
     *
190
     * @param Directive $directive
191
     *
192
     * @return Response
193
     */
194
    public function directive(Directive $directive)
195
    {
196
        $this->response->response->addDirective($directive);
197
198
        return $this->response;
199
    }
200
201
    /**
202
     * Add a directive to response.
203
     *
204
     * @param Directive $directive
205
     *
206
     * @return ResponseHelper
207
     */
208
    public function addDirective(Directive $directive): ResponseHelper
209
    {
210
        $this->response->response->addDirective($directive);
211
212
        return $this;
213
    }
214
215
    /**
216
     * Add a new attribute to response session attributes.
217
     *
218
     * @param string $key
219
     * @param string $value
220
     * @return ResponseHelper
221
     */
222
    public function addSessionAttribute(string $key, string $value): ResponseHelper
223
    {
224
        $this->response->sessionAttributes[$key] = $value;
225
226
        return $this;
227
    }
228
229
    /**
230
     * Reset the response in ResponseHelper.
231
     */
232
    public function resetResponse()
233
    {
234
        $this->response = new Response();
235
    }
236
237
    /**
238
     * Get current response of response helper.
239
     *
240
     * @return Response
241
     */
242
    public function getResponse(): Response
243
    {
244
        return $this->response;
245
    }
246
}
247