Completed
Push — master ( 3baf41...27da68 )
by Anton
01:35
created

RocketChatAttachment::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
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|null The color you want the order on the left side to be, any value background-css supports. */
15
    protected $color;
16
17
    /** @var string|null The text to display for this attachment, it is different than the message’s text. */
18
    protected $text;
19
20
    /** @var string|null Displays the time next to the text portion. */
21
    protected $timestamp;
22
23
    /** @var string|null An image that displays to the left of the text, looks better when this is relatively small. */
24
    protected $thumbnailUrl;
25
26
    /** @var string|null 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|null Name of the author. */
33
    protected $authorName;
34
35
    /** @var string|null Providing this makes the author name clickable and points to this link. */
36
    protected $authorLink;
37
38
    /** @var string|null Displays a tiny icon to the left of the Author’s name. */
39
    protected $authorIcon;
40
41
    /** @var string|null Title to display for this attachment, displays under the author. */
42
    protected $title;
43
44
    /** @var string|null 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|null The image to display, will be “big” and easy to see. */
51
    protected $imageUrl;
52
53
    /** @var string|null Audio file to play, only supports what html audio does. */
54
    protected $audioUrl;
55
56
    /** @var string|null 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 \NotificationChannels\RocketChat\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 \NotificationChannels\RocketChat\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
120 2
        $this->timestamp = $timestamp;
121
122 2
        return $this;
123
    }
124
125
    /**
126
     * @param string $thumbnailUrl
127
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
128
     */
129 1
    public function thumbnailUrl(string $thumbnailUrl): self
130
    {
131 1
        $this->thumbnailUrl = $thumbnailUrl;
132
133 1
        return $this;
134
    }
135
136
    /**
137
     * @param string $messageLink
138
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
139
     */
140 1
    public function messageLink(string $messageLink): self
141
    {
142 1
        $this->messageLink = $messageLink;
143
144 1
        return $this;
145
    }
146
147
    /**
148
     * @param bool $collapsed
149
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
150
     */
151 1
    public function collapsed(bool $collapsed): self
152
    {
153 1
        $this->collapsed = $collapsed;
154
155 1
        return $this;
156
    }
157
158
    /**
159
     * @param string $name
160
     * @param string $link
161
     * @param string $icon
162
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
163
     */
164 1
    public function author(string $name, string $link = '', string $icon = ''): self
165
    {
166 1
        $this->authorName($name);
167 1
        $this->authorLink($link);
168 1
        $this->authorIcon($icon);
169
170 1
        return $this;
171
    }
172
173
    /**
174
     * @param string $authorName
175
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
176
     */
177 2
    public function authorName(string $authorName): self
178
    {
179 2
        $this->authorName = $authorName;
180
181 2
        return $this;
182
    }
183
184
    /**
185
     * @param string $authorLink
186
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
187
     */
188 2
    public function authorLink(string $authorLink): self
189
    {
190 2
        $this->authorLink = $authorLink;
191
192 2
        return $this;
193
    }
194
195
    /**
196
     * @param string $authorIcon
197
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
198
     */
199 2
    public function authorIcon(string $authorIcon): self
200
    {
201 2
        $this->authorIcon = $authorIcon;
202
203 2
        return $this;
204
    }
205
206
    /**
207
     * @param string $title
208
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
209
     */
210 5
    public function title(string $title): self
211
    {
212 5
        $this->title = $title;
213
214 5
        return $this;
215
    }
216
217
    /**
218
     * @param string $titleLink
219
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
220
     */
221 1
    public function titleLink(string $titleLink): self
222
    {
223 1
        $this->titleLink = $titleLink;
224
225 1
        return $this;
226
    }
227
228
    /**
229
     * @param bool $titleLinkDownload
230
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
231
     */
232 1
    public function titleLinkDownload(bool $titleLinkDownload): self
233
    {
234 1
        $this->titleLinkDownload = $titleLinkDownload;
235
236 1
        return $this;
237
    }
238
239
    /**
240
     * @param string $imageUrl
241
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
242
     */
243 1
    public function imageUrl(string $imageUrl): self
244
    {
245 1
        $this->imageUrl = $imageUrl;
246
247 1
        return $this;
248
    }
249
250
    /**
251
     * @param string $audioUrl
252
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
253
     */
254 1
    public function audioUrl(string $audioUrl): self
255
    {
256 1
        $this->audioUrl = $audioUrl;
257
258 1
        return $this;
259
    }
260
261
    /**
262
     * @param string $videoUrl
263
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
264
     */
265 1
    public function videoUrl(string $videoUrl): self
266
    {
267 1
        $this->videoUrl = $videoUrl;
268
269 1
        return $this;
270
    }
271
272
    /**
273
     * @param array $fields
274
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
275
     */
276 1
    public function fields(array $fields): self
277
    {
278 1
        $this->fields = $fields;
279
280 1
        return $this;
281
    }
282
283
    /**
284
     * Get an array representation of the RocketChatAttachment.
285
     *
286
     * @return array
287
     */
288 25
    public function toArray(): array
289
    {
290 25
        return array_filter([
291 25
            'color' => $this->color,
292 25
            'text' => $this->text,
293 25
            'ts' => $this->timestamp,
294 25
            'thumb_url' => $this->thumbnailUrl,
295 25
            'message_link' => $this->messageLink,
296 25
            'collapsed' => $this->collapsed,
297 25
            'author_name' => $this->authorName,
298 25
            'author_link' => $this->authorLink,
299 25
            'author_icon' => $this->authorIcon,
300 25
            'title' => $this->title,
301 25
            'title_link' => $this->titleLink,
302 25
            'title_link_download' => $this->titleLinkDownload,
303 25
            'image_url' => $this->imageUrl,
304 25
            'audio_url' => $this->audioUrl,
305 25
            'video_url' => $this->videoUrl,
306 25
            'fields' => $this->fields,
307
        ]);
308
    }
309
310
    /**
311
     * Set attachment data from array.
312
     *
313
     * @param array $data
314
     * @return void
315
     */
316 26
    private function setPropertiesFromArray(array $data): void
317
    {
318 26
        foreach ($data as $key => $value) {
319 5
            $methodName = Str::camel($key);
320
321 5
            if (! method_exists($this, $methodName)) {
322 1
                continue;
323
            }
324
325 4
            $this->{$methodName}($value);
326
        }
327 26
    }
328
}
329