MediaTag::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 10
dl 0
loc 13
ccs 2
cts 2
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Request;
6
7
class MediaTag
8
{
9
    /**
10
     * @param bool|null $allowAdjustSeekPositionForward Whether seeking forward is allowed
11
     * @param bool|null $allowAdjustSeekPositionBackwards Whether seeking backwards is allowed
12
     * @param bool|null $allowNext Whether next track is allowed
13
     * @param bool|null $allowPrevious Whether previous track is allowed
14
     * @param AudioTrack|null $audioTrack Audio track type
15
     * @param Entity[]|null $entities Array of entities
16
     * @param bool|null $muted Whether audio is muted
17
     * @param int|null $positionInMilliseconds Current position in milliseconds
18
     * @param MediaState|null $state Current media state
19
     * @param string|null $url Media URL
20
     */
21 4
    public function __construct(
22
        public ?bool $allowAdjustSeekPositionForward = null,
23
        public ?bool $allowAdjustSeekPositionBackwards = null,
24
        public ?bool $allowNext = null,
25
        public ?bool $allowPrevious = null,
26
        public ?AudioTrack $audioTrack = null,
27
        public ?array $entities = null,
28
        public ?bool $muted = null,
29
        public ?int $positionInMilliseconds = null,
30
        public ?MediaState $state = null,
31
        public ?string $url = null,
32
    ) {
33 4
        $this->entities = $this->entities ?? [];
34
    }
35
36 4
    public static function fromAmazonRequest(array $amazonRequest): self
37
    {
38 4
        $entities = [];
39 4
        if (isset($amazonRequest['entities']) && is_array($amazonRequest['entities'])) {
40 2
            foreach ($amazonRequest['entities'] as $entityData) {
41 1
                $entities[] = Entity::fromAmazonRequest($entityData);
42
            }
43
        }
44
45 4
        return new self(
46 4
            allowAdjustSeekPositionForward: $amazonRequest['allowAdjustSeekPositionForward'] ?? null,
47 4
            allowAdjustSeekPositionBackwards: $amazonRequest['allowAdjustSeekPositionBackwards'] ?? null,
48 4
            allowNext: $amazonRequest['allowNext'] ?? null,
49 4
            allowPrevious: $amazonRequest['allowPrevious'] ?? null,
50 4
            audioTrack: isset($amazonRequest['audioTrack']) ? AudioTrack::tryFrom($amazonRequest['audioTrack']) : null,
51 4
            entities: $entities,
52 4
            muted: $amazonRequest['muted'] ?? null,
53 4
            positionInMilliseconds: $amazonRequest['positionInMilliseconds'] ?? null,
54 4
            state: isset($amazonRequest['state']) ? MediaState::tryFrom($amazonRequest['state']) : null,
55 4
            url: $amazonRequest['url'] ?? null,
56 4
        );
57
    }
58
}
59