Passed
Push — master ( 8fc162...54fc89 )
by Maximilian
47s
created

Card::createAskForPermissionsConsent()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
namespace MaxBeckers\AmazonAlexa\Response;
4
5
use MaxBeckers\AmazonAlexa\Exception\InvalidCardPermissionsException;
6
7
/**
8
 * @author Maximilian Beckers <[email protected]>
9
 */
10
class Card implements \JsonSerializable
11
{
12
    const TYPE_SIMPLE                      = 'Simple';
13
    const TYPE_STANDARD                    = 'Standard';
14
    const TYPE_LINK_ACCOUNT                = 'LinkAccount';
15
    const TYPE_ASK_FOR_PERMISSIONS_CONSENT = 'AskForPermissionsConsent';
16
17
    const PERMISSION_FULL_ADDRESS                   = 'read::alexa:device:all:address';
18
    const PERMISSION_COUNTRY_REGION_AND_POSTAL_CODE = 'read::alexa:device:all:address:country_and_postal_code';
19
    const PERMISSIONS                               = [
20
        self::PERMISSION_FULL_ADDRESS,
21
        self::PERMISSION_COUNTRY_REGION_AND_POSTAL_CODE,
22
    ];
23
24
    /**
25
     * @var string
26
     */
27
    public $type;
28
29
    /**
30
     * @var string|null
31
     */
32
    public $title;
33
34
    /**
35
     * @var string|null
36
     */
37
    public $content;
38
39
    /**
40
     * @var string|null
41
     */
42
    public $text;
43
44
    /**
45
     * @var CardImage|null
46
     */
47
    public $image;
48
49
    /**
50
     * @var array
51
     */
52
    public $permissions = [];
53
54
    /**
55
     * @param string $type
56
     */
57
    public function __construct(string $type = self::TYPE_STANDARD)
58
    {
59
        $this->type = $type;
60
    }
61
62
    /**
63
     * @param string $title
64
     * @param string $content
65
     *
66
     * @return Card
67
     */
68
    public static function createSimple(string $title, string $content): self
69
    {
70
        $card = new self(self::TYPE_SIMPLE);
71
72
        $card->title   = $title;
73
        $card->content = $content;
74
75
        return $card;
76
    }
77
78
    /**
79
     * @param string         $title
80
     * @param string         $text
81
     * @param CardImage|null $cardImage
82
     *
83
     * @return Card
84
     */
85
    public static function createStandard(string $title, string $text, CardImage $cardImage = null): self
86
    {
87
        $card = new self();
88
89
        $card->title = $title;
90
        $card->text  = $text;
91
        $card->image = $cardImage;
92
93
        return $card;
94
    }
95
96
    /**
97
     * @return Card
98
     */
99
    public static function createLinkAccount(): self
100
    {
101
        return new self(self::TYPE_LINK_ACCOUNT);
102
    }
103
104
    /**
105
     * @param array $permissions
106
     *
107
     * @throws InvalidCardPermissionsException
108
     *
109
     * @return Card
110
     */
111
    public static function createAskForPermissionsConsent(array $permissions): self
112
    {
113
        if (empty($permissions) || !empty(array_diff($permissions, self::PERMISSIONS))) {
114
            throw new InvalidCardPermissionsException();
115
        }
116
117
        $card = new self(self::TYPE_ASK_FOR_PERMISSIONS_CONSENT);
118
119
        $card->permissions = $permissions;
120
121
        return $card;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function jsonSerialize()
128
    {
129
        $data = new \ArrayObject();
130
131
        if (null !== $this->type) {
132
            $data['type'] = $this->type;
133
        }
134
        if (null !== $this->title) {
135
            $data['title'] = $this->title;
136
        }
137
        if (null !== $this->content) {
138
            $data['content'] = $this->content;
139
        }
140
        if (null !== $this->text) {
141
            $data['text'] = $this->text;
142
        }
143
        if (null !== $this->image) {
144
            $data['image'] = $this->image;
145
        }
146
        if (!empty($this->permissions)) {
147
            $data['permissions'] = $this->permissions;
148
        }
149
150
        return $data;
151
    }
152
}
153