Intent::fromAmazonRequest()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

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