Message::setUsername()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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 Message implements MessageInterface
20
{
21
    /**
22
     * Reference to the Slack client responsible for sending
23
     * the message.
24
     *
25
     * @var \Nexy\Slack\ClientInterface
26
     */
27
    private $client;
28
29
    /**
30
     * The text to send with the message.
31
     *
32
     * @var string|null
33
     */
34
    private $text;
35
36
    /**
37
     * The channel the message should be sent to.
38
     *
39
     * @var string|null
40
     */
41
    private $channel;
42
43
    /**
44
     * The username the message should be sent as.
45
     *
46
     * @var string|null
47
     */
48
    private $username;
49
50
    /**
51
     * The URL to the icon to use.
52
     *
53
     * @var string|null
54
     */
55
    private $icon;
56
57
    /**
58
     * The type of icon we are using.
59
     *
60
     * @var string|null
61
     */
62
    private $iconType;
63
64
    /**
65
     * Whether the message text should be interpreted in Slack's
66
     * Markdown-like language.
67
     *
68
     * @var bool
69
     */
70
    private $allowMarkdown = true;
71
72
    /**
73
     * The attachment fields which should be formatted with
74
     * Slack's Markdown-like language.
75
     *
76
     * @var array
77
     */
78
    private $markdownInAttachments = [];
79
80
    /**
81
     * An array of attachments to send.
82
     *
83
     * @var array
84
     */
85
    private $attachments = [];
86
87
    /**
88
     * @var string
89
     */
90
    const ICON_TYPE_URL = 'icon_url';
91
92
    /**
93
     * @var string
94
     */
95
    const ICON_TYPE_EMOJI = 'icon_emoji';
96
97
    /**
98
     * Instantiate a new Message.
99
     *
100
     * @param \Nexy\Slack\ClientInterface $client
101
     */
102
    public function __construct(ClientInterface $client)
103
    {
104
        $this->client = $client;
105
    }
106
107
    public function getText(): ?string
108
    {
109
        return $this->text;
110
    }
111
112
    /**
113
     * Set the message text.
114
     *
115
     * @return $this
116
     */
117
    public function setText(?string $text): MessageInterface
118
    {
119
        $this->text = $text;
120
121
        return $this;
122
    }
123
124
    public function getChannel(): ?string
125
    {
126
        return $this->channel;
127
    }
128
129
    /**
130
     * Set the channel we will post to.
131
     *
132
     * @return $this
133
     */
134
    public function setChannel(?string $channel): MessageInterface
135
    {
136
        $this->channel = $channel;
137
138
        return $this;
139
    }
140
141
    public function getUsername(): ?string
142
    {
143
        return $this->username;
144
    }
145
146
    /**
147
     * Set the username we will post as.
148
     *
149
     * @return $this
150
     */
151
    public function setUsername(?string $username): MessageInterface
152
    {
153
        $this->username = $username;
154
155
        return $this;
156
    }
157
158
    public function getIcon(): ?string
159
    {
160
        return $this->icon;
161
    }
162
163
    /**
164
     * Set the icon (either URL or emoji) we will post as.
165
     *
166
     * @return $this
167
     */
168
    public function setIcon(?string $icon): MessageInterface
169
    {
170
        if (null === $icon) {
171
            $this->icon = $this->iconType = null;
172
173
            return $this;
174
        }
175
176
        if (':' === \mb_substr($icon, 0, 1) && ':' === \mb_substr($icon, \mb_strlen($icon) - 1, 1)) {
177
            $this->iconType = self::ICON_TYPE_EMOJI;
178
        } else {
179
            $this->iconType = self::ICON_TYPE_URL;
180
        }
181
182
        $this->icon = $icon;
183
184
        return $this;
185
    }
186
187
    public function getIconType(): ?string
188
    {
189
        return $this->iconType;
190
    }
191
192
    public function getAllowMarkdown(): bool
193
    {
194
        return $this->allowMarkdown;
195
    }
196
197
    /**
198
     * Set whether message text should be formatted with
199
     * Slack's Markdown-like language.
200
     *
201
     * @return Message
202
     */
203
    public function setAllowMarkdown(bool $value): MessageInterface
204
    {
205
        $this->allowMarkdown = $value;
206
207
        return $this;
208
    }
209
210
    public function enableMarkdown(): MessageInterface
211
    {
212
        return $this->setAllowMarkdown(true);
213
    }
214
215
    public function disableMarkdown(): MessageInterface
216
    {
217
        return $this->setAllowMarkdown(false);
218
    }
219
220
    public function getMarkdownInAttachments(): array
221
    {
222
        return $this->markdownInAttachments;
223
    }
224
225
    /**
226
     * Set the attachment fields which should be formatted
227
     * in Slack's Markdown-like language.
228
     *
229
     * @return Message
230
     */
231
    public function setMarkdownInAttachments(array $fields): MessageInterface
232
    {
233
        $this->markdownInAttachments = $fields;
234
235
        return $this;
236
    }
237
238
    /**
239
     * Change the name of the user the post will be made as.
240
     *
241
     * @return $this
242
     */
243
    public function from(string $username): MessageInterface
244
    {
245
        return $this->setUsername($username);
246
    }
247
248
    /**
249
     * Change the channel the post will be made to.
250
     *
251
     * @return $this
252
     */
253
    public function to(string $channel): MessageInterface
254
    {
255
        return $this->setChannel($channel);
256
    }
257
258
    /**
259
     * Chainable method for setting the icon.
260
     *
261
     * @return $this
262
     */
263
    public function withIcon(string $icon): MessageInterface
264
    {
265
        return $this->setIcon($icon);
266
    }
267
268
    /**
269
     * Add an attachment to the message.
270
     *
271
     * @return $this
272
     */
273
    public function attach(Attachment $attachment): MessageInterface
274
    {
275
        $this->attachments[] = $attachment;
276
277
        return $this;
278
    }
279
280
    /**
281
     * @return Attachment[]
282
     */
283
    public function getAttachments(): array
284
    {
285
        return $this->attachments;
286
    }
287
288
    /**
289
     * Set the attachments for the message.
290
     *
291
     * @return $this
292
     */
293
    public function setAttachments(array $attachments): MessageInterface
294
    {
295
        $this->clearAttachments();
296
297
        foreach ($attachments as $attachment) {
298
            $this->attach($attachment);
299
        }
300
301
        return $this;
302
    }
303
304
    /**
305
     * Remove all attachments for the message.
306
     *
307
     * @return $this
308
     */
309
    public function clearAttachments(): MessageInterface
310
    {
311
        $this->attachments = [];
312
313
        return $this;
314
    }
315
316
    /**
317
     * Send the message.
318
     *
319
     * @param string|null $text The text to send
320
     *
321
     * @return Message
322
     */
323
    public function send(?string $text = null): MessageInterface
324
    {
325
        if ($text) {
326
            $this->setText($text);
327
        }
328
329
        $this->client->sendMessage($this);
330
331
        return $this;
332
    }
333
}
334