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

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