Total Complexity | 5 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Coverage | 92.86% |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
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 |
|
45 | 2 | ], |
|
46 | 2 | ]; |
|
49 |