Passed
Push — master ( ba4173...1e3786 )
by Sullivan
01:42
created

AttachmentAction::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Nexylan packages.
7
 *
8
 * (c) Nexylan SAS <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nexy\Slack;
15
16
/**
17
 * @author Sullivan Senechal <[email protected]>
18
 */
19
final class AttachmentAction
20
{
21
    const TYPE_BUTTON = 'button';
22
23
    const STYLE_DEFAULT = 'default';
24
    const STYLE_PRIMARY = 'primary';
25
    const STYLE_DANGER = 'danger';
26
27
    /**
28
     * The required name field of the action. The name will be returned to your Action URL.
29
     *
30
     * @var string
31
     */
32
    private $name;
33
34
    /**
35
     * The required label for the action.
36
     *
37
     * @var string
38
     */
39
    private $text;
40
41
    /**
42
     * Button style.
43
     *
44
     * @var string
45
     */
46
    private $style = self::STYLE_DEFAULT;
47
48
    /**
49
     * The required type of the action.
50
     *
51
     * @var string
52
     */
53
    private $type = self::TYPE_BUTTON;
54
55
    /**
56
     * Optional value. It will be sent to your Action URL.
57
     *
58
     * @var string|null
59
     */
60
    private $value;
61
62
    /**
63
     * Optional value. It can be used to create link buttons.
64
     *
65
     * @var string|null
66
     */
67
    private $url;
68
69
    /**
70
     * Confirmation field.
71
     *
72
     * @var ActionConfirmation|null
73
     */
74
    private $confirm;
75
76
    /**
77
     * @param string $name
78
     * @param string $text
79
     */
80
    public function __construct(string $name, string $text)
81
    {
82
        $this->name = $name;
83
        $this->text = $text;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getName(): string
90
    {
91
        return $this->name;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getText(): string
98
    {
99
        return $this->text;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getStyle(): string
106
    {
107
        return $this->style;
108
    }
109
110
    /**
111
     * @param string $style
112
     *
113
     * @return AttachmentAction
114
     */
115
    public function setStyle(string $style): self
116
    {
117
        $this->style = $style;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getType(): string
126
    {
127
        return $this->type;
128
    }
129
130
    /**
131
     * @param string $type
132
     *
133
     * @return AttachmentAction
134
     */
135
    public function setType(string $type): self
136
    {
137
        $this->type = $type;
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return string|null
144
     */
145
    public function getValue(): ?string
146
    {
147
        return $this->value;
148
    }
149
150
    /**
151
     * @param string|null $value
152
     *
153
     * @return AttachmentAction
154
     */
155
    public function setValue(?string $value): self
156
    {
157
        $this->value = $value;
158
159
        return $this;
160
    }
161
162
    /**
163
     * @return string|null
164
     */
165
    public function getUrl(): ?string
166
    {
167
        return $this->url;
168
    }
169
170
    /**
171
     * @param string|null $url
172
     *
173
     * @return AttachmentAction
174
     */
175
    public function setUrl(?string $url): self
176
    {
177
        $this->url = $url;
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return ActionConfirmation|null
184
     */
185
    public function getConfirm(): ?ActionConfirmation
186
    {
187
        return $this->confirm;
188
    }
189
190
    /**
191
     * @param ActionConfirmation $confirm
192
     *
193
     * @return AttachmentAction
194
     */
195
    public function setConfirm(?ActionConfirmation $confirm): self
196
    {
197
        $this->confirm = $confirm;
198
199
        return $this;
200
    }
201
202
    /**
203
     * Get the array representation of this attachment action.
204
     *
205
     * @return array
206
     */
207
    public function toArray(): array
208
    {
209
        $array = [
210
            'text' => $this->text,
211
            'style' => $this->style,
212
            'type' => $this->type,
213
        ];
214
215
        // Link buttons do not require "name", "value" and "confirm" attributes.
216
        // @see https://api.slack.com/docs/message-attachments#link_buttons
217
        if (null !== $this->url) {
218
            $array['url'] = $this->url;
219
        } else {
220
            $array['name'] = $this->name;
221
            $array['value'] = $this->value;
222
            $array['confirm'] = $this->confirm ? $this->confirm->toArray() : null;
223
        }
224
225
        return $array;
226
    }
227
}
228