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
Pull Request — master (#63)
by
unknown
21:40
created

Attachment::setMarkdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Spatie\SlashCommand;
4
5
use DateTime;
6
use Illuminate\Support\Collection;
7
use Spatie\SlashCommand\Exceptions\FieldCannotBeAdded;
8
use Spatie\SlashCommand\Exceptions\ActionCannotBeAdded;
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 callback id to use for the attachment.
116
     *
117
     * @var string
118
     */
119
    protected $callbackId;
120
121
    /**
122
     * The fields of the attachment.
123
     *
124
     * @var \Illuminate\Support\Collection
125
     */
126
    protected $fields;
127
128
    /**
129
     * Message Formatting.
130
     *
131
     * @var array $mrkdwn
132
     */
133
    protected $mrkdwn = [];
134
135
    /**
136
     * The actions of the attachment.
137
     *
138
     * @var \Illuminate\Support\Collection
139
     */
140
    protected $actions;
141
142
    public static function create()
143
    {
144
        return new static();
145
    }
146
147
    public function __construct()
148
    {
149
        $this->fields = new Collection();
150
151
        $this->actions = new Collection();
152
    }
153
154
    /**
155
     * Set the fallback text.
156
     *
157
     * @param string $fallback
158
     *
159
     * @return $this
160
     */
161
    public function setFallback(string $fallback)
162
    {
163
        $this->fallback = $fallback;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Set the optional text to appear within the attachment.
170
     *
171
     * @param string $text
172
     *
173
     * @return $this
174
     */
175
    public function setText($text)
176
    {
177
        $this->text = $text;
178
179
        return $this;
180
    }
181
182
    /**
183
     * Set the optional image to appear within the attachment.
184
     *
185
     * @param string $imageUrl
186
     *
187
     * @return $this
188
     */
189
    public function setImageUrl(string $imageUrl)
190
    {
191
        $this->imageUrl = $imageUrl;
192
193
        return $this;
194
    }
195
196
    /**
197
     * Set the optional thumbnail to appear within the attachment.
198
     *
199
     * @param string $thumbUrl
200
     *
201
     * @return $this
202
     */
203
    public function setThumbUrl(string $thumbUrl)
204
    {
205
        $this->thumbUrl = $thumbUrl;
206
207
        return $this;
208
    }
209
210
    /**
211
     * Set the text that should appear above the formatted data.
212
     *
213
     * @param string $preText
214
     *
215
     * @return $this
216
     */
217
    public function setPreText(string $preText)
218
    {
219
        $this->preText = $preText;
220
221
        return $this;
222
    }
223
224
    /**
225
     * Set the color to use for the attachment.
226
     *
227
     * @param string $color
228
     *
229
     * @return $this
230
     */
231
    public function setColor($color)
232
    {
233
        $this->color = $color;
234
235
        return $this;
236
    }
237
238
    /**
239
     * Set the footer text to use for the attachment.
240
     *
241
     * @param string $footer
242
     *
243
     * @return $this
244
     */
245
    public function setFooter(string $footer)
246
    {
247
        $this->footer = $footer;
248
249
        return $this;
250
    }
251
252
    /**
253
     * Set the footer icon to use for the attachment.
254
     *
255
     * @param string $footerIcon
256
     *
257
     * @return $this
258
     */
259
    public function setFooterIcon(string $footerIcon)
260
    {
261
        $this->footerIcon = $footerIcon;
262
263
        return $this;
264
    }
265
266
    /**
267
     * Set the timestamp to use for the attachment.
268
     *
269
     * @param \DateTime $timestamp
270
     *
271
     * @return $this
272
     */
273
    public function setTimestamp(DateTime $timestamp)
274
    {
275
        $this->timestamp = $timestamp;
276
277
        return $this;
278
    }
279
280
    /**
281
     * Set the title to use for the attachment.
282
     *
283
     * @param string $title
284
     *
285
     * @return $this
286
     */
287
    public function setTitle(string $title)
288
    {
289
        $this->title = $title;
290
291
        return $this;
292
    }
293
294
    /**
295
     * Set the title link to use for the attachment.
296
     *
297
     * @param string $titleLink
298
     *
299
     * @return $this
300
     */
301
    public function setTitleLink(string $titleLink)
302
    {
303
        $this->titleLink = $titleLink;
304
305
        return $this;
306
    }
307
308
    /**
309
     * Set the author name to use for the attachment.
310
     *
311
     * @param string $authorName
312
     *
313
     * @return $this
314
     */
315
    public function setAuthorName(string $authorName)
316
    {
317
        $this->authorName = $authorName;
318
319
        return $this;
320
    }
321
322
    /**
323
     * Enable simple markup language.
324
     *
325
     * @param boolean $mrkdwn
0 ignored issues
show
Documentation introduced by
There is no parameter named $mrkdwn. Did you maybe mean $setMrkdwn?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
326
     *
327
     * @return $this
328
     */
329
    public function setMarkdown(bool $setMrkdwn)
330
    {
331
        if ($setMrkdwn) {
332
            $this->mrkdwn = ["text", "pretext", "footer"];
333
        }
334
335
        return $this;
336
    }
337
338
    /**
339
     * Set the auhtor link to use for the attachment.
340
     *
341
     * @param string $authorLink
342
     *
343
     * @return $this
344
     */
345
    public function setAuthorLink(string $authorLink)
346
    {
347
        $this->authorLink = $authorLink;
348
349
        return $this;
350
    }
351
352
    /**
353
     * Set the author icon to use for the attachment.
354
     *
355
     * @param string $authorIcon
356
     *
357
     * @return $this
358
     */
359
    public function setAuthorIcon(string $authorIcon)
360
    {
361
        $this->authorIcon = $authorIcon;
362
363
        return $this;
364
    }
365
366
    /**
367
     * Set the callback id to use for the attachment.
368
     *
369
     * @param string $callbackId
370
     *
371
     * @return $this
372
     */
373
    public function setCallbackId(string $callbackId)
374
    {
375
        $this->callbackId = $callbackId;
376
377
        return $this;
378
    }
379
380
    /**
381
     * Add a field to the attachment.
382
     *
383
     * @param \Spatie\SlashCommand\AttachmentField|array $field
384
     *
385
     * @return $this
386
     *
387
     * @throws \Spatie\SlashCommand\Exceptions\FieldCannotBeAdded
388
     */
389
    public function addField($field)
390
    {
391
        if (! is_array($field) && ! $field instanceof AttachmentField) {
392
            throw FieldCannotBeAdded::invalidType();
393
        }
394
395
        if (is_array($field)) {
396
            $title = array_keys($field)[0];
397
            $value = $field[$title];
398
399
            $field = AttachmentField::create($title, $value);
400
        }
401
402
        $this->fields->push($field);
403
404
        return $this;
405
    }
406
407
    /**
408
     * Add the fields for the attachment.
409
     *
410
     * @param array $fields
411
     *
412
     * @return $this
413
     */
414
    public function addFields(array $fields)
415
    {
416
        collect($fields)->each(function ($field, $key) {
417
            if (! $field instanceof AttachmentField) {
418
                $field = [$key => $field];
419
            }
420
421
            $this->addField($field);
422
        });
423
424
        return $this;
425
    }
426
427
    /**
428
     * Set the fields for the attachment.
429
     *
430
     * @param array $fields
431
     *
432
     * @return $this
433
     */
434
    public function setFields(array $fields)
435
    {
436
        $this->clearFields();
437
438
        $this->addFields($fields);
439
440
        return $this;
441
    }
442
443
    /**
444
     * Clear all fields for this attachment.
445
     *
446
     * @return $this
447
     */
448
    public function clearFields()
449
    {
450
        $this->fields = new Collection();
451
452
        return $this;
453
    }
454
455
    /**
456
     * @param \Spatie\SlashCommand\AttachmentAction|array $action
457
     *
458
     * @return $this
459
     * @throws \Spatie\SlashCommand\Exceptions\ActionCannotBeAdded
460
     */
461
    public function addAction($action)
462
    {
463
        if (! is_array($action) && ! $action instanceof AttachmentAction) {
464
            throw ActionCannotBeAdded::invalidType();
465
        }
466
467
        if (is_array($action)) {
468
            $name = $action['name'];
469
            $text = $action['text'];
470
            $type = $action['type'];
471
472
            $action = AttachmentAction::create($name, $text, $type);
473
        }
474
475
        $this->actions->push($action);
476
477
        return $this;
478
    }
479
480
    /**
481
     * Add the actions for the attachment.
482
     *
483
     * @param array $actions
484
     *
485
     * @return $this
486
     */
487
    public function addActions(array $actions)
488
    {
489
        collect($actions)->each(function ($action) {
490
            $this->addAction($action);
491
        });
492
493
        return $this;
494
    }
495
496
    /**
497
     * Set the actions for the attachment.
498
     *
499
     * @param array $actions
500
     *
501
     * @return $this
502
     */
503
    public function setActions(array $actions)
504
    {
505
        $this->clearActions();
506
507
        $this->addActions($actions);
508
509
        return $this;
510
    }
511
512
    /**
513
     * Clear all actions for this attachment.
514
     *
515
     * @return $this
516
     */
517
    public function clearActions()
518
    {
519
        $this->actions = new Collection();
520
521
        return $this;
522
    }
523
524
    /**
525
     * Convert this attachment to its array representation.
526
     *
527
     * @return array
528
     */
529
    public function toArray()
530
    {
531
        return [
532
            'fallback'    => $this->fallback,
533
            'text'        => $this->text,
534
            'pretext'     => $this->preText,
535
            'color'       => $this->color,
536
            'footer'      => $this->footer,
537
            'mrkdwn_in'   => $this->mrkdwn,
538
            'footer_icon' => $this->footer,
539
            'ts'          => $this->timestamp ? $this->timestamp->getTimestamp() : null,
540
            'image_url'   => $this->imageUrl,
541
            'thumb_url'   => $this->thumbUrl,
542
            'title'       => $this->title,
543
            'title_link'  => $this->titleLink,
544
            'author_name' => $this->authorName,
545
            'author_link' => $this->authorLink,
546
            'author_icon' => $this->authorIcon,
547
            'callback_id' => $this->callbackId,
548
            'fields'      => $this->fields->map(function (AttachmentField $field) {
549
                return $field->toArray();
550
            })->toArray(),
551
            'actions'     => $this->actions->map(function (AttachmentAction $action) {
552
                return $action->toArray();
553
            })->toArray(),
554
        ];
555
    }
556
}
557