TickHandler::jsonSerialize()   A
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 10
nc 16
nop 0
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
8
9
class TickHandler implements \JsonSerializable
10
{
11
    public const DEFAULT_MINIMUM_DELAY = 1000;
12
13
    /**
14
     * @param AbstractStandardCommand[] $commands Commands to run during tick events
15
     * @param string|null $when APL boolean expression controlling whether the handler is active. Null means always active.
16
     * @param string $description Optional description of this handler
17
     * @param int $minimumDelay Minimum delay between tick events in milliseconds
18
     */
19 17
    public function __construct(
20
        public array $commands = [],
21
        public ?string $when = null,
22
        public string $description = '',
23
        public int $minimumDelay = self::DEFAULT_MINIMUM_DELAY,
24
    ) {
25
        // Filter and ensure valid commands
26 17
        $this->commands = array_values(array_filter(
27 17
            $this->commands,
28 17
            static fn ($c) => $c instanceof AbstractStandardCommand
29 17
        ));
30
    }
31
32 11
    public function jsonSerialize(): array
33
    {
34 11
        $data = [];
35
36 11
        if ($this->commands !== []) {
37 2
            $data['commands'] = $this->commands;
38
        }
39 11
        if ($this->description !== '') {
40 6
            $data['description'] = $this->description;
41
        }
42 11
        if ($this->minimumDelay !== self::DEFAULT_MINIMUM_DELAY) {
43 6
            $data['minimumDelay'] = $this->minimumDelay;
44
        }
45 11
        if ($this->when !== null && $this->when !== '') {
46 4
            $data['when'] = $this->when;
47
        }
48
49 11
        return $data;
50
    }
51
}
52