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

Document   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 31
ccs 11
cts 12
cp 0.9167
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A toArray() 0 6 1
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