Completed
Push — master ( 3f8806...162df9 )
by Peter
02:17
created

Card::title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace NotificationChannels\HipChat;
4
5
class Card
6
{
7
    /**
8
     * An id that will help HipChat recognise the same card when it is sent multiple times.
9
     *
10
     * @var string
11
     */
12
    public $id;
13
14
    /**
15
     * The title of the card.
16
     * Valid length range: 1 - 500.
17
     *
18
     * @var string
19
     */
20
    public $title = '';
21
22
    /**
23
     * Style of the card.
24
     * Valid values: file, image, application, link, media.
25
     *
26
     * @var string
27
     */
28
    public $style = CardStyles::APPLICATION;
29
30
    /**
31
     * The description in the specific format.
32
     * Valid length range: 1 - 1000.
33
     *
34
     * @var string
35
     */
36
    public $content = '';
37
38
    /**
39
     * The format that can be html or text.
40
     *
41
     * @var string
42
     */
43
    public $format = 'text';
44
45
    /**
46
     * Application cards can be compact (1 to 2 lines) or medium (1 to 5 lines).
47
     *
48
     * @var string
49
     */
50
    public $cardFormat;
51
52
    /**
53
     * The url where the card will open.
54
     *
55
     * @var string
56
     */
57
    public $url = '';
58
59
    /**
60
     * The thumbnail url. Valid length range: 1 - 250.
61
     *
62
     * @var string
63
     */
64
    public $thumbnail;
65
66
    /**
67
     * The thumbnail url in retina. Valid length range: 1 - 250.
68
     *
69
     * @var string
70
     */
71
    public $thumbnail2;
72
73
    /**
74
     * The original width of the image.
75
     *
76
     * @var int
77
     */
78
    public $thumbnailWidth;
79
80
    /**
81
     * The original height of the image.
82
     *
83
     * @var int
84
     */
85
    public $thumbnailHeight;
86
87
    /**
88
     * Html for the activity to show in one line a summary of the action that happened.
89
     *
90
     * @var string
91
     */
92
    public $activity;
93
94
    /**
95
     * The activity icon url.
96
     *
97
     * @var string
98
     */
99
    public $activityIcon;
100
101
    /**
102
     * The activity icon url in retina.
103
     *
104
     * @var string
105
     */
106
    public $activityIcon2;
107
108
    /**
109
     * The icon url.
110
     *
111
     * @var string
112
     */
113
    public $icon;
114
115
    /**
116
     * The icon url in retina.
117
     *
118
     * @var string
119
     */
120
    public $icon2;
121
122
    /**
123
     * List of attributes to show below the card. Sample {label}:{value.icon} {value.label}.
124
     *
125
     * @var CardAttribute[]
126
     */
127
    public $attributes = [];
128
129
    /**
130
     * Create a new Card instance.
131
     *
132
     * @param string $title
133
     * @param string $id
134
     */
135
    public function __construct($title = '', $id = '')
136
    {
137
        if (! empty($title)) {
138
            $this->title = $title;
139
        }
140
141
        $this->id = $id ?: str_random();
142
    }
143
144
    /**
145
     * Create a new Card instance.
146
     *
147
     * @param string $title
148
     * @param string $id
149
     * @return static
150
     */
151
    public static function create($title = '', $id = '')
152
    {
153
        return new static($title, $id);
154
    }
155
156
    /**
157
     * Sets the title of the card.
158
     *
159
     * @param string $title
160
     * @return $this
161
     */
162
    public function title($title)
163
    {
164
        $this->title = trim($title);
165
166
        return $this;
167
    }
168
169
    /**
170
     * Sets the id of the card.
171
     *
172
     * @param $id
173
     * @return $this
174
     */
175
    public function id($id)
176
    {
177
        $this->id = trim($id);
178
179
        return $this;
180
    }
181
182
    /**
183
     * Sets the style of the card.
184
     *
185
     * @param $style
186
     * @return $this
187
     */
188
    public function style($style)
189
    {
190
        $this->style = $style;
191
192
        return $this;
193
    }
194
195
    /**
196
     * Sets the content of the card.
197
     *
198
     * @param $content
199
     * @return $this
200
     */
201
    public function content($content)
202
    {
203
        $this->content = trim($content);
204
205
        return $this;
206
    }
207
208
    /**
209
     * Sets the format to plain text and optionally the content.
210
     *
211
     * @param string $content
212
     * @return $this
213
     */
214
    public function text($content = '')
215
    {
216
        $this->format = 'text';
217
218
        if (! empty($content)) {
219
            $this->content($content);
220
        }
221
222
        return $this;
223
    }
224
225
    /**
226
     * Sets the format to html and optionally the content.
227
     *
228
     * @param string $content
229
     * @return $this
230
     */
231
    public function html($content = '')
232
    {
233
        $this->format = 'html';
234
235
        if (! empty($content)) {
236
            $this->content($content);
237
        }
238
239
        return $this;
240
    }
241
242
    /**
243
     * Sets the format of the card.
244
     *
245
     * @param $cardFormat
246
     * @return $this
247
     */
248
    public function cardFormat($cardFormat)
249
    {
250
        $this->cardFormat = trim($cardFormat);
251
252
        return $this;
253
    }
254
255
    /**
256
     * Sets the url of the card.
257
     *
258
     * @param $url
259
     * @return $this
260
     */
261
    public function url($url)
262
    {
263
        $this->url = trim($url);
264
265
        return $this;
266
    }
267
268
    /**
269
     * Sets the thumbnail of the card.
270
     *
271
     * @param string $icon
272
     * @param string|null $icon2
273
     * @param int|null $width
274
     * @param int|null $height
275
     * @return $this
276
     */
277
    public function thumbnail($icon, $icon2 = null, $width = null, $height = null)
278
    {
279
        $this->thumbnail = trim($icon);
280
281
        if (! empty($icon2)) {
282
            $this->thumbnail2 = trim($icon2);
283
        }
284
285
        if (! empty($width)) {
286
            $this->thumbnailWidth = $width;
287
        }
288
289
        if (! empty($height)) {
290
            $this->thumbnailHeight = $height;
291
        }
292
293
        return $this;
294
    }
295
296
    /**
297
     * Sets the activity of the card.
298
     *
299
     * @param string $html
300
     * @param string|null $icon
301
     * @param string|null $icon2
302
     * @return $this
303
     */
304
    public function activity($html, $icon = null, $icon2 = null)
305
    {
306
        $this->activity = trim($html);
307
308
        if (! empty($icon)) {
309
            $this->activityIcon = trim($icon);
310
        }
311
312
        if (! empty($icon2)) {
313
            $this->activityIcon2 = trim($icon2);
314
        }
315
316
        return $this;
317
    }
318
319
    /**
320
     * Sets the icon of the card.
321
     *
322
     * @param string $icon
323
     * @param string|null $icon2
324
     * @return $this
325
     */
326 View Code Duplication
    public function icon($icon, $icon2 = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
327
    {
328
        $this->icon = trim($icon);
329
330
        if (! empty($icon2)) {
331
            $this->icon2 = trim($icon2);
332
        }
333
334
        return $this;
335
    }
336
337
    /**
338
     * Adds a CardAttribute to the card.
339
     *
340
     * @param CardAttribute|\Closure $attribute
341
     * @return $this
342
     */
343 View Code Duplication
    public function addAttribute($attribute)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
344
    {
345
        if ($attribute instanceof CardAttribute) {
346
            $this->attributes[] = $attribute;
347
348
            return $this;
349
        }
350
351
        if ($attribute instanceof \Closure) {
352
            $attribute($new = new CardAttribute());
353
            $this->attributes[] = $new;
354
355
            return $this;
356
        }
357
358
        throw new \InvalidArgumentException('Invalid attribute type. Expected '.CardAttribute::class.' or '.\Closure::class.'.');
359
    }
360
361
    /**
362
     * Get an array representation of the Card.
363
     *
364
     * @return array
365
     */
366
    public function toArray()
367
    {
368
        $card = array_filter([
369
            'id' => $this->id,
370
            'style' => $this->style,
371
            'format' => $this->cardFormat,
372
            'title' => $this->title,
373
            'url' => $this->url,
374
        ]);
375
376
        if (! empty($this->content)) {
377
            $card['description'] = [
378
                'value' => $this->content,
379
                'format' => $this->format,
380
            ];
381
        }
382
383
        if (! empty($this->thumbnail)) {
384
            $card['thumbnail'] = array_filter([
385
                'url' => $this->thumbnail,
386
                'url@2x' => $this->thumbnail2,
387
                'width' => $this->thumbnailWidth,
388
                'height' => $this->thumbnailHeight,
389
            ]);
390
        }
391
392
        if (! empty($this->activity)) {
393
            $card['activity'] = array_filter([
394
                'html' => $this->activity,
395
                'icon' => array_filter([
396
                    'url' => $this->activityIcon,
397
                    'url@2x' => $this->activityIcon2,
398
                ]),
399
            ]);
400
        }
401
402
        if (! empty($this->icon)) {
403
            $card['icon'] = array_filter([
404
                'url' => $this->icon,
405
                'url@2x' => $this->icon2,
406
            ]);
407
        }
408
409
        if (! empty($this->attributes)) {
410
            $card['attributes'] = array_map(function (CardAttribute $attribute) {
411
                return $attribute->toArray();
412
            }, $this->attributes);
413
        }
414
415
        return $card;
416
    }
417
}
418