Passed
Pull Request — master (#53)
by
unknown
01:19
created

Message::addBlock()   A

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
20
{
21
    /**
22
     * Reference to the Slack client responsible for sending
23
     * the message.
24
     *
25
     * @var \Nexy\Slack\Client
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
     * Any messages published via `response_url` will leave the source message untouched. You can however use
74
     * `response_url` to update the source message of an interaction by including an attribute `replace_original` and
75
     * set it to true.
76
     *
77
     * @var bool|null
78
     */
79
    private $replaceOriginal;
80
81
    /**
82
     * The attachment fields which should be formatted with
83
     * Slack's Markdown-like language.
84
     *
85
     * @var array
86
     */
87
    private $markdownInAttachments = [];
88
89
    /**
90
     * An array of attachments to send.
91
     *
92
     * @var array
93
     */
94
    private $attachments = [];
95
96
    /**
97
     * An array of blocks to send.
98
     *
99
     * @var array
100
     */
101
    private $blocks = [];
102
103
    /**
104
     * @var string
105
     */
106
    const ICON_TYPE_URL = 'icon_url';
107
108
    /**
109
     * @var string
110
     */
111
    const ICON_TYPE_EMOJI = 'icon_emoji';
112
113
    /**
114
     * Instantiate a new Message.
115
     *
116
     * @param \Nexy\Slack\Client $client
117
     */
118
    public function __construct(Client $client)
119
    {
120
        $this->client = $client;
121
    }
122
123
    /**
124
     * @return string|null
125
     */
126
    public function getText(): ?string
127
    {
128
        return $this->text;
129
    }
130
131
    /**
132
     * Set the message text.
133
     *
134
     * @param string|null $text
135
     *
136
     * @return $this
137
     */
138
    public function setText(?string $text): self
139
    {
140
        $this->text = $text;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return string|null
147
     */
148
    public function getChannel(): ?string
149
    {
150
        return $this->channel;
151
    }
152
153
    /**
154
     * Set the channel we will post to.
155
     *
156
     * @param string|null $channel
157
     *
158
     * @return $this
159
     */
160
    public function setChannel(?string $channel): self
161
    {
162
        $this->channel = $channel;
163
164
        return $this;
165
    }
166
167
    /**
168
     * @return string|null
169
     */
170
    public function getUsername(): ?string
171
    {
172
        return $this->username;
173
    }
174
175
    /**
176
     * Set the username we will post as.
177
     *
178
     * @param string|null $username
179
     *
180
     * @return $this
181
     */
182
    public function setUsername(?string $username): self
183
    {
184
        $this->username = $username;
185
186
        return $this;
187
    }
188
189
    /**
190
     * @return string|null
191
     */
192
    public function getIcon(): ?string
193
    {
194
        return $this->icon;
195
    }
196
197
    /**
198
     * Set the icon (either URL or emoji) we will post as.
199
     *
200
     * @param string|null $icon
201
     *
202
     * @return $this
203
     */
204
    public function setIcon(?string $icon): self
205
    {
206
        if (null === $icon) {
207
            $this->icon = $this->iconType = null;
208
209
            return $this;
210
        }
211
212
        if (':' === \mb_substr($icon, 0, 1) && ':' === \mb_substr($icon, \mb_strlen($icon) - 1, 1)) {
213
            $this->iconType = self::ICON_TYPE_EMOJI;
214
        } else {
215
            $this->iconType = self::ICON_TYPE_URL;
216
        }
217
218
        $this->icon = $icon;
219
220
        return $this;
221
    }
222
223
    /**
224
     * @return string|null
225
     */
226
    public function getIconType(): ?string
227
    {
228
        return $this->iconType;
229
    }
230
231
    /**
232
     * @return bool
233
     */
234
    public function getAllowMarkdown(): bool
235
    {
236
        return $this->allowMarkdown;
237
    }
238
239
    /**
240
     * Set whether message text should be formatted with
241
     * Slack's Markdown-like language.
242
     *
243
     * @param bool $value
244
     *
245
     * @return Message
246
     */
247
    public function setAllowMarkdown(bool $value): self
248
    {
249
        $this->allowMarkdown = $value;
250
251
        return $this;
252
    }
253
254
    public function enableMarkdown(): self
255
    {
256
        return $this->setAllowMarkdown(true);
257
    }
258
259
    public function disableMarkdown(): self
260
    {
261
        return $this->setAllowMarkdown(false);
262
    }
263
264
    /**
265
     * @return bool
266
     */
267
    public function getReplaceOriginal():? bool
268
    {
269
        return $this->replaceOriginal;
270
    }
271
272
    /**
273
     * @param bool $value
274
     * @return Message
275
     */
276
    public function setReplaceOriginal(bool $value): self
277
    {
278
        $this->replaceOriginal = $value;
279
280
        return $this;
281
    }
282
283
    public function enableReplaceOriginal(): self
284
    {
285
        return $this->setReplaceOriginal(true);
286
    }
287
288
    /**
289
     * @return array
290
     */
291
    public function getMarkdownInAttachments(): array
292
    {
293
        return $this->markdownInAttachments;
294
    }
295
296
    /**
297
     * Set the attachment fields which should be formatted
298
     * in Slack's Markdown-like language.
299
     *
300
     * @param array $fields
301
     *
302
     * @return Message
303
     */
304
    public function setMarkdownInAttachments(array $fields): self
305
    {
306
        $this->markdownInAttachments = $fields;
307
308
        return $this;
309
    }
310
311
    /**
312
     * Change the name of the user the post will be made as.
313
     *
314
     * @param string $username
315
     *
316
     * @return $this
317
     */
318
    public function from(string $username): self
319
    {
320
        return $this->setUsername($username);
321
    }
322
323
    /**
324
     * Change the channel the post will be made to.
325
     *
326
     * @param string $channel
327
     *
328
     * @return $this
329
     */
330
    public function to(string $channel): self
331
    {
332
        return $this->setChannel($channel);
333
    }
334
335
    /**
336
     * Chainable method for setting the icon.
337
     *
338
     * @param string $icon
339
     *
340
     * @return $this
341
     */
342
    public function withIcon(string $icon): self
343
    {
344
        return $this->setIcon($icon);
345
    }
346
347
    /**
348
     * Add an attachment to the message.
349
     *
350
     * @param Attachment $attachment
351
     *
352
     * @return $this
353
     */
354
    public function attach(Attachment $attachment): self
355
    {
356
        $this->attachments[] = $attachment;
357
358
        return $this;
359
    }
360
361
    /**
362
     * @return Attachment[]
363
     */
364
    public function getAttachments(): array
365
    {
366
        return $this->attachments;
367
    }
368
369
    /**
370
     * Set the attachments for the message.
371
     *
372
     * @param array $attachments
373
     *
374
     * @return $this
375
     */
376
    public function setAttachments(array $attachments): self
377
    {
378
        $this->clearAttachments();
379
380
        foreach ($attachments as $attachment) {
381
            $this->attach($attachment);
382
        }
383
384
        return $this;
385
    }
386
387
    /**
388
     * Remove all attachments for the message.
389
     *
390
     * @return $this
391
     */
392
    public function clearAttachments(): self
393
    {
394
        $this->attachments = [];
395
396
        return $this;
397
    }
398
399
    /**
400
     * Add an block to the message.
401
     *
402
     * @param Block $block
403
     *
404
     * @return $this
405
     */
406
    public function addBlock(Block $block): self
407
    {
408
        $this->blocks[] = $block;
409
410
        return $this;
411
    }
412
413
    /**
414
     * @return Block[]
415
     */
416
    public function getBlocks(): array
417
    {
418
        return $this->blocks;
419
    }
420
421
    /**
422
     * Set the blocks for the message.
423
     *
424
     * @param array $blocks
425
     *
426
     * @return $this
427
     */
428
    public function setBlocks(array $blocks): self
429
    {
430
        $this->clearBlocks();
431
432
        foreach ($blocks as $block) {
433
            $this->addBlock($block);
434
        }
435
436
        return $this;
437
    }
438
439
    /**
440
     * Remove all blocks for the message.
441
     *
442
     * @return $this
443
     */
444
    public function clearBlocks(): self
445
    {
446
        $this->blocks = [];
447
448
        return $this;
449
    }
450
451
    /**
452
     * Send the message.
453
     *
454
     * @param string|null $text The text to send
455
     *
456
     * @return Message
457
     */
458
    public function send(?string $text = null): self
459
    {
460
        if ($text) {
461
            $this->setText($text);
462
        }
463
464
        $this->client->sendMessage($this);
465
466
        return $this;
467
    }
468
}
469