Completed
Push — master ( de26df...7eff32 )
by jelmer
02:31
created

Attachment   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 1
cbo 2
dl 0
loc 188
ccs 45
cts 50
cp 0.9
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setText() 0 6 1
A setColour() 0 6 1
A setPretext() 0 6 1
A setAuthor() 0 14 3
A setTitle() 0 10 2
A addField() 0 10 2
A addFields() 0 8 2
A setImage() 0 6 1
A setThumbnail() 0 6 1
A jsonSerialize() 0 4 1
A get() 0 4 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
     * The attachment itself.
19
     * We enable markdown in everything, why wouldn't we?
20
     *
21
     * @var array
22
     */
23
    private $attachment = ['mrkdwn_in' => ["pretext", "text", "fields"]];
24
25
    /**
26
     * Attachment constructor.
27
     * I opted for setters since there are so many optional parameters.
28
     * A new instance would look be messy if you only needed the last optional parameter.
29
     *
30
     * @param string $fallback A plain-text summary of the attachment.
31
     */
32 9
    public function __construct($fallback)
33
    {
34 9
        if (empty($fallback)) {
35 1
            throw new \InvalidArgumentException('The fallback is required');
36
        }
37
38 9
        $this->attachment['fallback'] = $fallback;
39 9
    }
40
41
    /**
42
     * This is the main text in a message attachment.
43
     *
44
     * @param string $text
45
     *
46
     * @return self
47
     */
48 1
    public function setText($text)
49
    {
50 1
        $this->attachment['text'] = $text;
51
52 1
        return $this;
53
    }
54
55
    /**
56
     * This value is used to color the border along the left side of the message attachment.
57
     *
58
     * @param Colour $colour
59
     *
60
     * @return self
61
     */
62 1
    public function setColour(Colour $colour)
63
    {
64 1
        $this->attachment['color'] = $colour;
65
66 1
        return $this;
67
    }
68
69
    /**
70
     * This is optional text that appears above the message attachment block.
71
     *
72
     * @param string $pretext
73
     *
74
     * @return self
75
     */
76 1
    public function setPretext($pretext)
77
    {
78 1
        $this->attachment['pretext'] = $pretext;
79
80 1
        return $this;
81
    }
82
83
    /**
84
     * The author parameters will display a small section at the top of a message attachment.
85
     *
86
     * @param Author $author
87
     *
88
     * @return self
89
     */
90 1
    public function setAuthor(Author $author)
91
    {
92 1
        $this->attachment['author_name'] = $author;
93
94 1
        if ($author->hasIcon()) {
95 1
            $this->attachment['author_icon'] = $author->getIcon();
96 1
        }
97
98 1
        if ($author->hasLink()) {
99 1
            $this->attachment['author_link'] = $author->getLink();
100 1
        }
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * The title is displayed as larger, bold text near the top of a message attachment.
107
     *
108
     * @param Title $title
109
     *
110
     * @return self
111
     */
112 1
    public function setTitle(Title $title)
113
    {
114 1
        $this->attachment['title'] = $title;
115
116 1
        if ($title->hasLink()) {
117 1
            $this->attachment['title_link'] = $title->getLink();
118 1
        }
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * Will be displayed in a table inside the message attachment.
125
     *
126
     * @param Field $field
127
     *
128
     * @return self
129
     */
130 1
    public function addField(Field $field)
131
    {
132 1
        if (!isset($this->attachment['fields'])) {
133 1
            $this->attachment['fields'] = [];
134 1
        }
135
136 1
        $this->attachment['fields'][] = $field;
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * Add multiple fields in 1 call.
143
     *
144
     * @param Field[] $fields
145
     *
146
     * @return self
147
     */
148
    public function addFields(array $fields)
149
    {
150
        foreach ($fields as $field) {
151
            $this->addField($field);
152
        }
153
154
        return $this;
155
    }
156
157
    /**
158
     * A valid URL to an image file that will be displayed inside a message attachment.
159
     *
160
     * @param Url $image
161
     *
162
     * @return self
163
     */
164 1
    public function setImage(Url $image)
165
    {
166 1
        $this->attachment['image_url'] = $image;
167
168 1
        return $this;
169
    }
170
171
    /**
172
     * A valid URL to an image file that will be displayed as a thumbnail on the right side of a message attachment.
173
     *
174
     * @param Url $thumbnail
175
     *
176
     * @return self
177
     */
178 1
    public function setThumbnail(Url $thumbnail)
179
    {
180 1
        $this->attachment['thumb_url'] = $thumbnail;
181
182 1
        return $this;
183
    }
184
185
    /**
186
     * @inheritDoc
187
     */
188 9
    public function jsonSerialize()
189
    {
190 9
        return $this->get();
191
    }
192
193
    /**
194
     * Get the attachment in the format that slack requires.
195
     *
196
     * @return array
197
     */
198 9
    public function get()
199
    {
200 9
        return $this->attachment;
201
    }
202
}
203