Context::fromAmazonRequest()   B
last analyzed

Complexity

Conditions 10
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10

Importance

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

How to fix   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\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