Slot::jsonSerialize()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 8
nop 0
crap 5
1
<?php
2
3
namespace MaxBeckers\AmazonAlexa\Intent;
4
5
/**
6
 * @author Maximilian Beckers <[email protected]>
7
 */
8
class Slot implements \JsonSerializable
9
{
10
    /**
11
     * @var string
12
     */
13
    public $name;
14
15
    /**
16
     * @var string|null
17
     */
18
    public $value;
19
20
    /**
21
     * @var string|null
22
     */
23
    public $confirmationStatus;
24
25
    /**
26
     * @var Resolution[]
27
     */
28
    public $resolutions = [];
29
30
    /**
31
     * @param array $amazonRequest
32
     *
33
     * @return Slot
34
     */
35 15
    public static function fromAmazonRequest(string $name, array $amazonRequest): self
36
    {
37 15
        $slot = new self();
38
39 15
        $slot->name               = $name;
40 15
        $slot->value              = isset($amazonRequest['value']) ? $amazonRequest['value'] : null;
41 15
        $slot->confirmationStatus = isset($amazonRequest['confirmationStatus']) ? $amazonRequest['confirmationStatus'] : null;
42
43 15
        if (isset($amazonRequest['resolutions']['resolutionsPerAuthority'])) {
44 9
            foreach ($amazonRequest['resolutions']['resolutionsPerAuthority'] as $resolution) {
45 9
                $slot->resolutions[] = Resolution::fromAmazonRequest($resolution);
46
            }
47
        }
48
49 15
        return $slot;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 4
    public function jsonSerialize()
56
    {
57 4
        $data         = [];
58 4
        $data['name'] = $this->name;
59 4
        if (null !== $this->value) {
60 3
            $data['value'] = $this->value;
61
        }
62 4
        if (null !== $this->confirmationStatus) {
63 2
            $data['confirmationStatus'] = $this->confirmationStatus;
64
        }
65 4
        if (!empty($this->resolutions)) {
66 3
            $data['resolutions']['resolutionsPerAuthority'] = [];
67 3
            foreach ($this->resolutions as $resolution) {
68 3
                $data['resolutions']['resolutionsPerAuthority'][] = $resolution->jsonSerialize();
69
            }
70
        }
71
72 4
        return $data;
73
    }
74
75
    /**
76
     * @return IntentValue|null
77
     */
78 1
    public function getFirstResolutionIntentValue()
79
    {
80 1
        if (isset($this->resolutions[0])) {
81 1
            $resolution = $this->resolutions[0];
82 1
            if (isset($resolution->values[0])) {
83 1
                return $resolution->values[0];
84
            }
85
        }
86
87 1
        return null;
88
    }
89
}
90