Completed
Pull Request — master (#38)
by
unknown
03:25
created

Button::toArray()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 16
nop 0
dl 0
loc 25
ccs 13
cts 13
cp 1
crap 5
rs 9.5222
c 1
b 0
f 0
1
<?php
2
namespace Maknz\Slack\BlockElement;
3
4
use InvalidArgumentException;
5
use Maknz\Slack\BlockElement\Text;
6
7
class Button extends Confirmable
8
{
9
    const STYLE_DEFAULT = 'default';
10
    const STYLE_PRIMARY = 'primary';
11
    const STYLE_DANGER = 'danger';
12
13
    /**
14
     * Block type.
15
     *
16
     * @var string
17
     */
18
    protected $type = 'button';
19
20
    /**
21
     * Button text.
22
     *
23
     * @var Maknz\Slack\BlockElement\Text
0 ignored issues
show
Bug introduced by
The type Maknz\Slack\BlockElement...Slack\BlockElement\Text was not found. Did you mean Maknz\Slack\BlockElement\Text? If so, make sure to prefix the type with \.
Loading history...
24
     */
25
    protected $text;
26
27
    /**
28
     * Button action.
29
     *
30
     * @var string
31
     */
32
    protected $action_id;
33
34
    /**
35
     * Button URL.
36
     *
37
     * @var string
38
     */
39
    protected $url;
40
41
    /**
42
     * Button value.
43
     *
44
     * @var string
45
     */
46
    protected $value;
47
48
    /**
49
     * Button style.
50
     *
51
     * @var string
52
     */
53
    protected $style;
54
55
56
    /**
57
     * Internal attribute to property map.
58
     *
59
     * @var array
60
     */
61
    protected static $availableAttributes = [
62
        'text'      => 'text',
63
        'action_id' => 'action_id',
64
        'url'       => 'url',
65
        'value'     => 'value',
66
        'style'     => 'style',
67
        'confirm'   => 'confirm',
68
    ];
69
70
    /**
71
     * Get the button text.
72
     *
73
     * @return Maknz\Slack\BlockElement\Text
74
     */
75 6
    public function getText()
76
    {
77 6
        return $this->text;
78
    }
79
80
    /**
81
     * Set the button text.
82
     *
83
     * @param mixed $text
84
     *
85
     * @return Button
86
     *
87
     * @throws \InvalidArgumentException
88
     */
89 6
    public function setText($text)
90
    {
91 6
        $this->text = Text::create($text, Text::TYPE_PLAIN);
0 ignored issues
show
Documentation Bug introduced by
It seems like Maknz\Slack\BlockElement...ement\Text::TYPE_PLAIN) of type Maknz\Slack\BlockElement\Text is incompatible with the declared type Maknz\Slack\BlockElement...Slack\BlockElement\Text of property $text.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
92
93 6
        return $this;
94
    }
95
96
    /**
97
     * Get the button action.
98
     *
99
     * @return string
100
     */
101 4
    public function getActionId()
102
    {
103 4
        return $this->action_id;
104
    }
105
106
    /**
107
     * Set the button action.
108
     *
109
     * @param string $actionId
110
     *
111
     * @return Button
112
     */
113 5
    public function setActionId($actionId)
114
    {
115 5
        $this->action_id = $actionId;
116
117 5
        return $this;
118
    }
119
120
    /**
121
     * Get the button URL.
122
     *
123
     * @return string
124
     */
125 4
    public function getUrl()
126
    {
127 4
        return $this->url;
128
    }
129
130
    /**
131
     * Set the button URL.
132
     *
133
     * @param string $url
134
     *
135
     * @return Button
136
     */
137 1
    public function setUrl($url)
138
    {
139 1
        $this->url = $url;
140
141 1
        return $this;
142
    }
143
144
    /**
145
     * Get the button value.
146
     *
147
     * @return string
148
     */
149 5
    public function getValue()
150
    {
151 5
        return $this->value;
152
    }
153
154
    /**
155
     * Set the button value.
156
     *
157
     * @param string $value
158
     *
159
     * @return Button
160
     */
161 3
    public function setValue($value)
162
    {
163 3
        $this->value = $value;
164
165 3
        return $this;
166
    }
167
168
    /**
169
     * Get the button style.
170
     *
171
     * @return string
172
     */
173 4
    public function getStyle()
174
    {
175 4
        return $this->style;
176
    }
177
178
    /**
179
     * Set the button style.
180
     *
181
     * @param string $style
182
     *
183
     * @return Button
184
     */
185 2
    public function setStyle($style)
186
    {
187 2
        $this->style = $style;
188
189 2
        return $this;
190
    }
191
192
    /**
193
     * Convert the block to its array representation.
194
     *
195
     * @return array
196
     */
197 4
    public function toArray()
198
    {
199
        $data = [
200 4
            'type'      => $this->getType(),
201 4
            'text'      => $this->getText()->toArray(),
202 4
            'action_id' => $this->getActionId(),
203
        ];
204
205 4
        if ($this->getUrl()) {
206 1
            $data['url'] = $this->getUrl();
207
        }
208
209 4
        if ($this->getValue()) {
210 2
            $data['value'] = $this->getValue();
211
        }
212
213 4
        if ($this->getStyle()) {
214 2
            $data['style'] = $this->getStyle();
215
        }
216
217 4
        if ($this->getConfirm()) {
218 1
            $data['confirm'] = $this->getConfirm()->toArray();
219
        }
220
221 4
        return $data;
222
    }
223
}
224