AttachmentAction   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 53
c 2
b 0
f 0
dl 0
loc 226
rs 10
wmc 22

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getValue() 0 3 1
B __construct() 0 24 7
A getConfirm() 0 3 1
A setName() 0 5 1
A setValue() 0 5 1
A getStyle() 0 3 1
A setConfirm() 0 13 3
A setStyle() 0 5 1
A setType() 0 5 1
A getType() 0 3 1
A getText() 0 3 1
A setText() 0 5 1
A toArray() 0 9 1
1
<?php
2
3
namespace Eloquentcoder\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
     * Confirmation field.
52
     *
53
     * @var ActionConfirmation
54
     */
55
    protected $confirm;
56
57
    /**
58
     * Instantiate a new AttachmentAction.
59
     *
60
     * @param array $attributes
61
     *
62
     * @return void
63
     */
64
    public function __construct(array $attributes)
65
    {
66
        if (isset($attributes['name'])) {
67
            $this->setName($attributes['name']);
68
        }
69
70
        if (isset($attributes['text'])) {
71
            $this->setText($attributes['text']);
72
        }
73
74
        if (isset($attributes['style'])) {
75
            $this->setStyle($attributes['style']);
76
        }
77
78
        if (isset($attributes['type'])) {
79
            $this->setType($attributes['type']);
80
        }
81
82
        if (isset($attributes['value'])) {
83
            $this->setValue($attributes['value']);
84
        }
85
86
        if (isset($attributes['confirm'])) {
87
            $this->setConfirm($attributes['confirm']);
88
        }
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getName()
95
    {
96
        return $this->name;
97
    }
98
99
    /**
100
     * @param string $name
101
     *
102
     * @return AttachmentAction
103
     */
104
    public function setName($name)
105
    {
106
        $this->name = $name;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getText()
115
    {
116
        return $this->text;
117
    }
118
119
    /**
120
     * @param string $text
121
     *
122
     * @return AttachmentAction
123
     */
124
    public function setText($text)
125
    {
126
        $this->text = $text;
127
128
        return $this;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getStyle()
135
    {
136
        return $this->style;
137
    }
138
139
    /**
140
     * @param string $style
141
     *
142
     * @return AttachmentAction
143
     */
144
    public function setStyle($style)
145
    {
146
        $this->style = $style;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function getType()
155
    {
156
        return $this->type;
157
    }
158
159
    /**
160
     * @param string $type
161
     *
162
     * @return AttachmentAction
163
     */
164
    public function setType($type)
165
    {
166
        $this->type = $type;
167
168
        return $this;
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getValue()
175
    {
176
        return $this->value;
177
    }
178
179
    /**
180
     * @param string $value
181
     *
182
     * @return AttachmentAction
183
     */
184
    public function setValue($value)
185
    {
186
        $this->value = $value;
187
188
        return $this;
189
    }
190
191
    /**
192
     * @return ActionConfirmation
193
     */
194
    public function getConfirm()
195
    {
196
        return $this->confirm;
197
    }
198
199
    /**
200
     * @param ActionConfirmation|array $confirm
201
     *
202
     * @return AttachmentAction
203
     */
204
    public function setConfirm($confirm)
205
    {
206
        if ($confirm instanceof ActionConfirmation) {
207
            $this->confirm = $confirm;
208
209
            return $this;
210
        } elseif (is_array($confirm)) {
0 ignored issues
show
introduced by
The condition is_array($confirm) is always true.
Loading history...
211
            $this->confirm = new ActionConfirmation($confirm);
212
213
            return $this;
214
        }
215
216
        throw new InvalidArgumentException('The action confirmation must be an instance of Maknz\Slack\ActionConfirmation or a keyed array');
217
    }
218
219
    /**
220
     * Get the array representation of this attachment action.
221
     *
222
     * @return array
223
     */
224
    public function toArray()
225
    {
226
        return [
227
            'name'    => $this->getName(),
228
            'text'    => $this->getText(),
229
            'style'   => $this->getStyle(),
230
            'type'    => $this->getType(),
231
            'value'   => $this->getValue(),
232
            'confirm' => $this->getConfirm()->toArray(),
233
        ];
234
    }
235
}
236