Completed
Push — master ( 1e46a1...de26df )
by jelmer
02:16
created

Attachment::getMarkdownSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
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
     * @return bool
391
     */
392 10
    public function hasMarkdownSettings()
393
    {
394 10
        return !empty($this->markdownIn);
395
    }
396
397
    /**
398
     * @return array
399
     */
400
    public function getMarkdownSettings()
401
    {
402 10
        return $this->markdownIn;
403
    }
404
405 10
    /**
406 10
     * @inheritDoc
407
     */
408 10
    public function jsonSerialize()
409 10
    {
410 10
        return $this->get();
411 10
    }
412 10
413 10
    /**
414 10
     * Get the attachment in the format that slack requires.
415 10
     *
416 10
     * @return array
417
     */
418 10
    public function get()
419
    {
420
        return (new Builder($this))->build();
421
    }
422
}
423