Context   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 48
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B fromAmazonRequest() 0 19 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Request;
6
7
class Context
8
{
9
    /**
10
     * @param System|null $system System context
11
     * @param AudioPlayer|null $audioPlayer Audio player context
12
     * @param Geolocation|null $geolocation Geolocation context
13
     * @param Advertising|null $advertising Advertising context
14
     * @param Viewport|null $viewport Single viewport context
15
     * @param Viewport[]|null $viewports Array of viewport contexts
16
     * @param AlexaPresentationAPL|null $apl APL presentation context
17
     * @param array|null $extensions Extensions context
18
     */
19 45
    public function __construct(
20
        public ?System $system = null,
21
        public ?AudioPlayer $audioPlayer = null,
22
        public ?Geolocation $geolocation = null,
23
        public ?Advertising $advertising = null,
24
        public ?Viewport $viewport = null,
25
        public ?array $viewports = null,
26
        public ?AlexaPresentationAPL $apl = null,
27
        public ?array $extensions = null,
28
    ) {
29 45
    }
30
31
    /**
32
     * @param array $amazonRequest
33
     *
34
     * @return Context
35
     */
36 37
    public static function fromAmazonRequest(array $amazonRequest): self
37
    {
38 37
        $viewports = null;
39 37
        if (isset($amazonRequest['Viewports']) && is_array($amazonRequest['Viewports'])) {
40 1
            $viewports = [];
41 1
            foreach ($amazonRequest['Viewports'] as $viewportData) {
42 1
                $viewports[] = Viewport::fromAmazonRequest($viewportData);
43
            }
44
        }
45
46 37
        return new self(
47 37
            system: isset($amazonRequest['System']) ? System::fromAmazonRequest($amazonRequest['System']) : null,
48 37
            audioPlayer: isset($amazonRequest['AudioPlayer']) ? AudioPlayer::fromAmazonRequest($amazonRequest['AudioPlayer']) : null,
49 37
            geolocation: isset($amazonRequest['Geolocation']) ? Geolocation::fromAmazonRequest($amazonRequest['Geolocation']) : null,
50 37
            advertising: isset($amazonRequest['Advertising']) ? Advertising::fromAmazonRequest($amazonRequest['Advertising']) : null,
51 37
            viewport: isset($amazonRequest['Viewport']) ? Viewport::fromAmazonRequest($amazonRequest['Viewport']) : null,
52 37
            viewports: $viewports,
53 37
            apl: isset($amazonRequest['Alexa.Presentation.APL']) ? AlexaPresentationAPL::fromAmazonRequest($amazonRequest['Alexa.Presentation.APL']) : null,
54 37
            extensions: $amazonRequest['Extensions'] ?? null,
55 37
        );
56
    }
57
}
58