Completed
Pull Request — master (#7)
by Anton
01:33
created

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