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

Builder::addAuthorToAttachment()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 5
nop 1
1
<?php
2
3
namespace Pageon\SlackWebhookMonolog\Slack\Attachment;
4
5
use Pageon\SlackWebhookMonolog\General\SerializeToString;
6
use Pageon\SlackWebhookMonolog\General\Url;
7
8
/**
9
 * This will turn the attachment info something we can send to slack.
10
 *
11
 * @author Jelmer Prins <[email protected]>
12
 *
13
 * @since 0.4.0
14
 */
15
final class Builder
16
{
17
    /**
18
     * @var Attachment
19
     */
20
    private $attachment;
21
22
    /**
23
     * @param Attachment $attachment
24
     */
25
    public function __construct(Attachment $attachment)
26
    {
27
        $this->attachment = $attachment;
28
    }
29
30
    /**
31
     * Get the attachment in the format that slack requires.
32
     *
33
     * @return array
34
     */
35
    public function build()
36
    {
37
        $attachment = [
38
            'fallback' => $this->attachment->getFallback()
39
        ];
40
41
        $attachment = $this->addColourToAttachment($attachment);
42
        $attachment = $this->addTextToAttachment($attachment);
43
        $attachment = $this->addPretextToAttachment($attachment);
44
        $attachment = $this->addAuthorToAttachment($attachment);
45
        $attachment = $this->addTitleToAttachment($attachment);
46
        $attachment = $this->addFieldsToAttachment($attachment);
47
        $attachment = $this->addImageToAttachment($attachment);
48
        $attachment = $this->addThumbnailToAttachment($attachment);
49
        $attachment = $this->addMarkdownSettingsToAttachment($attachment);
50
51
        return $attachment;
52
    }
53
54
    /**
55
     * If set, add the colour to the attachment.
56
     *
57
     * @param array $attachment
58
     *
59
     * @return array
60
     */
61
    private function addColourToAttachment(array $attachment)
62
    {
63
        if (!$this->attachment->hasColour()) {
64
            return $attachment;
65
        }
66
67
        $attachment['color'] = $this->attachment->getColour();
68
69
        return $attachment;
70
    }
71
72
    /**
73
     * If set, add the text to the attachment.
74
     *
75
     * @param array $attachment
76
     *
77
     * @return array
78
     */
79
    private function addTextToAttachment(array $attachment)
80
    {
81
        if (!$this->attachment->hasText()) {
82
            return $attachment;
83
        }
84
85
        $attachment['text'] = $this->attachment->getText();
86
87
        return $attachment;
88
    }
89
90
    /**
91
     * If set, add the pretext to the attachment.
92
     *
93
     * @param array $attachment
94
     *
95
     * @return array
96
     */
97
    private function addPretextToAttachment(array $attachment)
98
    {
99
        if (!$this->attachment->hasPretext()) {
100
            return $attachment;
101
        }
102
103
        $attachment['pretext'] = $this->attachment->getPretext();
104
105
        return $attachment;
106
    }
107
108
    /**
109
     * If set, add the author to the attachment.
110
     *
111
     * @param array $attachment
112
     *
113
     * @return array
114
     */
115
    private function addAuthorToAttachment(array $attachment)
116
    {
117
        if (!$this->attachment->hasAuthor()) {
118
            return $attachment;
119
        }
120
121
        $attachment['author_name'] = $this->attachment->getAuthor();
122
123
        if ($this->attachment->getAuthor()->hasIcon()) {
124
            $attachment['author_icon'] = $this->attachment->getAuthor()->getIcon();
125
        }
126
127
        if ($this->attachment->getAuthor()->hasLink()) {
128
            $attachment['author_link'] = $this->attachment->getAuthor()->getLink();
129
        }
130
131
        return $attachment;
132
    }
133
134
    /**
135
     * If set, add the title to the attachment.
136
     *
137
     * @param array $attachment
138
     *
139
     * @return array
140
     */
141
    private function addTitleToAttachment(array $attachment)
142
    {
143
        if (!$this->attachment->hasTitle()) {
144
            return $attachment;
145
        }
146
147
        $attachment['title'] = $this->attachment->getTitle();
148
149
        if ($this->attachment->getTitle()->hasLink()) {
150
            $attachment['title_link'] = $this->attachment->getTitle()->getLink();
151
        }
152
153
        return $attachment;
154
    }
155
156
    /**
157
     * If set, add the fields to the attachment.
158
     *
159
     * @param array $attachment
160
     *
161
     * @return array
162
     */
163
    private function addFieldsToAttachment(array $attachment)
164
    {
165
        if (!$this->attachment->hasFields()) {
166
            return $attachment;
167
        }
168
169
        $attachment['fields'] = $this->attachment->getFields();
170
171
        return $attachment;
172
    }
173
174
    /**
175
     * If set, add the image to the attachment.
176
     *
177
     * @param array $attachment
178
     *
179
     * @return array
180
     */
181
    private function addImageToAttachment(array $attachment)
182
    {
183
        if (!$this->attachment->hasImage()) {
184
            return $attachment;
185
        }
186
187
        $attachment['image_url'] = $this->attachment->getImage();
188
189
        return $attachment;
190
    }
191
192
    /**
193
     * If set, add the thumbnail to the attachment.
194
     *
195
     * @param array $attachment
196
     *
197
     * @return array
198
     */
199
    private function addThumbnailToAttachment(array $attachment)
200
    {
201
        if (!$this->attachment->hasThumbnail()) {
202
            return $attachment;
203
        }
204
205
        $attachment['thumb_url'] = $this->attachment->getThumbnail();
206
207
        return $attachment;
208
    }
209
210
    /**
211
     * If set, add the fields to the attachment.
212
     *
213
     * @param array $attachment
214
     *
215
     * @return array
216
     */
217
    private function addMarkdownSettingsToAttachment(array $attachment)
218
    {
219
        if (empty($this->attachment->hasMarkdownSettings())) {
220
            return $attachment;
221
        }
222
223
        $attachment['mrkdwn_in'] = $this->attachment->getMarkdownSettings();
224
225
        return $attachment;
226
    }
227
}
228