Completed
Push — master ( a5b2b0...59ec6e )
by Anton
01:38
created

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