ResponseHelper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 27
dl 0
loc 107
ccs 36
cts 36
cp 1
rs 10
c 4
b 1
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addSessionAttribute() 0 3 1
A directive() 0 5 1
A getResponse() 0 3 1
A reprompt() 0 8 1
A resetResponse() 0 4 1
A __construct() 0 6 3
A repromptSsml() 0 8 1
A respondSsml() 0 8 1
A respond() 0 8 1
A card() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Helper;
6
7
use MaxBeckers\AmazonAlexa\Response\Card;
8
use MaxBeckers\AmazonAlexa\Response\Directives\Directive;
9
use MaxBeckers\AmazonAlexa\Response\OutputSpeech;
10
use MaxBeckers\AmazonAlexa\Response\Reprompt;
11
use MaxBeckers\AmazonAlexa\Response\Response;
12
use MaxBeckers\AmazonAlexa\Response\ResponseBody;
13
14
/**
15
 * This helper class can create simple responses for the most needed intents.
16
 */
17
class ResponseHelper
18
{
19
    /**
20
     * @param Response|null $response The response object
21
     * @param ResponseBody|null $responseBody The response body object
22
     */
23 20
    public function __construct(
24
        public ?Response $response = null,
25
        public ?ResponseBody $responseBody = null,
26
    ) {
27 20
        if ($this->response === null || $this->responseBody === null) {
28 20
            $this->resetResponse();
29
        }
30
    }
31
32
    /**
33
     * Add a plaintext respond to response.
34
     */
35 2
    public function respond(string $text, bool $endSession = false): ?Response
36
    {
37 2
        $outputSpeech = OutputSpeech::createByText($text);
38
39 2
        $this->responseBody->outputSpeech = $outputSpeech;
40 2
        $this->responseBody->shouldEndSession = $endSession;
41
42 2
        return $this->response;
43
    }
44
45
    /**
46
     * Add a ssml respond to response.
47
     */
48 2
    public function respondSsml(string $ssml, bool $endSession = false): ?Response
49
    {
50 2
        $outputSpeech = OutputSpeech::createBySsml($ssml);
51
52 2
        $this->responseBody->outputSpeech = $outputSpeech;
53 2
        $this->responseBody->shouldEndSession = $endSession;
54
55 2
        return $this->response;
56
    }
57
58
    /**
59
     * Add a plaintext reprompt to response.
60
     */
61 1
    public function reprompt(string $text): ?Response
62
    {
63 1
        $outputSpeech = OutputSpeech::createByText($text);
64 1
        $reprompt = new Reprompt($outputSpeech);
65
66 1
        $this->responseBody->reprompt = $reprompt;
67
68 1
        return $this->response;
69
    }
70
71
    /**
72
     * Add a ssml reprompt to response.
73
     */
74 1
    public function repromptSsml(string $ssml): ?Response
75
    {
76 1
        $outputSpeech = OutputSpeech::createBySsml($ssml);
77 1
        $reprompt = new Reprompt($outputSpeech);
78
79 1
        $this->responseBody->reprompt = $reprompt;
80
81 1
        return $this->response;
82
    }
83
84
    /**
85
     * Add a card to response.
86
     */
87 1
    public function card(Card $card): ?Response
88
    {
89 1
        $this->responseBody->card = $card;
90
91 1
        return $this->response;
92
    }
93
94
    /**
95
     * Add a directive to response.
96
     */
97 2
    public function directive(Directive $directive): ?Response
98
    {
99 2
        $this->responseBody->addDirective($directive);
0 ignored issues
show
Bug introduced by
The method addDirective() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        $this->responseBody->/** @scrutinizer ignore-call */ 
100
                             addDirective($directive);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
101 2
        return $this->response;
102
    }
103
104
    /**
105
     * Add a new attribute to response session attributes.
106
     */
107 1
    public function addSessionAttribute(string $key, string $value): void
108
    {
109 1
        $this->response->sessionAttributes[$key] = $value;
110
    }
111
112
    /**
113
     * Reset the response in ResponseHelper.
114
     */
115 20
    public function resetResponse(): void
116
    {
117 20
        $this->responseBody = new ResponseBody();
118 20
        $this->response = new Response([], '1.0', $this->responseBody);
119
    }
120
121 1
    public function getResponse(): Response
122
    {
123 1
        return $this->response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response could return the type null which is incompatible with the type-hinted return MaxBeckers\AmazonAlexa\Response\Response. Consider adding an additional type-check to rule them out.
Loading history...
124
    }
125
}
126