GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d6c57b...8da903 )
by Freek
01:22
created

Attachment::toArray()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 2
nop 0
1
<?php
2
3
namespace Spatie\SlashCommand;
4
5
use DateTime;
6
use Illuminate\Support\Collection;
7
use Spatie\SlashCommand\Exceptions\ActionCannotBeAdded;
8
use Spatie\SlashCommand\Exceptions\FieldCannotBeAdded;
9
10
class Attachment
11
{
12
    const COLOR_GOOD = 'good';
13
    const COLOR_WARNING = 'warning';
14
    const COLOR_DANGER = 'danger';
15
16
    /**
17
     * The fallback text to use for clients that don't support attachments.
18
     *
19
     * @var string
20
     */
21
    protected $fallback;
22
23
    /**
24
     * Optional text that should appear within the attachment.
25
     *
26
     * @var string
27
     */
28
    protected $text = '';
29
30
    /**
31
     * Optional image that should appear within the attachment.
32
     *
33
     * @var string
34
     */
35
    protected $imageUrl;
36
37
    /**
38
     * Optional thumbnail that should appear within the attachment.
39
     *
40
     * @var string
41
     */
42
    protected $thumbUrl;
43
44
    /**
45
     * Optional text that should appear above the formatted data.
46
     *
47
     * @var string
48
     */
49
    protected $preText;
50
51
    /**
52
     * Optional title for the attachment.
53
     *
54
     * @var string
55
     */
56
    protected $title;
57
58
    /**
59
     * Optional title link for the attachment.
60
     *
61
     * @var string
62
     */
63
    protected $titleLink;
64
65
    /**
66
     * Optional author name for the attachment.
67
     *
68
     * @var string
69
     */
70
    protected $authorName;
71
72
    /**
73
     * Optional author link for the attachment.
74
     *
75
     * @var string
76
     */
77
    protected $authorLink;
78
79
    /**
80
     * Optional author icon for the attachment.
81
     *
82
     * @var string
83
     */
84
    protected $authorIcon;
85
86
    /**
87
     * The color to use for the attachment.
88
     *
89
     * @var string
90
     */
91
    protected $color;
92
93
    /**
94
     * The text to use for the attachment footer.
95
     *
96
     * @var string
97
     */
98
    protected $footer;
99
100
    /**
101
     * The icon to use for the attachment footer.
102
     *
103
     * @var string
104
     */
105
    protected $footerIcon;
106
107
    /**
108
     * The timestamp to use for the attachment.
109
     *
110
     * @var \DateTime
111
     */
112
    protected $timestamp;
113
114
    /**
115
     * The fields of the attachment.
116
     *
117
     * @var Collection
118
     */
119
    protected $fields;
120
121
    /**
122
     * The actions of the attachment.
123
     *
124
     * @var Collection
125
     */
126
    protected $actions;
127
128
    public static function create()
129
    {
130
        return new static();
131
    }
132
133
    public function __construct()
134
    {
135
        $this->fields  = new Collection();
136
        $this->actions = new Collection();
137
    }
138
139
    /**
140
     * Set the fallback text.
141
     *
142
     * @param string $fallback
143
     *
144
     * @return $this
145
     */
146
    public function setFallback(string $fallback)
147
    {
148
        $this->fallback = $fallback;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Set the optional text to appear within the attachment.
155
     *
156
     * @param string $text
157
     *
158
     * @return $this
159
     */
160
    public function setText($text)
161
    {
162
        $this->text = $text;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Set the optional image to appear within the attachment.
169
     *
170
     * @param string $imageUrl
171
     *
172
     * @return $this
173
     */
174
    public function setImageUrl(string $imageUrl)
175
    {
176
        $this->imageUrl = $imageUrl;
177
178
        return $this;
179
    }
180
181
    /**
182
     * Set the optional thumbnail to appear within the attachment.
183
     *
184
     * @param string $thumbUrl
185
     *
186
     * @return $this
187
     */
188
    public function setThumbUrl(string $thumbUrl)
189
    {
190
        $this->thumbUrl = $thumbUrl;
191
192
        return $this;
193
    }
194
195
    /**
196
     * Set the text that should appear above the formatted data.
197
     *
198
     * @param string $preText
199
     *
200
     * @return $this
201
     */
202
    public function setPreText(string $preText)
203
    {
204
        $this->preText = $preText;
205
206
        return $this;
207
    }
208
209
    /**
210
     * Set the color to use for the attachment.
211
     *
212
     * @param string $color
213
     *
214
     * @return $this
215
     */
216
    public function setColor($color)
217
    {
218
        $this->color = $color;
219
220
        return $this;
221
    }
222
223
    /**
224
     * Set the footer text to use for the attachment.
225
     *
226
     * @param string $footer
227
     *
228
     * @return $this
229
     */
230
    public function setFooter(string $footer)
231
    {
232
        $this->footer = $footer;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Set the footer icon to use for the attachment.
239
     *
240
     * @param string $footerIcon
241
     *
242
     * @return $this
243
     */
244
    public function setFooterIcon(string $footerIcon)
245
    {
246
        $this->footerIcon = $footerIcon;
247
248
        return $this;
249
    }
250
251
    /**
252
     * Set the timestamp to use for the attachment.
253
     *
254
     * @param \DateTime $timestamp
255
     *
256
     * @return $this
257
     */
258
    public function setTimestamp(DateTime $timestamp)
259
    {
260
        $this->timestamp = $timestamp;
261
262
        return $this;
263
    }
264
265
    /**
266
     * Set the title to use for the attachment.
267
     *
268
     * @param string $title
269
     *
270
     * @return $this
271
     */
272
    public function setTitle(string $title)
273
    {
274
        $this->title = $title;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Set the title link to use for the attachment.
281
     *
282
     * @param string $titleLink
283
     *
284
     * @return $this
285
     */
286
    public function setTitleLink(string $titleLink)
287
    {
288
        $this->titleLink = $titleLink;
289
290
        return $this;
291
    }
292
293
    /**
294
     * Set the author name to use for the attachment.
295
     *
296
     * @param string $authorName
297
     *
298
     * @return $this
299
     */
300
    public function setAuthorName(string $authorName)
301
    {
302
        $this->authorName = $authorName;
303
304
        return $this;
305
    }
306
307
    /**
308
     * Set the auhtor link to use for the attachment.
309
     *
310
     * @param string $authorLink
311
     *
312
     * @return $this
313
     */
314
    public function setAuthorLink(string $authorLink)
315
    {
316
        $this->authorLink = $authorLink;
317
318
        return $this;
319
    }
320
321
    /**
322
     * Set the author icon to use for the attachment.
323
     *
324
     * @param string $authorIcon
325
     *
326
     * @return $this
327
     */
328
    public function setAuthorIcon(string $authorIcon)
329
    {
330
        $this->authorIcon = $authorIcon;
331
332
        return $this;
333
    }
334
335
    /**
336
     * Add a field to the attachment.
337
     *
338
     * @param \Spatie\SlashCommand\AttachmentField|array $field
339
     *
340
     * @return $this
341
     *
342
     * @throws \Spatie\SlashCommand\Exceptions\FieldCannotBeAdded
343
     */
344
    public function addField($field)
345
    {
346
        if (!is_array($field) && !$field instanceof AttachmentField) {
347
            throw FieldCannotBeAdded::invalidType();
348
        }
349
350
        if (is_array($field)) {
351
            $title = array_keys($field)[0];
352
            $value = $field[$title];
353
354
            $field = AttachmentField::create($title, $value);
355
        }
356
357
        $this->fields->push($field);
358
359
        return $this;
360
    }
361
362
    /**
363
     * Add the fields for the attachment.
364
     *
365
     * @param array $fields
366
     *
367
     * @return $this
368
     */
369
    public function addFields(array $fields)
370
    {
371
        collect($fields)->each(function ($field, $key) {
372
373
            if (!$field instanceof AttachmentField) {
374
                $field = [$key => $field];
375
            }
376
377
            $this->addField($field);
378
        });
379
380
        return $this;
381
    }
382
383
    /**
384
     * Set the fields for the attachment.
385
     *
386
     * @param array $fields
387
     *
388
     * @return $this
389
     */
390
    public function setFields(array $fields)
391
    {
392
        $this->clearFields();
393
394
        $this->addFields($fields);
395
396
        return $this;
397
    }
398
399
    /**
400
     * Clear all fields for this attachment.
401
     *
402
     * @return $this
403
     */
404
    public function clearFields()
405
    {
406
        $this->fields = new Collection();
407
408
        return $this;
409
    }
410
411
    /**
412
     * @param \Spatie\SlashCommand\AttachmentAction|array $action
413
     *
414
     * @return $this
415
     * @throws \Spatie\SlashCommand\Exceptions\ActionCannotBeAdded
416
     */
417
    public function addAction($action)
418
    {
419
        if (!is_array($action) && !$action instanceof AttachmentAction) {
420
            throw ActionCannotBeAdded::invalidType();
421
        }
422
423
        if (is_array($action)) {
424
            $name = $action['name'];
425
            $text = $action['text'];
426
            $type = $action['type'];
427
428
            $action = AttachmentAction::create($name, $text, $type);
429
        }
430
431
        $this->actions->push($action);
432
433
        return $this;
434
    }
435
436
    /**
437
     * Add the actions for the attachment.
438
     *
439
     * @param array $actions
440
     *
441
     * @return $this
442
     */
443
    public function addActions(array $actions)
444
    {
445
        collect($actions)->each(function ($action) {
446
            $this->addAction($action);
447
        });
448
449
        return $this;
450
    }
451
452
    /**
453
     * Set the actions for the attachment.
454
     *
455
     * @param array $actions
456
     *
457
     * @return $this
458
     */
459
    public function setActions(array $actions)
460
    {
461
        $this->clearActions();
462
463
        $this->addActions($actions);
464
465
        return $this;
466
    }
467
468
    /**
469
     * Clear all actions for this attachment.
470
     *
471
     * @return $this
472
     */
473
    public function clearActions()
474
    {
475
        $this->actions = new Collection();
476
477
        return $this;
478
    }
479
480
    /**
481
     * Convert this attachment to its array representation.
482
     *
483
     * @return array
484
     */
485
    public function toArray()
486
    {
487
        return [
488
            'fallback'    => $this->fallback,
489
            'text'        => $this->text,
490
            'pretext'     => $this->preText,
491
            'color'       => $this->color,
492
            'footer'      => $this->footer,
493
            'footer_icon' => $this->footer,
494
            'ts'          => $this->timestamp ? $this->timestamp->getTimestamp() : null,
495
            'image_url'   => $this->imageUrl,
496
            'thumb_url'   => $this->thumbUrl,
497
            'title'       => $this->title,
498
            'title_link'  => $this->titleLink,
499
            'author_name' => $this->authorName,
500
            'author_link' => $this->authorLink,
501
            'author_icon' => $this->authorIcon,
502
            'fields'      => $this->fields->map(function (AttachmentField $field) {
503
                return $field->toArray();
504
            })->toArray(),
505
            'actions'     => $this->actions->map(function (AttachmentAction $action) {
506
                return $action->toArray();
507
            })->toArray(),
508
        ];
509
    }
510
}
511