Passed
Pull Request — master (#38)
by
unknown
02:06
created

Attachment   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 624
Duplicated Lines 0 %

Test Coverage

Coverage 99.15%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 119
dl 0
loc 624
ccs 116
cts 117
cp 0.9915
rs 9.1199
c 3
b 1
f 0
wmc 41

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