Card::activity()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace NotificationChannels\HipChat;
4
5
use Closure;
6
use InvalidArgumentException;
7
8
class Card
9
{
10
    /**
11
     * An id that will help HipChat recognise the same card when it is sent multiple times.
12
     *
13
     * @var string
14
     */
15
    public $id;
16
17
    /**
18
     * The title of the card.
19
     * Valid length range: 1 - 500.
20
     *
21
     * @var string
22
     */
23
    public $title = '';
24
25
    /**
26
     * Style of the card.
27
     * Valid values: file, image, application, link, media.
28
     *
29
     * @var string
30
     */
31
    public $style = CardStyles::APPLICATION;
32
33
    /**
34
     * The description in the specific format.
35
     * Valid length range: 1 - 1000.
36
     *
37
     * @var string
38
     */
39
    public $content = '';
40
41
    /**
42
     * The format that can be html or text.
43
     *
44
     * @var string
45
     */
46
    public $format = 'text';
47
48
    /**
49
     * Application cards can be compact (1 to 2 lines) or medium (1 to 5 lines).
50
     *
51
     * @var string
52
     */
53
    public $cardFormat;
54
55
    /**
56
     * The url where the card will open.
57
     *
58
     * @var string
59
     */
60
    public $url = '';
61
62
    /**
63
     * The thumbnail url. Valid length range: 1 - 250.
64
     *
65
     * @var string
66
     */
67
    public $thumbnail;
68
69
    /**
70
     * The thumbnail url in retina. Valid length range: 1 - 250.
71
     *
72
     * @var string
73
     */
74
    public $thumbnail2;
75
76
    /**
77
     * The original width of the image.
78
     *
79
     * @var int
80
     */
81
    public $thumbnailWidth;
82
83
    /**
84
     * The original height of the image.
85
     *
86
     * @var int
87
     */
88
    public $thumbnailHeight;
89
90
    /**
91
     * Html for the activity to show in one line a summary of the action that happened.
92
     *
93
     * @var string
94
     */
95
    public $activity;
96
97
    /**
98
     * The activity icon url.
99
     *
100
     * @var string
101
     */
102
    public $activityIcon;
103
104
    /**
105
     * The activity icon url in retina.
106
     *
107
     * @var string
108
     */
109
    public $activityIcon2;
110
111
    /**
112
     * The icon url.
113
     *
114
     * @var string
115
     */
116
    public $icon;
117
118
    /**
119
     * The icon url in retina.
120
     *
121
     * @var string
122
     */
123
    public $icon2;
124
125
    /**
126
     * List of attributes to show below the card. Sample {label}:{value.icon} {value.label}.
127
     *
128
     * @var CardAttribute[]
129
     */
130
    public $attributes = [];
131
132
    /**
133
     * Create a new Card instance.
134
     *
135
     * @param string $title
136
     * @param string $id
137
     */
138 23
    public function __construct($title = '', $id = '')
139
    {
140 23
        $this->title($title);
141 23
        $this->id(str_empty($id) ? str_random() : $id);
142 23
    }
143
144
    /**
145
     * Create a new Card instance.
146
     *
147
     * @param string $title
148
     * @param string $id
149
     * @return static
150
     */
151 21
    public static function create($title = '', $id = '')
152
    {
153 21
        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 23
    public function title($title)
163
    {
164 23
        $this->title = trim($title);
165
166 23
        return $this;
167
    }
168
169
    /**
170
     * Sets the id of the card.
171
     *
172
     * @param $id
173
     * @return $this
174
     */
175 23
    public function id($id)
176
    {
177 23
        $this->id = trim($id);
178
179 23
        return $this;
180
    }
181
182
    /**
183
     * Sets the style of the card.
184
     *
185
     * @param $style
186
     * @return $this
187
     */
188 4
    public function style($style)
189
    {
190 4
        $this->style = $style;
191
192 4
        return $this;
193
    }
194
195
    /**
196
     * Sets the content of the card.
197
     *
198
     * @param $content
199
     * @return $this
200
     */
201 9
    public function content($content)
202
    {
203 9
        $this->content = trim($content);
204
205 9
        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 6
    public function text($content = '')
215
    {
216 6
        $this->format = 'text';
217
218 6
        if (! str_empty($content)) {
219 5
            $this->content($content);
220
        }
221
222 6
        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 2
    public function html($content = '')
232
    {
233 2
        $this->format = 'html';
234
235 2
        if (! str_empty($content)) {
236 1
            $this->content($content);
237
        }
238
239 2
        return $this;
240
    }
241
242
    /**
243
     * Sets the format of the card.
244
     *
245
     * @param $cardFormat
246
     * @return $this
247
     */
248 2
    public function cardFormat($cardFormat)
249
    {
250 2
        $this->cardFormat = trim($cardFormat);
251
252 2
        return $this;
253
    }
254
255
    /**
256
     * Sets the url of the card.
257
     *
258
     * @param $url
259
     * @return $this
260
     */
261 2
    public function url($url)
262
    {
263 2
        $this->url = trim($url);
264
265 2
        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 2
    public function thumbnail($icon, $icon2 = null, $width = null, $height = null)
278
    {
279 2
        $this->thumbnail = trim($icon);
280
281 2
        if (! str_empty($icon2)) {
282 2
            $this->thumbnail2 = trim($icon2);
283
        }
284
285 2
        if (! is_null($width)) {
286 2
            $this->thumbnailWidth = $width;
287
        }
288
289 2
        if (! is_null($height)) {
290 2
            $this->thumbnailHeight = $height;
291
        }
292
293 2
        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 2
    public function activity($html, $icon = null, $icon2 = null)
305
    {
306 2
        $this->activity = trim($html);
307
308 2
        if (! str_empty($icon)) {
309 2
            $this->activityIcon = trim($icon);
310
        }
311
312 2
        if (! str_empty($icon2)) {
313 2
            $this->activityIcon2 = trim($icon2);
314
        }
315
316 2
        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 1 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 1
        $this->icon = trim($icon);
329
330 1
        if (! str_empty($icon2)) {
331 1
            $this->icon2 = trim($icon2);
332
        }
333
334 1
        return $this;
335
    }
336
337
    /**
338
     * Adds a CardAttribute to the card.
339
     *
340
     * @param CardAttribute|Closure $attribute
341
     * @return $this
342
     */
343 3 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 3
        if ($attribute instanceof CardAttribute) {
346 2
            $this->attributes[] = $attribute;
347
348 2
            return $this;
349
        }
350
351 1
        if ($attribute instanceof Closure) {
352 1
            $attribute($new = new CardAttribute());
353 1
            $this->attributes[] = $new;
354
355 1
            return $this;
356
        }
357
358
        throw new InvalidArgumentException(
359
            'Invalid attribute type. Expected '.CardAttribute::class.' or '.Closure::class.'.'
360
        );
361
    }
362
363
    /**
364
     * Get an array representation of the Card.
365
     *
366
     * @return array
367
     */
368 5
    public function toArray()
369
    {
370 5
        $card = str_array_filter([
371 5
            'id' => $this->id,
372 5
            'style' => $this->style,
373 5
            'format' => $this->cardFormat,
374 5
            'title' => $this->title,
375 5
            'url' => $this->url,
376
        ]);
377
378 5
        if (! str_empty($this->content)) {
379 4
            $card['description'] = [
380 4
                'value' => $this->content,
381 4
                'format' => $this->format,
382
            ];
383
        }
384
385 5
        if (! str_empty($this->thumbnail)) {
386 1
            $card['thumbnail'] = str_array_filter([
387 1
                'url' => $this->thumbnail,
388 1
                'url@2x' => $this->thumbnail2,
389 1
                'width' => $this->thumbnailWidth,
390 1
                'height' => $this->thumbnailHeight,
391
            ]);
392
        }
393
394 5
        if (! str_empty($this->activity)) {
395 1
            $card['activity'] = str_array_filter([
396 1
                'html' => $this->activity,
397 1
                'icon' => str_array_filter([
398 1
                    'url' => $this->activityIcon,
399 1
                    'url@2x' => $this->activityIcon2,
400
                ]),
401
            ]);
402
        }
403
404 5 View Code Duplication
        if (! str_empty($this->icon)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
405 1
            $card['icon'] = str_array_filter([
406 1
                'url' => $this->icon,
407 1
                'url@2x' => $this->icon2,
408
            ]);
409
        }
410
411 5
        if (! empty($this->attributes)) {
412 1
            $card['attributes'] = array_map(function (CardAttribute $attribute) {
413 1
                return $attribute->toArray();
414 1
            }, $this->attributes);
415
        }
416
417
        return $card;
418
    }
419
}
420