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 (#35)
by Eduardo
01:58
created

Attachment   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 393
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 3
dl 0
loc 393
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setPreText() 0 6 1
A setFooter() 0 6 1
A setTitle() 0 6 1
A setAuthorName() 0 6 1
A create() 0 4 1
A setFallback() 0 6 1
A setText() 0 6 1
A setImageUrl() 0 6 1
A setThumbUrl() 0 6 1
A setColor() 0 6 1
A setFooterIcon() 0 6 1
A setTimestamp() 0 6 1
A setTitleLink() 0 6 1
A setAuthorLink() 0 6 1
A setAuthorIcon() 0 6 1
C addField() 0 25 8
A clearFields() 0 6 1
A toArray() 0 22 2
1
<?php
2
3
namespace Spatie\SlashCommand;
4
5
use DateTime;
6
use Illuminate\Support\Collection;
7
use Spatie\SlashCommand\Exceptions\FieldCannotBeAdded;
8
9
class Attachment
10
{
11
    const COLOR_GOOD = 'good';
12
    const COLOR_WARNING = 'warning';
13
    const COLOR_DANGER = 'danger';
14
15
    /**
16
     * The fallback text to use for clients that don't support attachments.
17
     *
18
     * @var string
19
     */
20
    protected $fallback;
21
22
    /**
23
     * Optional text that should appear within the attachment.
24
     *
25
     * @var string
26
     */
27
    protected $text = '';
28
29
    /**
30
     * Optional image that should appear within the attachment.
31
     *
32
     * @var string
33
     */
34
    protected $imageUrl;
35
36
    /**
37
     * Optional thumbnail that should appear within the attachment.
38
     *
39
     * @var string
40
     */
41
    protected $thumbUrl;
42
43
    /**
44
     * Optional text that should appear above the formatted data.
45
     *
46
     * @var string
47
     */
48
    protected $preText;
49
50
    /**
51
     * Optional title for the attachment.
52
     *
53
     * @var string
54
     */
55
    protected $title;
56
57
    /**
58
     * Optional title link for the attachment.
59
     *
60
     * @var string
61
     */
62
    protected $titleLink;
63
64
    /**
65
     * Optional author name for the attachment.
66
     *
67
     * @var string
68
     */
69
    protected $authorName;
70
71
    /**
72
     * Optional author link for the attachment.
73
     *
74
     * @var string
75
     */
76
    protected $authorLink;
77
78
    /**
79
     * Optional author icon for the attachment.
80
     *
81
     * @var string
82
     */
83
    protected $authorIcon;
84
85
    /**
86
     * The color to use for the attachment.
87
     *
88
     * @var string
89
     */
90
    protected $color;
91
92
    /**
93
     * The text to use for the attachment footer.
94
     *
95
     * @var string
96
     */
97
    protected $footer;
98
99
    /**
100
     * The icon to use for the attachment footer.
101
     *
102
     * @var string
103
     */
104
    protected $footerIcon;
105
106
    /**
107
     * The timestamp to use for the attachment.
108
     *
109
     * @var \DateTime
110
     */
111
    protected $timestamp;
112
113
    /**
114
     * The fields of the attachment.
115
     *
116
     * @var Collection
117
     */
118
    protected $fields;
119
120
    public static function create()
121
    {
122
        return new static();
123
    }
124
125
    public function __construct()
126
    {
127
        $this->fields = new Collection();
128
    }
129
130
    /**
131
     * Set the fallback text.
132
     *
133
     * @param string $fallback
134
     *
135
     * @return $this
136
     */
137
    public function setFallback(string $fallback)
138
    {
139
        $this->fallback = $fallback;
140
141
        return $this;
142
    }
143
144
    /**
145
     * Set the optional text to appear within the attachment.
146
     *
147
     * @param string $text
148
     *
149
     * @return $this
150
     */
151
    public function setText($text)
152
    {
153
        $this->text = $text;
154
155
        return $this;
156
    }
157
158
    /**
159
     * Set the optional image to appear within the attachment.
160
     *
161
     * @param string $imageUrl
162
     *
163
     * @return $this
164
     */
165
    public function setImageUrl(string $imageUrl)
166
    {
167
        $this->imageUrl = $imageUrl;
168
169
        return $this;
170
    }
171
172
    /**
173
     * Set the optional thumbnail to appear within the attachment.
174
     *
175
     * @param string $thumbUrl
176
     *
177
     * @return $this
178
     */
179
    public function setThumbUrl(string $thumbUrl)
180
    {
181
        $this->thumbUrl = $thumbUrl;
182
183
        return $this;
184
    }
185
186
    /**
187
     * Set the text that should appear above the formatted data.
188
     *
189
     * @param string $preText
190
     *
191
     * @return $this
192
     */
193
    public function setPreText(string $preText)
194
    {
195
        $this->preText = $preText;
196
197
        return $this;
198
    }
199
200
    /**
201
     * Set the color to use for the attachment.
202
     *
203
     * @param string $color
204
     *
205
     * @return $this
206
     */
207
    public function setColor($color)
208
    {
209
        $this->color = $color;
210
211
        return $this;
212
    }
213
214
    /**
215
     * Set the footer text to use for the attachment.
216
     *
217
     * @param string $footer
218
     *
219
     * @return $this
220
     */
221
    public function setFooter(string $footer)
222
    {
223
        $this->footer = $footer;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Set the footer icon to use for the attachment.
230
     *
231
     * @param string $footerIcon
232
     *
233
     * @return $this
234
     */
235
    public function setFooterIcon(string $footerIcon)
236
    {
237
        $this->footerIcon = $footerIcon;
238
239
        return $this;
240
    }
241
242
    /**
243
     * Set the timestamp to use for the attachment.
244
     *
245
     * @param \DateTime $timestamp
246
     *
247
     * @return $this
248
     */
249
    public function setTimestamp(DateTime $timestamp)
250
    {
251
        $this->timestamp = $timestamp;
252
253
        return $this;
254
    }
255
256
    /**
257
     * Set the title to use for the attachment.
258
     *
259
     * @param string $title
260
     *
261
     * @return $this
262
     */
263
    public function setTitle(string $title)
264
    {
265
        $this->title = $title;
266
267
        return $this;
268
    }
269
270
    /**
271
     * Set the title link to use for the attachment.
272
     *
273
     * @param string $titleLink
274
     *
275
     * @return $this
276
     */
277
    public function setTitleLink(string $titleLink)
278
    {
279
        $this->titleLink = $titleLink;
280
281
        return $this;
282
    }
283
284
    /**
285
     * Set the author name to use for the attachment.
286
     *
287
     * @param string $authorName
288
     *
289
     * @return $this
290
     */
291
    public function setAuthorName(string $authorName)
292
    {
293
        $this->authorName = $authorName;
294
295
        return $this;
296
    }
297
298
    /**
299
     * Set the auhtor link to use for the attachment.
300
     *
301
     * @param string $authorLink
302
     *
303
     * @return $this
304
     */
305
    public function setAuthorLink(string $authorLink)
306
    {
307
        $this->authorLink = $authorLink;
308
309
        return $this;
310
    }
311
312
    /**
313
     * Set the author icon to use for the attachment.
314
     *
315
     * @param string $authorIcon
316
     *
317
     * @return $this
318
     */
319
    public function setAuthorIcon(string $authorIcon)
320
    {
321
        $this->authorIcon = $authorIcon;
322
323
        return $this;
324
    }
325
326
    /**
327
    * Add a field to the attachment.
328
    *
329
    * @param \Spatie\SlashCommand\AttachmentField|array|string $key
330
    * @param null|string $value
331
    *
332
    * @return $this
333
    *
334
    * @throws \Spatie\SlashCommand\Exceptions\FieldCannotBeAdded
335
    */
336
    public function addField($key, $value = null)
337
    {
338
        if (! is_array($key) && ! $key instanceof AttachmentField && is_null($value)) {
339
            throw FieldCannotBeAdded::invalidType();
340
        }
341
342
        if (is_array($key) && $value !== '') {
343
            array_map(function ($value, $index) {
344
                $this->fields->push(AttachmentField::create($index, $value));
345
            }, $key, array_keys($key));
346
347
            return $this;
348
        }
349
350
        if (! $key instanceof AttachmentField && ! is_array($key)) {
351
            $this->fields->push(AttachmentField::create($key, $value));
352
353
            return $this;
354
        }
355
356
        var_dump($key);exit;
0 ignored issues
show
Security Debugging Code introduced by
var_dump($key); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
Coding Style Compatibility introduced by
The method addField() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
357
        $this->fields->push($key);
0 ignored issues
show
Unused Code introduced by
$this->fields->push($key); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
358
359
        return $this;
360
    }
361
362
    /**
363
     * Clear all fields for this attachment.
364
     *
365
     * @return $this
366
     */
367
    public function clearFields()
368
    {
369
        $this->fields = new Collection();
370
371
        return $this;
372
    }
373
374
    /**
375
     * Convert this attachment to its array representation.
376
     *
377
     * @return array
378
     */
379
    public function toArray()
380
    {
381
        return [
382
            'fallback' => $this->fallback,
383
            'text' => $this->text,
384
            'pretext' => $this->preText,
385
            'color' => $this->color,
386
            'footer' => $this->footer,
387
            'footer_icon' => $this->footer,
388
            'ts' => $this->timestamp ? $this->timestamp->getTimestamp() : null,
389
            'image_url' => $this->imageUrl,
390
            'thumb_url' => $this->thumbUrl,
391
            'title' => $this->title,
392
            'title_link' => $this->titleLink,
393
            'author_name' => $this->authorName,
394
            'author_link' => $this->authorLink,
395
            'author_icon' => $this->authorIcon,
396
            'fields' => $this->fields->map(function (AttachmentField $field) {
397
                return $field->toArray();
398
            })->toArray(),
399
        ];
400
    }
401
}
402