Passed
Push — 1.0 ( be1921...2dfb46 )
by Alexander
02:25
created

Attachment::getImageUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Maknz\Slack;
4
5
use InvalidArgumentException;
6
7
class Attachment
8
{
9
    /**
10
     * The fallback text to use for clients that don't support attachments.
11
     *
12
     * @var string
13
     */
14
    protected $fallback;
15
16
    /**
17
     * Optional text that should appear within the attachment.
18
     *
19
     * @var string
20
     */
21
    protected $text;
22
23
    /**
24
     * Optional image that should appear within the attachment.
25
     *
26
     * @var string
27
     */
28
    protected $image_url;
29
30
    /**
31
     * Optional thumbnail that should appear within the attachment.
32
     *
33
     * @var string
34
     */
35
    protected $thumb_url;
36
37
    /**
38
     * Optional text that should appear above the formatted data.
39
     *
40
     * @var string
41
     */
42
    protected $pretext;
43
44
    /**
45
     * Optional title for the attachment.
46
     *
47
     * @var string
48
     */
49
    protected $title;
50
51
    /**
52
     * Optional title link for the attachment.
53
     *
54
     * @var string
55
     */
56
    protected $title_link;
57
58
    /**
59
     * Optional author name for the attachment.
60
     *
61
     * @var string
62
     */
63
    protected $author_name;
64
65
    /**
66
     * Optional author link for the attachment.
67
     *
68
     * @var string
69
     */
70
    protected $author_link;
71
72
    /**
73
     * Optional author icon for the attachment.
74
     *
75
     * @var string
76
     */
77
    protected $author_icon;
78
79
    /**
80
     * The color to use for the attachment.
81
     *
82
     * @var string
83
     */
84
    protected $color = 'good';
85
86
    /**
87
     * The text to use for the attachment footer.
88
     *
89
     * @var string
90
     */
91
    protected $footer;
92
93
    /**
94
     * The icon to use for the attachment footer.
95
     *
96
     * @var string
97
     */
98
    protected $footer_icon;
99
100
    /**
101
     * The timestamp to use for the attachment.
102
     *
103
     * @var \DateTime
104
     */
105
    protected $timestamp;
106
107
    /**
108
     * The fields of the attachment.
109
     *
110
     * @var array
111
     */
112
    protected $fields = [];
113
114
    /**
115
     * The fields of the attachment that Slack should interpret
116
     * with its Markdown-like language.
117
     *
118
     * @var array
119
     */
120
    protected $markdown_fields = [];
121
122
    /**
123
     * A collection of actions (buttons) to include in the attachment.
124
     * A maximum of 5 actions may be provided.
125
     *
126
     * @var array
127
     */
128
    protected $actions = [];
129
130
    /**
131
     * Instantiate a new Attachment.
132
     *
133
     * @param array $attributes
134
     *
135
     * @throws \InvalidArgumentException
136
     */
137 15
    public function __construct(array $attributes)
138
    {
139 15
        if (isset($attributes['fallback'])) {
140 15
            $this->setFallback($attributes['fallback']);
141
        }
142
143 15
        if (isset($attributes['text'])) {
144 15
            $this->setText($attributes['text']);
145
        }
146
147 15
        if (isset($attributes['image_url'])) {
148 4
            $this->setImageUrl($attributes['image_url']);
149
        }
150
151 15
        if (isset($attributes['thumb_url'])) {
152 4
            $this->setThumbUrl($attributes['thumb_url']);
153
        }
154
155 15
        if (isset($attributes['pretext'])) {
156 3
            $this->setPretext($attributes['pretext']);
157
        }
158
159 15
        if (isset($attributes['color'])) {
160 6
            $this->setColor($attributes['color']);
161
        }
162
163 15
        if (isset($attributes['footer'])) {
164 5
            $this->setFooter($attributes['footer']);
165
        }
166
167 15
        if (isset($attributes['footer_icon'])) {
168 5
            $this->setFooterIcon($attributes['footer_icon']);
169
        }
170
171 15
        if (isset($attributes['timestamp'])) {
172 5
            $this->setTimestamp($attributes['timestamp']);
173
        }
174
175 15
        if (isset($attributes['fields'])) {
176 6
            $this->setFields($attributes['fields']);
177
        }
178
179 15
        if (isset($attributes['mrkdwn_in'])) {
180 5
            $this->setMarkdownFields($attributes['mrkdwn_in']);
181
        }
182
183 15
        if (isset($attributes['title'])) {
184 3
            $this->setTitle($attributes['title']);
185
        }
186
187 15
        if (isset($attributes['title_link'])) {
188 3
            $this->setTitleLink($attributes['title_link']);
189
        }
190
191 15
        if (isset($attributes['author_name'])) {
192 4
            $this->setAuthorName($attributes['author_name']);
193
        }
194
195 15
        if (isset($attributes['author_link'])) {
196 4
            $this->setAuthorLink($attributes['author_link']);
197
        }
198
199 15
        if (isset($attributes['author_icon'])) {
200 4
            $this->setAuthorIcon($attributes['author_icon']);
201
        }
202
203 15
        if (isset($attributes['actions'])) {
204 4
            $this->setActions($attributes['actions']);
205
        }
206 15
    }
207
208
    /**
209
     * Get the fallback text.
210
     *
211
     * @return string
212
     */
213 7
    public function getFallback()
214
    {
215 7
        return $this->fallback;
216
    }
217
218
    /**
219
     * Set the fallback text.
220
     *
221
     * @param string $fallback
222
     * @return $this
223
     */
224 15
    public function setFallback($fallback)
225
    {
226 15
        $this->fallback = $fallback;
227
228 15
        return $this;
229
    }
230
231
    /**
232
     * Get the optional text to appear within the attachment.
233
     *
234
     * @return string
235
     */
236 6
    public function getText()
237
    {
238 6
        return $this->text;
239
    }
240
241
    /**
242
     * Set the optional text to appear within the attachment.
243
     *
244
     * @param string $text
245
     * @return $this
246
     */
247 15
    public function setText($text)
248
    {
249 15
        $this->text = $text;
250
251 15
        return $this;
252
    }
253
254
    /**
255
     * Get the optional image to appear within the attachment.
256
     *
257
     * @return string
258
     */
259 4
    public function getImageUrl()
260
    {
261 4
        return $this->image_url;
262
    }
263
264
    /**
265
     * Set the optional image to appear within the attachment.
266
     *
267
     * @param string $image_url
268
     * @return $this
269
     */
270 4
    public function setImageUrl($image_url)
271
    {
272 4
        $this->image_url = $image_url;
273
274 4
        return $this;
275
    }
276
277
    /**
278
     * Get the optional thumbnail to appear within the attachment.
279
     *
280
     * @return string
281
     */
282 4
    public function getThumbUrl()
283
    {
284 4
        return $this->thumb_url;
285
    }
286
287
    /**
288
     * Set the optional thumbnail to appear within the attachment.
289
     *
290
     * @param string $thumb_url
291
     * @return $this
292
     */
293 4
    public function setThumbUrl($thumb_url)
294
    {
295 4
        $this->thumb_url = $thumb_url;
296
297 4
        return $this;
298
    }
299
300
    /**
301
     * Get the text that should appear above the formatted data.
302
     *
303
     * @return string
304
     */
305 6
    public function getPretext()
306
    {
307 6
        return $this->pretext;
308
    }
309
310
    /**
311
     * Set the text that should appear above the formatted data.
312
     *
313
     * @param string $pretext
314
     * @return $this
315
     */
316 3
    public function setPretext($pretext)
317
    {
318 3
        $this->pretext = $pretext;
319
320 3
        return $this;
321
    }
322
323
    /**
324
     * Get the color to use for the attachment.
325
     *
326
     * @return string
327
     */
328 6
    public function getColor()
329
    {
330 6
        return $this->color;
331
    }
332
333
    /**
334
     * Set the color to use for the attachment.
335
     *
336
     * @param string $color
337
     * @return $this
338
     */
339 6
    public function setColor($color)
340
    {
341 6
        $this->color = $color;
342
343 6
        return $this;
344
    }
345
346
    /**
347
     * Get the footer to use for the attachment.
348
     *
349
     * @return string
350
     */
351 5
    public function getFooter()
352
    {
353 5
        return $this->footer;
354
    }
355
356
    /**
357
     * Set the footer text to use for the attachment.
358
     *
359
     * @param string $footer
360
     * @return $this
361
     */
362 5
    public function setFooter($footer)
363
    {
364 5
        $this->footer = $footer;
365
366 5
        return $this;
367
    }
368
369
    /**
370
     * Get the footer icon to use for the attachment.
371
     *
372
     * @return string
373
     */
374 5
    public function getFooterIcon()
375
    {
376 5
        return $this->footer_icon;
377
    }
378
379
    /**
380
     * Set the footer icon to use for the attachment.
381
     *
382
     * @param string $footerIcon
383
     * @return $this
384
     */
385 5
    public function setFooterIcon($footerIcon)
386
    {
387 5
        $this->footer_icon = $footerIcon;
388
389 5
        return $this;
390
    }
391
392
    /**
393
     * Get the timestamp to use for the attachment.
394
     *
395
     * @return \DateTime
396
     */
397 5
    public function getTimestamp()
398
    {
399 5
        return $this->timestamp;
400
    }
401
402
    /**
403
     * Set the timestamp to use for the attachment.
404
     *
405
     * @param \DateTime $timestamp
406
     * @return $this
407
     */
408 5
    public function setTimestamp($timestamp)
409
    {
410 5
        $this->timestamp = $timestamp;
411
412 5
        return $this;
413
    }
414
415
    /**
416
     * Get the title to use for the attachment.
417
     *
418
     * @return string
419
     */
420 4
    public function getTitle()
421
    {
422 4
        return $this->title;
423
    }
424
425
    /**
426
     * Set the title to use for the attachment.
427
     *
428
     * @param string $title
429
     * @return $this
430
     */
431 3
    public function setTitle($title)
432
    {
433 3
        $this->title = $title;
434
435 3
        return $this;
436
    }
437
438
    /**
439
     * Get the title link to use for the attachment.
440
     *
441
     * @return string
442
     */
443 4
    public function getTitleLink()
444
    {
445 4
        return $this->title_link;
446
    }
447
448
    /**
449
     * Set the title link to use for the attachment.
450
     *
451
     * @param string $title_link
452
     * @return $this
453
     */
454 3
    public function setTitleLink($title_link)
455
    {
456 3
        $this->title_link = $title_link;
457
458 3
        return $this;
459
    }
460
461
    /**
462
     * Get the author name to use for the attachment.
463
     *
464
     * @return string
465
     */
466 4
    public function getAuthorName()
467
    {
468 4
        return $this->author_name;
469
    }
470
471
    /**
472
     * Set the author name to use for the attachment.
473
     *
474
     * @param string $author_name
475
     * @return $this
476
     */
477 4
    public function setAuthorName($author_name)
478
    {
479 4
        $this->author_name = $author_name;
480
481 4
        return $this;
482
    }
483
484
    /**
485
     * Get the author link to use for the attachment.
486
     *
487
     * @return string
488
     */
489 4
    public function getAuthorLink()
490
    {
491 4
        return $this->author_link;
492
    }
493
494
    /**
495
     * Set the author link to use for the attachment.
496
     *
497
     * @param string $author_link
498
     *
499
     * @return $this
500
     */
501 4
    public function setAuthorLink($author_link)
502
    {
503 4
        $this->author_link = $author_link;
504
505 4
        return $this;
506
    }
507
508
    /**
509
     * Get the author icon to use for the attachment.
510
     *
511
     * @return string
512
     */
513 4
    public function getAuthorIcon()
514
    {
515 4
        return $this->author_icon;
516
    }
517
518
    /**
519
     * Set the author icon to use for the attachment.
520
     *
521
     * @param string $author_icon
522
     * @return $this
523
     */
524 4
    public function setAuthorIcon($author_icon)
525
    {
526 4
        $this->author_icon = $author_icon;
527
528 4
        return $this;
529
    }
530
531
    /**
532
     * Get the fields for the attachment.
533
     *
534
     * @return array
535
     */
536 9
    public function getFields()
537
    {
538 9
        return $this->fields;
539
    }
540
541
    /**
542
     * Set the fields for the attachment.
543
     *
544
     * @param array $fields
545
     *
546
     * @return $this
547
     *
548
     * @throws \InvalidArgumentException
549
     */
550 7
    public function setFields(array $fields)
551
    {
552 7
        $this->clearFields();
553
554 7
        foreach ($fields as $field) {
555 3
            $this->addField($field);
556
        }
557
558 7
        return $this;
559
    }
560
561
    /**
562
     * Add a field to the attachment.
563
     *
564
     * @param mixed $field
565
     *
566
     * @return $this
567
     *
568
     * @throws \InvalidArgumentException
569
     */
570 6
    public function addField($field)
571
    {
572 6
        if ($field instanceof AttachmentField) {
573 1
            $this->fields[] = $field;
574
575 1
            return $this;
576 5
        } elseif (is_array($field)) {
577 5
            $this->fields[] = new AttachmentField($field);
578
579 5
            return $this;
580
        }
581
582
        throw new InvalidArgumentException('The attachment field must be an instance of Maknz\Slack\AttachmentField or a keyed array');
583
    }
584
585
    /**
586
     * Clear the fields for the attachment.
587
     *
588
     * @return $this
589
     */
590 7
    public function clearFields()
591
    {
592 7
        $this->fields = [];
593
594 7
        return $this;
595
    }
596
597
    /**
598
     * Clear the actions for the attachment.
599
     *
600
     * @return $this
601
     */
602 4
    public function clearActions()
603
    {
604 4
        $this->actions = [];
605
606 4
        return $this;
607
    }
608
609
    /**
610
     * Get the fields Slack should interpret in its
611
     * Markdown-like language.
612
     *
613
     * @return array
614
     */
615 5
    public function getMarkdownFields()
616
    {
617 5
        return $this->markdown_fields;
618
    }
619
620
    /**
621
     * Set the fields Slack should interpret in its
622
     * Markdown-like language.
623
     *
624
     * @param array $fields
625
     * @return $this
626
     */
627 7
    public function setMarkdownFields(array $fields)
628
    {
629 7
        $this->markdown_fields = $fields;
630
631 7
        return $this;
632
    }
633
634
    /**
635
     * Get the collection of actions (buttons) to include in the attachment.
636
     *
637
     * @return AttachmentAction[]
638
     */
639 6
    public function getActions()
640
    {
641 6
        return $this->actions;
642
    }
643
644
    /**
645
     * Set the collection of actions (buttons) to include in the attachment.
646
     *
647
     * @param array $actions
648
     *
649
     * @return Attachment
650
     *
651
     * @throws \InvalidArgumentException
652
     */
653 4
    public function setActions($actions)
654
    {
655 4
        $this->clearActions();
656
657 4
        foreach ($actions as $action) {
658 2
            $this->addAction($action);
659
        }
660
661 4
        return $this;
662
    }
663
664
    /**
665
     * Add an action to the attachment.
666
     *
667
     * @param mixed $action
668
     *
669
     * @return $this
670
     *
671
     * @throws \InvalidArgumentException
672
     */
673 4
    public function addAction($action)
674
    {
675 4
        if ($action instanceof AttachmentAction) {
676 1
            $this->actions[] = $action;
677
678 1
            return $this;
679 3
        } elseif (is_array($action)) {
680 3
            $this->actions[] = new AttachmentAction($action);
681
682 3
            return $this;
683
        }
684
685
        throw new InvalidArgumentException('The attachment action must be an instance of Maknz\Slack\AttachmentAction or a keyed array');
686
    }
687
688
    /**
689
     * Convert this attachment to its array representation.
690
     *
691
     * @return array
692
     */
693 4
    public function toArray()
694
    {
695
        $data = [
696 4
            'fallback' => $this->getFallback(),
697 4
            'text' => $this->getText(),
698 4
            'pretext' => $this->getPretext(),
699 4
            'color' => $this->getColor(),
700 4
            'footer' => $this->getFooter(),
701 4
            'footer_icon' => $this->getFooterIcon(),
702 4
            'ts' => $this->getTimestamp() ? $this->getTimestamp()->getTimestamp() : null,
703 4
            'mrkdwn_in' => $this->getMarkdownFields(),
704 4
            'image_url' => $this->getImageUrl(),
705 4
            'thumb_url' => $this->getThumbUrl(),
706 4
            'title' => $this->getTitle(),
707 4
            'title_link' => $this->getTitleLink(),
708 4
            'author_name' => $this->getAuthorName(),
709 4
            'author_link' => $this->getAuthorLink(),
710 4
            'author_icon' => $this->getAuthorIcon(),
711
        ];
712
713 4
        $data['fields'] = $this->getFieldsAsArrays();
714 4
        $data['actions'] = $this->getActionsAsArrays();
715
716 4
        return $data;
717
    }
718
719
    /**
720
     * Iterates over all fields in this attachment and returns
721
     * them in their array form.
722
     *
723
     * @return array
724
     */
725 4
    protected function getFieldsAsArrays()
726
    {
727 4
        $fields = [];
728
729 4
        foreach ($this->getFields() as $field) {
730 2
            $fields[] = $field->toArray();
731
        }
732
733 4
        return $fields;
734
    }
735
736
    /**
737
     * Iterates over all actions in this attachment and returns
738
     * them in their array form.
739
     *
740
     * @return array
741
     */
742 4
    protected function getActionsAsArrays()
743
    {
744 4
        $actions = [];
745
746 4
        foreach ($this->getActions() as $action) {
747 2
            $actions[] = $action->toArray();
748
        }
749
750 4
        return $actions;
751
    }
752
}
753