1
|
|
|
<?php namespace nyx\notify\transports\slack\message\attachment; |
2
|
|
|
|
3
|
|
|
// External dependencies |
4
|
|
|
use nyx\core; |
5
|
|
|
use nyx\diagnostics; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Slack Action |
9
|
|
|
* |
10
|
|
|
* See {@see https://api.slack.com/docs/message-buttons} for Slack's documentation on using Messages with Actions. |
11
|
|
|
* |
12
|
|
|
* @package Nyx\Notify |
13
|
|
|
* @version 0.1.0 |
14
|
|
|
* @author Michal Chojnacki <[email protected]> |
15
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
16
|
|
|
* @link https://github.com/unyx/nyx |
17
|
|
|
* @todo PHP7.1 pass for nullable return types (missing ?string on most methods). |
18
|
|
|
*/ |
19
|
|
|
class Action implements core\interfaces\Serializable |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The traits of an Action. |
23
|
|
|
*/ |
24
|
|
|
use core\traits\Serializable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The types an Action can be of. Note: Currently Slack only provides the 'button' action. |
28
|
|
|
*/ |
29
|
|
|
const TYPE_BUTTON = 'button'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The styles a button can be of. |
33
|
|
|
*/ |
34
|
|
|
const STYLE_DEFAULT = 'default'; |
35
|
|
|
const STYLE_PRIMARY = 'primary'; |
36
|
|
|
const STYLE_DANGER = 'danger'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string Required. The name of the Action. This name will be returned to your Action URL along with the message's |
40
|
|
|
* callback_id when this action is invoked. |
41
|
|
|
*/ |
42
|
|
|
protected $name; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string Required. The user-facing label for the message button representing this action. Cannot contain |
46
|
|
|
* any markup. |
47
|
|
|
*/ |
48
|
|
|
protected $text; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string Required. The type of the Action. One of the TYPE_* class constants. |
52
|
|
|
*/ |
53
|
|
|
protected $type = self::TYPE_BUTTON; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string Optional. The style of the Action. One of the STYLE_* class constants. |
57
|
|
|
*/ |
58
|
|
|
protected $style; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string Optional. The value of the Action - a string identifying this specific action. It will be sent |
62
|
|
|
* to your Action URL along with the name and attachment's callback_id. If providing multiple |
63
|
|
|
* actions with the same name, the $value can be strategically used to differentiate intent. |
64
|
|
|
*/ |
65
|
|
|
protected $value; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var actions\Confirmation Optional. The Confirmation field for this Action. |
69
|
|
|
*/ |
70
|
|
|
protected $confirm; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates a new Action instance. |
74
|
|
|
* |
75
|
|
|
* @param array $attributes |
76
|
|
|
*/ |
77
|
|
|
public function __construct(array $attributes) |
78
|
|
|
{ |
79
|
|
|
if (!empty($attributes)) { |
80
|
|
|
$this->setAttributes($attributes); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the attributes of this Action. |
86
|
|
|
* |
87
|
|
|
* @param array $attributes |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setAttributes(array $attributes) : Action |
91
|
|
|
{ |
92
|
|
|
if (isset($attributes['name'])) { |
93
|
|
|
$this->setName($attributes['name']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (isset($attributes['text'])) { |
97
|
|
|
$this->setText($attributes['text']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (isset($attributes['type'])) { |
101
|
|
|
$this->setType($attributes['type']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($attributes['style'])) { |
105
|
|
|
$this->setStyle($attributes['style']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (isset($attributes['value'])) { |
109
|
|
|
$this->setValue($attributes['value']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (isset($attributes['confirm'])) { |
113
|
|
|
$this->setConfirm($attributes['confirm']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the name of the Action. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getName() |
125
|
|
|
{ |
126
|
|
|
return $this->name; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets the name of the Action. |
131
|
|
|
* |
132
|
|
|
* @param string $name |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function setName(string $name) : Action |
136
|
|
|
{ |
137
|
|
|
$this->name = $name; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the user-facing label for the message button representing this action. |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getText() |
148
|
|
|
{ |
149
|
|
|
return $this->text; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Sets the user-facing label for the message button representing this action. |
154
|
|
|
* |
155
|
|
|
* @param string $text |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function setText(string $text) : Action |
159
|
|
|
{ |
160
|
|
|
$this->text = $text; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns the style of the Action. One of the STYLE_* class constants. |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function getStyle() |
171
|
|
|
{ |
172
|
|
|
return $this->style; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Sets the style of the Action. One of the STYLE_* class constants. |
177
|
|
|
* |
178
|
|
|
* @param string $style |
179
|
|
|
* @return $this |
180
|
|
|
*/ |
181
|
|
|
public function setStyle(string $style) : Action |
182
|
|
|
{ |
183
|
|
|
$this->style = $style; |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns the type of the Action. One of the TYPE_* class constants. |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function getType() |
194
|
|
|
{ |
195
|
|
|
return $this->type; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Sets the type of the Action. One of the TYPE_* class constants. |
200
|
|
|
* |
201
|
|
|
* @param string $type |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
|
|
public function setType(string $type) : Action |
205
|
|
|
{ |
206
|
|
|
$this->type = $type; |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns the value of the Action. |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
public function getValue() |
217
|
|
|
{ |
218
|
|
|
return $this->value; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Sets the value of the Action. |
223
|
|
|
* |
224
|
|
|
* @param string $value |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
|
|
public function setValue(string $value) : Action |
228
|
|
|
{ |
229
|
|
|
$this->value = $value; |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns the Confirmation field for this Action. |
236
|
|
|
* |
237
|
|
|
* @return actions\Confirmation |
238
|
|
|
*/ |
239
|
|
|
public function getConfirm() |
240
|
|
|
{ |
241
|
|
|
return $this->confirm; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Sets the Confirmation field for this Action. |
246
|
|
|
* |
247
|
|
|
* @param array|actions\Confirmation $confirm |
248
|
|
|
* @return $this |
249
|
|
|
* @throws \InvalidArgumentException |
250
|
|
|
*/ |
251
|
|
View Code Duplication |
public function setConfirm($confirm) : Action |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
if (is_array($confirm)) { |
254
|
|
|
$this->confirm = new actions\Confirmation($confirm); |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if ($confirm instanceof actions\Confirmation) { |
260
|
|
|
$this->confirm = $confirm; |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
throw new \InvalidArgumentException("Expected an array or an instance of ".actions\Confirmation::class.", got [".diagnostics\Debug::getTypeName($confirm)."] instead."); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* {@inheritDoc} |
270
|
|
|
*/ |
271
|
|
|
public function unserialize($data) |
272
|
|
|
{ |
273
|
|
|
$this->setAttributes(unserialize($data)); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* {@inheritDoc} |
278
|
|
|
*/ |
279
|
|
|
public function toArray() : array |
280
|
|
|
{ |
281
|
|
|
$data = [ |
282
|
|
|
'name' => $this->getName(), |
283
|
|
|
'text' => $this->getText(), |
284
|
|
|
'style' => $this->getStyle(), |
285
|
|
|
'type' => $this->getType(), |
286
|
|
|
'value' => $this->getValue() |
287
|
|
|
]; |
288
|
|
|
|
289
|
|
|
if (null !== $confirm = $this->getConfirm()) { |
290
|
|
|
$data['confirm'] = $confirm->toArray(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $data; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.