Passed
Pull Request — master (#97)
by Maximilian
03:49
created

SendTokenListDataDirective   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
dl 0
loc 46
ccs 0
cts 18
cp 0
rs 10
c 1
b 0
f 1
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addItem() 0 3 1
A jsonSerialize() 0 21 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\Directive;
8
9
class SendTokenListDataDirective extends Directive implements \JsonSerializable
10
{
11
    public const TYPE = 'Alexa.Presentation.APL.SendTokenListData';
12
13
    /**
14
     * @param string $listId The identifier of the list whose items are contained in this response
15
     * @param string $pageToken Opaque token for the array of items which are contained in this response
16
     * @param string|null $correlationToken The correlation token supplied in the LoadTokenListData request
17
     * @param string|null $nextPageToken Opaque token to retrieve the next page of list items data
18
     * @param array $items Array of objects to be added to the list
19
     */
20
    public function __construct(
21
        public string $listId,
22
        public string $pageToken,
23
        public ?string $correlationToken = null,
24
        public ?string $nextPageToken = null,
25
        public array $items = [],
26
    ) {
27
    }
28
29
    public function addItem(array $item): void
30
    {
31
        $this->items[] = $item;
32
    }
33
34
    public function jsonSerialize(): array
35
    {
36
        $data = [
37
            'type' => self::TYPE,
38
            'listId' => $this->listId,
39
            'pageToken' => $this->pageToken,
40
        ];
41
42
        if ($this->correlationToken !== null) {
43
            $data['correlationToken'] = $this->correlationToken;
44
        }
45
46
        if ($this->nextPageToken !== null) {
47
            $data['nextPageToken'] = $this->nextPageToken;
48
        }
49
50
        if (!empty($this->items)) {
51
            $data['items'] = $this->items;
52
        }
53
54
        return $data;
55
    }
56
}
57