Document::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 7
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 14
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 10
1
<?php
2
3
namespace NotificationChannels\WhatsApp\Component;
4
5
use NotificationChannels\WhatsApp\Exceptions\UnsupportedMediaValue;
6
7
class Document extends Component
8
{
9
    protected const SUPPORTED_EXTENSIONS = ['pdf'];
10
11
    /**
12
     * Link to the document; e.g. https://URL
13
     * Only PDF documents are supported.
14
     */
15
    protected string $link;
16
17
    /**
18
     * File name to be used for file.
19
     */
20
    protected string $filename;
21
22 4
    public function __construct(string $link, ?string $filename = null)
23
    {
24 4
        if (filter_var($link, FILTER_VALIDATE_URL) === false) {
25
            throw new UnsupportedMediaValue($link, 'document', 'Link is not a valid URL');
26
        }
27
28 4
        $extension = pathinfo($link, PATHINFO_EXTENSION);
29
30 4
        if (! in_array($extension, static::SUPPORTED_EXTENSIONS)) {
31 1
            throw new UnsupportedMediaValue($link, 'document', 'Only PDF documents are supported.');
32
        }
33
34 3
        $this->link = $link;
35 3
        $this->filename = empty($filename) ? 'document' : $filename;
36
    }
37
38 2
    public function toArray(): array
39
    {
40 2
        return [
41 2
            'type' => 'document',
42 2
            'document' => [
43 2
                'link' => $this->link,
44 2
                'filename' => $this->filename,
45 2
            ],
46 2
        ];
47
    }
48
}
49