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

VideoComponent::jsonSerialize()   F

Complexity

Conditions 27
Paths > 20000

Size

Total Lines 69
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 27

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 27
eloc 34
nc 65536
nop 0
dl 0
loc 69
ccs 35
cts 35
cp 1
crap 27
rs 0
c 1
b 0
f 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Component;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\APLComponentType;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\AudioTrack;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Scale;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
11
12
class VideoComponent extends APLBaseComponent implements \JsonSerializable
13
{
14
    public const TYPE = APLComponentType::VIDEO;
15
16
    /**
17
     * @param AudioTrack|null $audioTrack Audio track to play on
18
     * @param bool $autoplay If true, automatically start playing the video
19
     * @param bool $muted When true, mute the audio for the video
20
     * @param AbstractStandardCommand[]|null $onEnd Commands to run when the last video track is finished playing
21
     * @param AbstractStandardCommand[]|null $onPause Commands to run when the video switches from playing to paused
22
     * @param AbstractStandardCommand[]|null $onPlay Commands to run when the video switches from paused to playing
23
     * @param AbstractStandardCommand[]|null $onTimeUpdate Commands to run when the playback position changes
24
     * @param AbstractStandardCommand[]|null $onTrackUpdate Commands to run when the current video track changes
25
     * @param AbstractStandardCommand[]|null $onTrackReady Commands to run when the current track state changes to ready
26
     * @param AbstractStandardCommand[]|null $onTrackFail Commands to run when an error occurs and video player can't play the media
27
     * @param string[]|null $preserve Properties to save when reinflating the document
28
     * @param Scale|null $scale How the video should scale to fill the space
29
     * @param bool $screenLock When true, extend the document lifecycle when the video is playing
30
     * @param string|array|null $source Single video source URL
31
     * @param array|null $sources Array of video sources
32
     * @param string[]|null $trackChanges Properties to track and report changes in the visual context
33
     */
34 12
    public function __construct(
35
        public ?AudioTrack $audioTrack = null,
36
        public bool $autoplay = false,
37
        public bool $muted = false,
38
        public ?array $onEnd = null,
39
        public ?array $onPause = null,
40
        public ?array $onPlay = null,
41
        public ?array $onTimeUpdate = null,
42
        public ?array $onTrackUpdate = null,
43
        public ?array $onTrackReady = null,
44
        public ?array $onTrackFail = null,
45
        ?array $preserve = null,
46
        public ?Scale $scale = null,
47
        public bool $screenLock = true,
48
        public string|array|null $source = null,
49
        public ?array $sources = null,
50
        ?array $trackChanges = null,
51
    ) {
52 12
        parent::__construct(type: self::TYPE, preserve: $preserve, trackChanges: $trackChanges);
53
    }
54
55 8
    public function jsonSerialize(): array
56
    {
57 8
        $data = parent::jsonSerialize();
58
59 8
        if ($this->audioTrack !== null) {
60 2
            $data['audioTrack'] = $this->audioTrack->value;
61
        }
62
63 8
        if ($this->autoplay) {
64 2
            $data['autoplay'] = $this->autoplay;
65
        }
66
67 8
        if ($this->muted) {
68 2
            $data['muted'] = $this->muted;
69
        }
70
71 8
        if ($this->onEnd !== null && !empty($this->onEnd)) {
72 1
            $data['onEnd'] = $this->onEnd;
73
        }
74
75 8
        if ($this->onPause !== null && !empty($this->onPause)) {
76 1
            $data['onPause'] = $this->onPause;
77
        }
78
79 8
        if ($this->onPlay !== null && !empty($this->onPlay)) {
80 1
            $data['onPlay'] = $this->onPlay;
81
        }
82
83 8
        if ($this->onTimeUpdate !== null && !empty($this->onTimeUpdate)) {
84 1
            $data['onTimeUpdate'] = $this->onTimeUpdate;
85
        }
86
87 8
        if ($this->onTrackUpdate !== null && !empty($this->onTrackUpdate)) {
88 1
            $data['onTrackUpdate'] = $this->onTrackUpdate;
89
        }
90
91 8
        if ($this->onTrackReady !== null && !empty($this->onTrackReady)) {
92 1
            $data['onTrackReady'] = $this->onTrackReady;
93
        }
94
95 8
        if ($this->onTrackFail !== null && !empty($this->onTrackFail)) {
96 1
            $data['onTrackFail'] = $this->onTrackFail;
97
        }
98
99 8
        if ($this->preserve !== null && !empty($this->preserve)) {
100 1
            $data['preserve'] = $this->preserve;
101
        }
102
103 8
        if ($this->scale !== null) {
104 2
            $data['scale'] = $this->scale->value;
105
        }
106
107 8
        if (!$this->screenLock) {
108 2
            $data['screenLock'] = $this->screenLock;
109
        }
110
111 8
        if ($this->source !== null) {
112 3
            $data['source'] = $this->source;
113
        }
114
115 8
        if ($this->sources !== null && !empty($this->sources)) {
116 1
            $data['sources'] = $this->sources;
117
        }
118
119 8
        if ($this->trackChanges !== null && !empty($this->trackChanges)) {
120 1
            $data['trackChanges'] = $this->trackChanges;
121
        }
122
123 8
        return $data;
124
    }
125
}
126