Passed
Pull Request — master (#97)
by Maximilian
04:02
created

ExecuteCommandsDirective::addCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
8
use MaxBeckers\AmazonAlexa\Response\Directives\Directive;
9
10
class ExecuteCommandsDirective extends Directive implements \JsonSerializable
11
{
12
    public const TYPE = 'Alexa.Presentation.APL.ExecuteCommands';
13
14
    /**
15
     * @param AbstractStandardCommand[] $commands Commands to run on the rendered document
16
     * @param string|null $token Token that identifies the RenderDocument command (for standard APL documents)
17
     * @param string|null $presentationUri String that identifies the widget to target with commands
18
     */
19
    public function __construct(
20
        public array $commands,
21
        public ?string $token = null,
22
        public ?string $presentationUri = null,
23
    ) {
24
    }
25
26
    public function addCommand(AbstractStandardCommand $command): void
27
    {
28
        $this->commands[] = $command;
29
    }
30
31
    public function jsonSerialize(): array
32
    {
33
        $data = [
34
            'type' => self::TYPE,
35
            'commands' => $this->commands,
36
        ];
37
38
        if ($this->token !== null) {
39
            $data['token'] = $this->token;
40
        }
41
42
        if ($this->presentationUri !== null) {
43
            $data['presentationUri'] = $this->presentationUri;
44
        }
45
46
        return $data;
47
    }
48
}
49