Passed
Pull Request — 1.0 (#42)
by Alexander
02:10
created

AttachmentAction   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 4
Bugs 2 Features 1
Metric Value
eloc 62
c 4
b 2
f 1
dl 0
loc 260
ccs 63
cts 66
cp 0.9545
rs 10
wmc 26

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setText() 0 5 1
A getName() 0 3 1
A setStyle() 0 5 1
A setName() 0 5 1
A getUrl() 0 3 1
A setValue() 0 5 1
A setUrl() 0 5 1
A setType() 0 5 1
A setConfirm() 0 13 3
A getText() 0 3 1
A getType() 0 3 1
A getValue() 0 3 1
A getStyle() 0 3 1
A getConfirm() 0 3 1
B __construct() 0 28 8
A toArray() 0 16 2
1
<?php
2
3
namespace Maknz\Slack;
4
5
use InvalidArgumentException;
6
7
class AttachmentAction
8
{
9
    const TYPE_BUTTON = 'button';
10
11
    const STYLE_DEFAULT = 'default';
12
    const STYLE_PRIMARY = 'primary';
13
    const STYLE_DANGER = 'danger';
14
15
    /**
16
     * The required name field of the action. The name will be returned to your Action URL.
17
     *
18
     * @var string
19
     */
20
    protected $name;
21
22
    /**
23
     * The required label for the action.
24
     *
25
     * @var string
26
     */
27
    protected $text;
28
29
    /**
30
     * Button style.
31
     *
32
     * @var string
33
     */
34
    protected $style;
35
36
    /**
37
     * The required type of the action.
38
     *
39
     * @var string
40
     */
41
    protected $type = self::TYPE_BUTTON;
42
43
    /**
44
     * Optional value. It will be sent to your Action URL.
45
     *
46
     * @var string
47
     */
48
    protected $value;
49
50
    /**
51
     * Optional URL.
52
     *
53
     * @var string
54
     */
55
    protected $url;
56
57
    /**
58
     * Confirmation field.
59
     *
60
     * @var ActionConfirmation
61
     */
62
    protected $confirm;
63
64
    /**
65
     * Instantiate a new AttachmentAction.
66
     *
67
     * @param array $attributes
68
     *
69
     * @throws \InvalidArgumentException
70
     */
71 4
    public function __construct(array $attributes)
72
    {
73 4
        if (isset($attributes['name'])) {
74 4
            $this->setName($attributes['name']);
75
        }
76
77 4
        if (isset($attributes['text'])) {
78 4
            $this->setText($attributes['text']);
79
        }
80
81 4
        if (isset($attributes['style'])) {
82 4
            $this->setStyle($attributes['style']);
83
        }
84
85 4
        if (isset($attributes['type'])) {
86 4
            $this->setType($attributes['type']);
87
        }
88
89 4
        if (isset($attributes['url'])) {
90 2
            $this->setUrl($attributes['url']);
91
        }
92
93 4
        if (isset($attributes['value'])) {
94 4
            $this->setValue($attributes['value']);
95
        }
96
97 4
        if (isset($attributes['confirm'])) {
98 4
            $this->setConfirm($attributes['confirm']);
99
        }
100 4
    }
101
102
    /**
103
     * @return string
104
     */
105 2
    public function getName()
106
    {
107 2
        return $this->name;
108
    }
109
110
    /**
111
     * @param string $name
112
     * @return AttachmentAction
113
     */
114 4
    public function setName($name)
115
    {
116 4
        $this->name = $name;
117
118 4
        return $this;
119
    }
120
121
    /**
122
     * @return string
123
     */
124 3
    public function getText()
125
    {
126 3
        return $this->text;
127
    }
128
129
    /**
130
     * @param string $text
131
     * @return AttachmentAction
132
     */
133 4
    public function setText($text)
134
    {
135 4
        $this->text = $text;
136
137 4
        return $this;
138
    }
139
140
    /**
141
     * @return string
142
     */
143 2
    public function getUrl()
144
    {
145 2
        return $this->url;
146
    }
147
148
    /**
149
     * @param string $url
150
     * @return AttachmentAction
151
     */
152 2
    public function setUrl($url)
153
    {
154 2
        $this->url = $url;
155
156 2
        return $this;
157
    }
158
159
    /**
160
     * @return string
161
     */
162 2
    public function getStyle()
163
    {
164 2
        return $this->style;
165
    }
166
167
    /**
168
     * @param string $style
169
     * @return AttachmentAction
170
     */
171 4
    public function setStyle($style)
172
    {
173 4
        $this->style = $style;
174
175 4
        return $this;
176
    }
177
178
    /**
179
     * @return string
180
     */
181 2
    public function getType()
182
    {
183 2
        return $this->type;
184
    }
185
186
    /**
187
     * @param string $type
188
     * @return AttachmentAction
189
     */
190 4
    public function setType($type)
191
    {
192 4
        $this->type = $type;
193
194 4
        return $this;
195
    }
196
197
    /**
198
     * @return string
199
     */
200 2
    public function getValue()
201
    {
202 2
        return $this->value;
203
    }
204
205
    /**
206
     * @param string $value
207
     * @return AttachmentAction
208
     */
209 4
    public function setValue($value)
210
    {
211 4
        $this->value = $value;
212
213 4
        return $this;
214
    }
215
216
    /**
217
     * @return ActionConfirmation
218
     */
219 2
    public function getConfirm()
220
    {
221 2
        return $this->confirm;
222
    }
223
224
    /**
225
     * @param ActionConfirmation|array $confirm
226
     *
227
     * @return AttachmentAction
228
     *
229
     * @throws \InvalidArgumentException
230
     */
231 4
    public function setConfirm($confirm)
232
    {
233 4
        if ($confirm instanceof ActionConfirmation) {
234
            $this->confirm = $confirm;
235
236
            return $this;
237 4
        } elseif (is_array($confirm)) {
0 ignored issues
show
introduced by
The condition is_array($confirm) is always true.
Loading history...
238 4
            $this->confirm = new ActionConfirmation($confirm);
239
240 4
            return $this;
241
        }
242
243
        throw new InvalidArgumentException('The action confirmation must be an instance of Maknz\Slack\ActionConfirmation or a keyed array');
244
    }
245
246
    /**
247
     * Get the array representation of this attachment action.
248
     *
249
     * @return array
250
     */
251 2
    public function toArray()
252
    {
253
        $array = [
254 2
            'name'  => $this->getName(),
255 2
            'text'  => $this->getText(),
256 2
            'style' => $this->getStyle(),
257 2
            'type'  => $this->getType(),
258 2
            'value' => $this->getValue(),
259 2
            'url'   => $this->getUrl(),
260
        ];
261
262 2
        if (($confirm = $this->getConfirm()) !== null) {
263 2
            $array['confirm'] = $confirm->toArray();
264
        }
265
266 2
        return $array;
267
    }
268
}
269