ComponentVisibleOnScreen::fromAmazonRequest()   B
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8.1624

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 21
nc 4
nop 1
dl 0
loc 29
ccs 19
cts 22
cp 0.8636
crap 8.1624
rs 8.4444
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Request;
6
7
class ComponentVisibleOnScreen
8
{
9
    /**
10
     * @param ComponentVisibleOnScreen[]|null $children Array of child components
11
     * @param Entity[]|null $entities Array of entity data copied from the component
12
     * @param string|null $id Component identifier
13
     * @param string|null $position Position as a string
14
     * @param string|null $role Component role
15
     * @param Tags|null $tags Element tags providing additional information
16
     * @param array|null $transform Array of transformations
17
     * @param string|null $type Component type
18
     * @param string|null $uid Unique identifier
19
     * @param float|null $visibility Visibility value
20
     */
21 2
    public function __construct(
22
        public ?array $children = null,
23
        public ?array $entities = null,
24
        public ?string $id = null,
25
        public ?string $position = null,
26
        public ?string $role = null,
27
        public ?Tags $tags = null,
28
        public ?array $transform = null,
29
        public ?string $type = null,
30
        public ?string $uid = null,
31
        public ?float $visibility = null,
32
    ) {
33 2
    }
34
35 2
    public static function fromAmazonRequest(array $amazonRequest): self
36
    {
37 2
        $children = null;
38 2
        if (isset($amazonRequest['children']) && is_array($amazonRequest['children'])) {
39
            $children = [];
40
            foreach ($amazonRequest['children'] as $childData) {
41
                $children[] = self::fromAmazonRequest($childData);
42
            }
43
        }
44
45 2
        $entities = null;
46 2
        if (isset($amazonRequest['entities']) && is_array($amazonRequest['entities'])) {
47 2
            $entities = [];
48 2
            foreach ($amazonRequest['entities'] as $entityData) {
49 2
                $entities[] = Entity::fromAmazonRequest($entityData);
50
            }
51
        }
52
53 2
        return new self(
54 2
            children: $children,
55 2
            entities: $entities,
56 2
            id: $amazonRequest['id'] ?? null,
57 2
            position: $amazonRequest['position'] ?? null,
58 2
            role: $amazonRequest['role'] ?? null,
59 2
            tags: isset($amazonRequest['tags']) ? Tags::fromAmazonRequest($amazonRequest['tags']) : null,
60 2
            transform: $amazonRequest['transform'] ?? null,
61 2
            type: $amazonRequest['type'] ?? null,
62 2
            uid: $amazonRequest['uid'] ?? null,
63 2
            visibility: $amazonRequest['visibility'] ?? null,
64 2
        );
65
    }
66
}
67