Test Failed
Pull Request — master (#50)
by
unknown
03:54
created

Attachment   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 657
Duplicated Lines 0 %

Test Coverage

Coverage 99.15%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 125
c 4
b 1
f 0
dl 0
loc 657
ccs 116
cts 117
cp 0.9915
rs 8.96
wmc 43

38 Methods

Rating   Name   Duplication   Size   Complexity  
A setAuthorName() 0 5 1
A setImageUrl() 0 5 1
A setText() 0 5 1
A getActionsAsArrays() 0 9 2
A setCallbackId() 0 5 1
A getFooter() 0 3 1
A setFooterIcon() 0 5 1
A getTimestamp() 0 3 1
A setAuthorIcon() 0 5 1
A getMarkdownFields() 0 3 1
A getAuthorIcon() 0 3 1
A getImageUrl() 0 3 1
A getColor() 0 3 1
A setTimestamp() 0 5 1
A clearActions() 0 5 1
A getThumbUrl() 0 3 1
A setFallback() 0 5 1
A setPretext() 0 5 1
A getTitleLink() 0 3 1
A getFallback() 0 3 1
A toArray() 0 25 2
A getText() 0 3 1
A getPretext() 0 3 1
A setMarkdownFields() 0 5 1
A getActions() 0 3 1
A addAction() 0 13 3
A getAuthorName() 0 3 1
A setAuthorLink() 0 5 1
A getAuthorLink() 0 3 1
A setFooter() 0 5 1
A setActions() 0 9 2
A getFooterIcon() 0 3 1
A getTitle() 0 3 1
A setColor() 0 5 1
A setThumbUrl() 0 5 1
A setTitleLink() 0 5 1
A getCallbackId() 0 3 1
A setTitle() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like Attachment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Attachment, and based on these observations, apply Extract Interface, too.

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