Completed
Push — master ( e77f4f...2b26f2 )
by Peter
03:40
created

Card   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 413
Duplicated Lines 6.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 99.14%

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 32
c 4
b 2
f 1
lcom 1
cbo 1
dl 25
loc 413
ccs 115
cts 116
cp 0.9914
rs 9.6

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A create() 0 4 1
A title() 0 6 1
A id() 0 6 1
A style() 0 6 1
A content() 0 6 1
A text() 0 10 2
A html() 0 10 2
A cardFormat() 0 6 1
A url() 0 6 1
A thumbnail() 0 18 4
A activity() 0 14 3
A icon() 10 10 2
A addAttribute() 15 17 3
B toArray() 0 51 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 21
    public function __construct($title = '', $id = '')
136
    {
137 21
        if (! empty($title)) {
138 1
            $this->title = $title;
139 1
        }
140
141 21
        $this->id = $id ?: str_random();
142 21
    }
143
144
    /**
145
     * Create a new Card instance.
146
     *
147
     * @param string $title
148
     * @param string $id
149
     * @return static
150
     */
151 19
    public static function create($title = '', $id = '')
152
    {
153 19
        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 5
    public function title($title)
163
    {
164 5
        $this->title = trim($title);
165
166 5
        return $this;
167
    }
168
169
    /**
170
     * Sets the id of the card.
171
     *
172
     * @param $id
173
     * @return $this
174
     */
175 4
    public function id($id)
176
    {
177 4
        $this->id = trim($id);
178
179 4
        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 8
    public function content($content)
202
    {
203 8
        $this->content = trim($content);
204
205 8
        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 5
    public function text($content = '')
215
    {
216 5
        $this->format = 'text';
217
218 5
        if (! empty($content)) {
219 4
            $this->content($content);
220 4
        }
221
222 5
        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 (! empty($content)) {
236 1
            $this->content($content);
237 1
        }
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 (! empty($icon2)) {
282 2
            $this->thumbnail2 = trim($icon2);
283 2
        }
284
285 2
        if (! empty($width)) {
286 2
            $this->thumbnailWidth = $width;
287 2
        }
288
289 2
        if (! empty($height)) {
290 2
            $this->thumbnailHeight = $height;
291 2
        }
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 (! empty($icon)) {
309 2
            $this->activityIcon = trim($icon);
310 2
        }
311
312 2
        if (! empty($icon2)) {
313 2
            $this->activityIcon2 = trim($icon2);
314 2
        }
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 (! empty($icon2)) {
331 1
            $this->icon2 = trim($icon2);
332 1
        }
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('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 3
    public function toArray()
367
    {
368 3
        $card = array_filter([
369 3
            'id' => $this->id,
370 3
            'style' => $this->style,
371 3
            'format' => $this->cardFormat,
372 3
            'title' => $this->title,
373 3
            'url' => $this->url,
374 3
        ]);
375
376 3
        if (! empty($this->content)) {
377 3
            $card['description'] = [
378 3
                'value' => $this->content,
379 3
                'format' => $this->format,
380
            ];
381 3
        }
382
383 3
        if (! empty($this->thumbnail)) {
384 1
            $card['thumbnail'] = array_filter([
385 1
                'url' => $this->thumbnail,
386 1
                'url@2x' => $this->thumbnail2,
387 1
                'width' => $this->thumbnailWidth,
388 1
                'height' => $this->thumbnailHeight,
389 1
            ]);
390 1
        }
391
392 3
        if (! empty($this->activity)) {
393 1
            $card['activity'] = array_filter([
394 1
                'html' => $this->activity,
395 1
                'icon' => array_filter([
396 1
                    'url' => $this->activityIcon,
397 1
                    'url@2x' => $this->activityIcon2,
398 1
                ]),
399 1
            ]);
400 1
        }
401
402 3
        if (! empty($this->icon)) {
403 1
            $card['icon'] = array_filter([
404 1
                'url' => $this->icon,
405 1
                'url@2x' => $this->icon2,
406 1
            ]);
407 1
        }
408
409 3
        if (! empty($this->attributes)) {
410 1
            $card['attributes'] = array_map(function (CardAttribute $attribute) {
411 1
                return $attribute->toArray();
412 1
            }, $this->attributes);
413 1
        }
414
415 3
        return $card;
416
    }
417
}
418