Completed
Pull Request — master (#3)
by
unknown
01:39
created

RocketChatAttachment::imageUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace NotificationChannels\RocketChat;
4
5
use Illuminate\Support\Str;
6
7
/**
8
 * Class RocketChatAttachment.
9
 */
10
class RocketChatAttachment
11
{
12
    /**
13
     * @var string The color you want the order on the left side to be, any value background-css supports.
14
     */
15
    protected $color = '';
16
17
    /**
18
     * @var string The text to display for this attachment, it is different than the message’s text.
19
     */
20
    protected $text = '';
21
    /**
22
     * @var string Displays the time next to the text portion.
23
     */
24
    protected $timestamp = '';
25
    /**
26
     * @var string An image that displays to the left of the text, looks better when this is relatively small.
27
     */
28
    protected $thumbnailUrl = '';
29
    /**
30
     * @var string Only applicable if the ts is provided, as it makes the time clickable to this link.
31
     */
32
    protected $messageLink = '';
33
    /**
34
     * @var bool Causes the image, audio, and video sections to be hiding when collapsed is true.
35
     */
36
    protected $collapsed = false;
37
    /**
38
     * @var string Name of the author.
39
     */
40
    protected $authorName = '';
41
    /**
42
     * @var string Providing this makes the author name clickable and points to this link.
43
     */
44
    protected $authorLink = '';
45
    /**
46
     * @var string Displays a tiny icon to the left of the Author’s name.
47
     */
48
    protected $authorIcon = '';
49
    /**
50
     * @var string Title to display for this attachment, displays under the author.
51
     */
52
    protected $title = '';
53
    /**
54
     * @var string Providing this makes the title clickable, pointing to this link.
55
     */
56
    protected $titleLink = '';
57
    /**
58
     * @var bool When this is true, a download icon appears and clicking this saves the link to file.
59
     */
60
    protected $titleLinkDownload = false;
61
    /**
62
     * @var string The image to display, will be “big” and easy to see.
63
     */
64
    protected $imageUrl = '';
65
    /**
66
     * @var string Audio file to play, only supports what html audio does.
67
     */
68
    protected $audioUrl = '';
69
    /**
70
     * @var string Video file to play, only supports what html video does.
71
     */
72
    protected $videoUrl = '';
73
    /**
74
     * @var array An array of Attachment Field Objects.
75
     */
76
    protected $fields = [];
77
78
    /**
79
     * RocketChatAttachment constructor.
80
     * @param array|null $config
81
     */
82 26
    public function __construct(array $config = null)
83
    {
84 26
        if ($config != null) {
85 5
            $this->setFromArray($config);
86
        }
87 26
    }
88
89
    /**
90
     * Create a new instance of RocketChatAttachment.
91
     *
92
     * @param array|null $config
93
     * @return RocketChatAttachment
94
     */
95 4
    public static function create(array $config = null)
96
    {
97 4
        return new self($config);
98
    }
99
100
    /**
101
     * set attachment data form array.
102
     *
103
     * @param array $data
104
     */
105 5
    protected function setFromArray(array $data)
106
    {
107 5
        foreach ($data as $key => $value) {
108 5
            $method = Str::camel($key);
109 5
            if (! method_exists($this, $method)) {
110 1
                continue;
111
            }
112 4
            $this->{$method}($value);
113
        }
114 5
    }
115
116
    /**
117
     * @param string $color
118
     * @return RocketChatAttachment
119
     */
120 1
    public function color(string $color): self
121
    {
122 1
        $this->color = $color;
123
124 1
        return $this;
125
    }
126
127
    /**
128
     * @param string $text
129
     * @return RocketChatAttachment
130
     */
131 1
    public function text(string $text): self
132
    {
133 1
        $this->text = $text;
134
135 1
        return $this;
136
    }
137
138
    /**
139
     * @param string|\DateTime $timestamp
140
     * @return RocketChatAttachment
141
     */
142 2
    public function timestamp($timestamp): self
143
    {
144 2
        if (! ($timestamp instanceof \DateTime) && ! is_string($timestamp)) {
145
            throw new \InvalidArgumentException('Timestamp must be string or DateTime, '.gettype($timestamp).' given.');
146
        }
147
148 2
        if ($timestamp instanceof \DateTime) {
149 1
            $date = clone $timestamp;
150 1
            $timestamp = $date->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d\TH:i:s.v\Z');
151
        }
152 2
        $this->timestamp = $timestamp;
153
154 2
        return $this;
155
    }
156
157
    /**
158
     * @param string $thumbnailUrl
159
     * @return RocketChatAttachment
160
     */
161 1
    public function thumbnailUrl(string $thumbnailUrl): self
162
    {
163 1
        $this->thumbnailUrl = $thumbnailUrl;
164
165 1
        return $this;
166
    }
167
168
    /**
169
     * @param string $messageLink
170
     * @return RocketChatAttachment
171
     */
172 1
    public function messageLink(string $messageLink): self
173
    {
174 1
        $this->messageLink = $messageLink;
175
176 1
        return $this;
177
    }
178
179
    /**
180
     * @param bool $collapsed
181
     * @return RocketChatAttachment
182
     */
183 1
    public function collapsed(bool $collapsed): self
184
    {
185 1
        $this->collapsed = $collapsed;
186
187 1
        return $this;
188
    }
189
190
    /**
191
     * @param string $name
192
     * @param string $link
193
     * @param string $icon
194
     * @return RocketChatAttachment
195
     */
196 1
    public function author(string $name, string $link = '', string $icon = ''): self
197
    {
198 1
        $this->authorName($name);
199 1
        $this->authorLink($link);
200 1
        $this->authorIcon($icon);
201
202 1
        return $this;
203
    }
204
205
    /**
206
     * @param string $authorName
207
     * @return RocketChatAttachment
208
     */
209 2
    public function authorName(string $authorName): self
210
    {
211 2
        $this->authorName = $authorName;
212
213 2
        return $this;
214
    }
215
216
    /**
217
     * @param string $authorLink
218
     * @return RocketChatAttachment
219
     */
220 2
    public function authorLink(string $authorLink): self
221
    {
222 2
        $this->authorLink = $authorLink;
223
224 2
        return $this;
225
    }
226
227
    /**
228
     * @param string $authorIcon
229
     * @return RocketChatAttachment
230
     */
231 2
    public function authorIcon(string $authorIcon): self
232
    {
233 2
        $this->authorIcon = $authorIcon;
234
235 2
        return $this;
236
    }
237
238
    /**
239
     * @param string $title
240
     * @return RocketChatAttachment
241
     */
242 5
    public function title(string $title): self
243
    {
244 5
        $this->title = $title;
245
246 5
        return $this;
247
    }
248
249
    /**
250
     * @param string $titleLink
251
     * @return RocketChatAttachment
252
     */
253 1
    public function titleLink(string $titleLink): self
254
    {
255 1
        $this->titleLink = $titleLink;
256
257 1
        return $this;
258
    }
259
260
    /**
261
     * @param bool $titleLinkDownload
262
     * @return RocketChatAttachment
263
     */
264 1
    public function titleLinkDownload(bool $titleLinkDownload): self
265
    {
266 1
        $this->titleLinkDownload = $titleLinkDownload;
267
268 1
        return $this;
269
    }
270
271
    /**
272
     * @param string $imageUrl
273
     * @return RocketChatAttachment
274
     */
275 1
    public function imageUrl(string $imageUrl): self
276
    {
277 1
        $this->imageUrl = $imageUrl;
278
279 1
        return $this;
280
    }
281
282
    /**
283
     * @param string $audioUrl
284
     * @return RocketChatAttachment
285
     */
286 1
    public function audioUrl(string $audioUrl): self
287
    {
288 1
        $this->audioUrl = $audioUrl;
289
290 1
        return $this;
291
    }
292
293
    /**
294
     * @param string $videoUrl
295
     * @return RocketChatAttachment
296
     */
297 1
    public function videoUrl(string $videoUrl): self
298
    {
299 1
        $this->videoUrl = $videoUrl;
300
301 1
        return $this;
302
    }
303
304
    /**
305
     * @param array $fields
306
     * @return RocketChatAttachment
307
     */
308 1
    public function fields(array $fields): self
309
    {
310 1
        $this->fields = $fields;
311
312 1
        return $this;
313
    }
314
315
    /**
316
     * Get an array representation of the RocketChatAttachment.
317
     *
318
     * @return array
319
     */
320 22
    public function toArray(): array
321
    {
322 22
        $message = array_filter([
323 22
            'color' => $this->color,
324 22
            'text' => $this->text,
325 22
            'ts' => $this->timestamp,
326 22
            'thumb_url' => $this->thumbnailUrl,
327 22
            'message_link' => $this->messageLink,
328 22
            'collapsed' => $this->collapsed,
329 22
            'author_name' => $this->authorName,
330 22
            'author_link' => $this->authorLink,
331 22
            'author_icon' => $this->authorIcon,
332 22
            'title' => $this->title,
333 22
            'title_link' => $this->titleLink,
334 22
            'title_link_download' => $this->titleLinkDownload,
335 22
            'image_url' => $this->imageUrl,
336 22
            'audio_url' => $this->audioUrl,
337 22
            'video_url' => $this->videoUrl,
338 22
            'fields' => $this->fields,
339
        ]);
340
341 22
        return $message;
342
    }
343
}
344