Completed
Push — master ( 6cd9bd...1e46a1 )
by jelmer
02:18
created

Attachment::hasThumbnail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack\Attachment;
4
5
use JsonSerializable;
6
use Pageon\SlackWebhookMonolog\General\Url;
7
8
/**
9
 * A more richly-formatted message.
10
 *
11
 * @author Jelmer Prins <[email protected]>
12
 *
13
 * @since 0.4.0
14
 */
15
final class Attachment implements JsonSerializable
16
{
17
    /**
18
     * A plain-text summary of the attachment.
19
     * This text will be used in clients that don't show formatted text (eg. IRC, mobile notifications).
20
     * It should not contain any markup.
21
     *
22
     * @var string
23
     */
24
    private $fallback;
25
26
    /**
27
     * This is the main text in a message attachment, and can contain standard message markup.
28
     * The content will automatically collapse if it contains 700+ characters or 5+ linebreaks,
29
     * and will display a "Show more..." link to expand the content.
30
     *
31
     * @var string|null
32
     */
33
    private $text = null;
34
35
    /**
36
     * This value is used to color the border along the left side of the message attachment.
37
     *
38
     * @var Colour|null
39
     */
40
    private $colour = null;
41
42
    /**
43
     * This is optional text that appears above the message attachment block.
44
     *
45
     * @var string|null
46
     */
47
    private $pretext = null;
48
49
    /**
50
     * The author parameters will display a small section at the top of a message attachment.
51
     *
52
     * @var Author|null
53
     */
54
    private $author = null;
55
56
    /**
57
     * The title is displayed as larger, bold text near the top of a message attachment.
58
     * By passing a valid URL in the link parameter (optional), the title will be hyperlinked.
59
     *
60
     * @var Title|null
61
     */
62
    private $title = null;
63
64
    /**
65
     * Will be displayed in a table inside the message attachment.
66
     *
67
     * @var Field[]
68
     */
69
    private $fields = [];
70
71
    /**
72
     * A valid URL to an image file that will be displayed inside a message attachment.
73
     * We currently support the following formats: GIF, JPEG, PNG, and BMP.
74
     * Large images will be resized to a maximum width of 400px or a maximum height of 500px,
75
     * while still maintaining the original aspect ratio.
76
     *
77
     * @var Url|null
78
     */
79
    private $image = null;
80
81
    /**
82
     * A valid URL to an image file that will be displayed as a thumbnail on the right side of a message attachment.
83
     * We currently support the following formats: GIF, JPEG, PNG, and BMP.
84
     * The thumbnail's longest dimension will be scaled down to 75px while maintaining the aspect ratio of the image.
85
     * The filesize of the image must also be less than 500 KB.
86
     *
87
     * @var Url|null
88
     */
89
    private $thumbnail = null;
90
91
    /**
92
     * By default bot message text will be formatted, but attachments are not.
93
     * Attachments need to enable the markdown manually.
94
     *
95
     * @var array
96
     */
97
    private $markdownIn = [];
98
99
    /**
100
     * Attachment constructor.
101
     * I opted for setters since there are so many optional parameters.
102
     * A new instance would look be messy if you only needed the last optional parameter.
103
     *
104
     * @param string $fallback
105
     */
106 10
    public function __construct($fallback)
107
    {
108 10
        if (empty($fallback)) {
109 1
            throw new \InvalidArgumentException('The fallback is required');
110
        }
111
112 10
        $this->fallback = $fallback;
113 10
    }
114
115
    /**
116
     * @return string
117
     */
118 10
    public function getFallback()
119
    {
120 10
        return $this->fallback;
121
    }
122
123
    /**
124
     * @param string $text
125
     *
126
     * @return self
127
     */
128 1
    public function setText($text)
129
    {
130 1
        $this->text = $text;
131
132 1
        return $this;
133
    }
134
135
    /**
136
     * @return bool
137
     */
138 10
    public function hasText()
139
    {
140 10
        return $this->text !== null;
141
    }
142
143
    /**
144
     * @return string|null
145
     */
146 1
    public function getText()
147
    {
148 1
        return $this->text;
149
    }
150
151
    /**
152
     * @param Colour $colour
153
     *
154
     * @return self
155
     */
156 1
    public function setColour(Colour $colour)
157
    {
158 1
        $this->colour = $colour;
159
160 1
        return $this;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166 10
    public function hasColour()
167
    {
168 10
        return $this->colour !== null;
169
    }
170
171
    /**
172
     * @return Colour|null
173
     */
174 1
    public function getColour()
175
    {
176 1
        return $this->colour;
177
    }
178
179
    /**
180
     * @param string $pretext
181
     *
182
     * @return self
183
     */
184 1
    public function setPretext($pretext)
185
    {
186 1
        $this->pretext = $pretext;
187
188 1
        return $this;
189
    }
190
191
    /**
192
     * @return bool
193
     */
194 10
    public function hasPretext()
195
    {
196 10
        return $this->pretext !== null;
197
    }
198
199
    /**
200
     * @return string|null
201
     */
202 1
    public function getPretext()
203
    {
204 1
        return $this->pretext;
205
    }
206
207
    /**
208
     * @param Author $author
209
     *
210
     * @return self
211
     */
212 1
    public function setAuthor(Author $author)
213
    {
214 1
        $this->author = $author;
215
216 1
        return $this;
217
    }
218
219
    /**
220
     * @return bool
221
     */
222 10
    public function hasAuthor()
223
    {
224 10
        return $this->author !== null;
225
    }
226
227
    /**
228
     * @return Author|null
229
     */
230 1
    public function getAuthor()
231
    {
232 1
        return $this->author;
233
    }
234
235
    /**
236
     * @param Title $title
237
     *
238
     * @return self
239
     */
240 1
    public function setTitle(Title $title)
241
    {
242 1
        $this->title = $title;
243
244 1
        return $this;
245
    }
246
247
    /**
248
     * @return bool
249
     */
250 10
    public function hasTitle()
251
    {
252 10
        return $this->title !== null;
253
    }
254
255
    /**
256
     * @return Title|null
257
     */
258 1
    public function getTitle()
259
    {
260 1
        return $this->title;
261
    }
262
263
    /**
264
     * @param Field $field
265
     */
266 1
    public function addField(Field $field)
267
    {
268 1
        $this->fields[] = $field;
269 1
    }
270
271
    /**
272
     * @param Field[] $fields
273
     */
274 1
    public function addFields(array $fields)
275
    {
276 1
        foreach ($fields as $field) {
277 1
            $this->addField($field);
278 1
        }
279 1
    }
280
281
    /**
282
     * @return bool
283
     */
284 10
    public function hasFields()
285
    {
286 10
        return !empty($this->fields);
287
    }
288
289
    /**
290
     * @return Field[]
291
     */
292 1
    public function getFields()
293
    {
294 1
        return $this->fields;
295
    }
296
297
    /**
298
     * @param Url $image
299
     *
300
     * @return self
301
     */
302 1
    public function setImage(Url $image)
303
    {
304 1
        $this->image = $image;
305
306 1
        return $this;
307
    }
308
309
    /**
310
     * @return bool
311
     */
312 10
    public function hasImage()
313
    {
314 10
        return $this->image !== null;
315
    }
316
317
    /**
318
     * @return Url|null
319
     */
320 1
    public function getImage()
321
    {
322 1
        return $this->image;
323
    }
324
325
    /**
326
     * @param Url $thumbnail
327
     *
328
     * @return self
329
     */
330 1
    public function setThumbnail(Url $thumbnail)
331
    {
332 1
        $this->thumbnail = $thumbnail;
333
334 1
        return $this;
335
    }
336
337
    /**
338
     * @return bool
339
     */
340 10
    public function hasThumbnail()
341
    {
342 10
        return $this->thumbnail !== null;
343
    }
344
345
    /**
346
     * @return Url|null
347
     */
348 1
    public function getThumbnail()
349
    {
350 1
        return $this->thumbnail;
351
    }
352
353
    /**
354
     * Enable markdown in the pretext.
355
     *
356
     * @return $this
357
     */
358 1
    public function enableMarkdownForPretext()
359
    {
360 1
        $this->markdownIn[] = 'pretext';
361
362 1
        return $this;
363
    }
364
365
    /**
366
     * Enable markdown in the text.
367
     *
368
     * @return $this
369
     */
370 1
    public function enableMarkdownForText()
371
    {
372 1
        $this->markdownIn[] = 'text';
373
374 1
        return $this;
375
    }
376
377
    /**
378
     * Enable markdown for the value in the fields.
379
     *
380
     * @return $this
381
     */
382 1
    public function enableMarkdownForFieldValues()
383
    {
384 1
        $this->markdownIn[] = 'fields';
385
386 1
        return $this;
387
    }
388
389
    /**
390
     * @inheritDoc
391
     */
392 10
    public function jsonSerialize()
393
    {
394 10
        return $this->get();
395
    }
396
397
    /**
398
     * Get the attachment in the format that slack requires.
399
     *
400
     * @return array
401
     */
402 10
    public function get()
403
    {
404
        $attachment = [
405 10
            'fallback' => $this->getFallback()
406 10
        ];
407
408 10
        $attachment = $this->addColourToAttachment($attachment);
409 10
        $attachment = $this->addTextToAttachment($attachment);
410 10
        $attachment = $this->addPretextToAttachment($attachment);
411 10
        $attachment = $this->addAuthorToAttachment($attachment);
412 10
        $attachment = $this->addTitleToAttachment($attachment);
413 10
        $attachment = $this->addFieldsToAttachment($attachment);
414 10
        $attachment = $this->addImageToAttachment($attachment);
415 10
        $attachment = $this->addThumbnailToAttachment($attachment);
416 10
        $attachment = $this->addMarkdownSettingsToAttachment($attachment);
417
418 10
        return $attachment;
419
    }
420
421
    /**
422
     * If set, add the colour to the attachment.
423
     *
424
     * @param array $attachment
425
     *
426
     * @return array
427
     */
428 10
    private function addColourToAttachment(array $attachment)
429
    {
430 10
        if (!$this->hasColour()) {
431 10
            return $attachment;
432
        }
433
434 1
        $attachment['color'] = $this->getColour();
435
436 1
        return $attachment;
437
    }
438
439
    /**
440
     * If set, add the text to the attachment.
441
     *
442
     * @param array $attachment
443
     *
444
     * @return array
445
     */
446 10
    private function addTextToAttachment(array $attachment)
447
    {
448 10
        if (!$this->hasText()) {
449 10
            return $attachment;
450
        }
451
452 1
        $attachment['text'] = $this->getText();
453
454 1
        return $attachment;
455
    }
456
457
    /**
458
     * If set, add the pretext to the attachment.
459
     *
460
     * @param array $attachment
461
     *
462
     * @return array
463
     */
464 10
    private function addPretextToAttachment(array $attachment)
465
    {
466 10
        if (!$this->hasPretext()) {
467 10
            return $attachment;
468
        }
469
470 1
        $attachment['pretext'] = $this->getPretext();
471
472 1
        return $attachment;
473
    }
474
475
    /**
476
     * If set, add the author to the attachment.
477
     *
478
     * @param array $attachment
479
     *
480
     * @return array
481
     */
482 10
    private function addAuthorToAttachment(array $attachment)
483
    {
484 10
        if (!$this->hasAuthor()) {
485 10
            return $attachment;
486
        }
487
488 1
        $attachment['author_name'] = $this->getAuthor();
489
490 1
        if ($this->getAuthor()->hasIcon()) {
491 1
            $attachment['author_icon'] = $this->getAuthor()->getIcon();
492 1
        }
493
494 1
        if ($this->getAuthor()->hasLink()) {
495 1
            $attachment['author_link'] = $this->getAuthor()->getLink();
496 1
        }
497
498 1
        return $attachment;
499
    }
500
501
    /**
502
     * If set, add the title to the attachment.
503
     *
504
     * @param array $attachment
505
     *
506
     * @return array
507
     */
508 10
    private function addTitleToAttachment(array $attachment)
509
    {
510 10
        if (!$this->hasTitle()) {
511 10
            return $attachment;
512
        }
513
514 1
        $attachment['title'] = $this->getTitle();
515
516 1
        if ($this->getTitle()->hasLink()) {
517 1
            $attachment['title_link'] = $this->getTitle()->getLink();
518 1
        }
519
520 1
        return $attachment;
521
    }
522
523
    /**
524
     * If set, add the fields to the attachment.
525
     *
526
     * @param array $attachment
527
     *
528
     * @return array
529
     */
530 10
    private function addFieldsToAttachment(array $attachment)
531
    {
532 10
        if (!$this->hasFields()) {
533 10
            return $attachment;
534
        }
535
536 1
        $attachment['fields'] = $this->getFields();
537
538 1
        return $attachment;
539
    }
540
541
    /**
542
     * If set, add the image to the attachment.
543
     *
544
     * @param array $attachment
545
     *
546
     * @return array
547
     */
548 10
    private function addImageToAttachment(array $attachment)
549
    {
550 10
        if (!$this->hasImage()) {
551 10
            return $attachment;
552
        }
553
554 1
        $attachment['image_url'] = $this->getImage();
555
556 1
        return $attachment;
557
    }
558
559
    /**
560
     * If set, add the thumbnail to the attachment.
561
     *
562
     * @param array $attachment
563
     *
564
     * @return array
565
     */
566 10
    private function addThumbnailToAttachment(array $attachment)
567
    {
568 10
        if (!$this->hasThumbnail()) {
569 10
            return $attachment;
570
        }
571
572 1
        $attachment['thumb_url'] = $this->getThumbnail();
573
574 1
        return $attachment;
575
    }
576
577
    /**
578
     * If set, add the fields to the attachment.
579
     *
580
     * @param array $attachment
581
     *
582
     * @return array
583
     */
584 10
    private function addMarkdownSettingsToAttachment(array $attachment)
585
    {
586 10
        if (empty($this->markdownIn)) {
587 10
            return $attachment;
588
        }
589
590 1
        $attachment['mrkdwn_in'] = $this->markdownIn;
591
592 1
        return $attachment;
593
    }
594
}
595