Completed
Pull Request — master (#9)
by
unknown
01:20
created

RocketChatAttachment::timestamp()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
rs 9.2568
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5.0144
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 make(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
                $invalidType
118
            ));
119
        }
120
121 3
        if ($timestamp instanceof DateTimeInterface) {
122 2
            $date = clone $timestamp;
123 2
            $timestamp = $date->format(DateTimeInterface::ATOM);
124
        }
125
126 3
        $this->timestamp = $timestamp;
127
128 3
        return $this;
129
    }
130
131
    /**
132
     * @param string $thumbnailUrl
133
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
134
     */
135 1
    public function thumbnailUrl(string $thumbnailUrl): self
136
    {
137 1
        $this->thumbnailUrl = $thumbnailUrl;
138
139 1
        return $this;
140
    }
141
142
    /**
143
     * @param string $messageLink
144
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
145
     */
146 1
    public function messageLink(string $messageLink): self
147
    {
148 1
        $this->messageLink = $messageLink;
149
150 1
        return $this;
151
    }
152
153
    /**
154
     * @param bool $collapsed
155
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
156
     */
157 1
    public function collapsed(bool $collapsed): self
158
    {
159 1
        $this->collapsed = $collapsed;
160
161 1
        return $this;
162
    }
163
164
    /**
165
     * @param string $name
166
     * @param string $link
167
     * @param string $icon
168
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
169
     */
170 1
    public function author(string $name, string $link = '', string $icon = ''): self
171
    {
172 1
        $this->authorName($name);
173 1
        $this->authorLink($link);
174 1
        $this->authorIcon($icon);
175
176 1
        return $this;
177
    }
178
179
    /**
180
     * @param string $authorName
181
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
182
     */
183 2
    public function authorName(string $authorName): self
184
    {
185 2
        $this->authorName = $authorName;
186
187 2
        return $this;
188
    }
189
190
    /**
191
     * @param string $authorLink
192
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
193
     */
194 2
    public function authorLink(string $authorLink): self
195
    {
196 2
        $this->authorLink = $authorLink;
197
198 2
        return $this;
199
    }
200
201
    /**
202
     * @param string $authorIcon
203
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
204
     */
205 2
    public function authorIcon(string $authorIcon): self
206
    {
207 2
        $this->authorIcon = $authorIcon;
208
209 2
        return $this;
210
    }
211
212
    /**
213
     * @param string $title
214
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
215
     */
216 5
    public function title(string $title): self
217
    {
218 5
        $this->title = $title;
219
220 5
        return $this;
221
    }
222
223
    /**
224
     * @param string $titleLink
225
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
226
     */
227 1
    public function titleLink(string $titleLink): self
228
    {
229 1
        $this->titleLink = $titleLink;
230
231 1
        return $this;
232
    }
233
234
    /**
235
     * @param bool $titleLinkDownload
236
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
237
     */
238 1
    public function titleLinkDownload(bool $titleLinkDownload): self
239
    {
240 1
        $this->titleLinkDownload = $titleLinkDownload;
241
242 1
        return $this;
243
    }
244
245
    /**
246
     * @param string $imageUrl
247
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
248
     */
249 1
    public function imageUrl(string $imageUrl): self
250
    {
251 1
        $this->imageUrl = $imageUrl;
252
253 1
        return $this;
254
    }
255
256
    /**
257
     * @param string $audioUrl
258
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
259
     */
260 1
    public function audioUrl(string $audioUrl): self
261
    {
262 1
        $this->audioUrl = $audioUrl;
263
264 1
        return $this;
265
    }
266
267
    /**
268
     * @param string $videoUrl
269
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
270
     */
271 1
    public function videoUrl(string $videoUrl): self
272
    {
273 1
        $this->videoUrl = $videoUrl;
274
275 1
        return $this;
276
    }
277
278
    /**
279
     * @param array $fields
280
     * @return \NotificationChannels\RocketChat\RocketChatAttachment
281
     */
282 1
    public function fields(array $fields): self
283
    {
284 1
        $this->fields = $fields;
285
286 1
        return $this;
287
    }
288
289
    /**
290
     * Get an array representation of the RocketChatAttachment.
291
     *
292
     * @return array
293
     */
294 26
    public function toArray(): array
295
    {
296 26
        return array_filter([
297 26
            'color' => $this->color,
298 26
            'text' => $this->text,
299 26
            'ts' => $this->timestamp,
300 26
            'thumb_url' => $this->thumbnailUrl,
301 26
            'message_link' => $this->messageLink,
302 26
            'collapsed' => $this->collapsed,
303 26
            'author_name' => $this->authorName,
304 26
            'author_link' => $this->authorLink,
305 26
            'author_icon' => $this->authorIcon,
306 26
            'title' => $this->title,
307 26
            'title_link' => $this->titleLink,
308 26
            'title_link_download' => $this->titleLinkDownload,
309 26
            'image_url' => $this->imageUrl,
310 26
            'audio_url' => $this->audioUrl,
311 26
            'video_url' => $this->videoUrl,
312 26
            'fields' => $this->fields,
313
        ]);
314
    }
315
316
    /**
317
     * Set attachment data from array.
318
     *
319
     * @param array $data
320
     * @return void
321
     */
322 27
    private function setPropertiesFromArray(array $data): void
323
    {
324 27
        foreach ($data as $key => $value) {
325 5
            $methodName = Str::camel($key);
326
327 5
            if (! method_exists($this, $methodName)) {
328 1
                continue;
329
            }
330
331 4
            $this->{$methodName}($value);
332
        }
333 27
    }
334
}
335