Passed
Push — main ( eccd0c...38deb3 )
by Alejandro
02:01
created

Document::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
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 4
    public function __construct(string $link)
18
    {
19 4
        if (filter_var($link, FILTER_VALIDATE_URL) === false) {
20
            throw new UnsupportedMediaValue($link, 'document', 'Link is not a valid URL');
21
        }
22
23 4
        $extension = pathinfo($link, PATHINFO_EXTENSION);
24
25 4
        if (! in_array($extension, static::SUPPORTED_EXTENSIONS)) {
26 1
            throw new UnsupportedMediaValue($link, 'document', 'Only PDF documents are supported.');
27
        }
28
29 3
        $this->link = $link;
30
    }
31
32 2
    public function toArray(): array
33
    {
34 2
        return [
35 2
            'type' => 'document',
36 2
            'document' => [
37 2
                'link' => $this->link,
38 2
            ],
39 2
        ];
40
    }
41
}
42