Passed
Push — master ( f0db14...f804b5 )
by Jelle
02:39
created

PdfToGenerateDTO::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 16
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 5
crap 1
1
<?php
2
3
namespace Traincase\HtmlToPdfTinker\DTO;
4
5
class PdfToGenerateDTO
6
{
7 8
    public function __construct(
8
        // The filename if you plan to store this PDF on the filesystem.
9
        public string $filename,
10
11
        // The HTML string to convert to PDF
12
        public string $html,
13
14
        // Display orientation of the PDF: 'portrait' or 'landscape'
15
        public string $orientation,
16
17
        // Any driver specific options you wish to pass.
18
        public array $options,
19
20
        // The path to where you plan to store this PDF on the filesystem.
21
        public string $path,
22
    ) {
23
    }
24
25 9
    public static function fromArray(array $attributes): self
26
    {
27 9
        if (array_key_exists('orientation', $attributes)
28 9
            && !in_array($attributes['orientation'], ['portrait', 'landscape'])
29
        ) {
30 1
            throw new \InvalidArgumentException(
31 1
                sprintf(
32
                    'Mode should either be "portrait" or "landscape", got "%s"',
33 1
                    $attributes['orientation']
34
                )
35
            );
36
        }
37
38 8
        return new self(
39 8
            filename: $attributes['filename'],
40 8
            html: $attributes['html'],
41 8
            orientation: $attributes['orientation'] ?? 'portrait',
42 8
            options: $attributes['options'] ?? [],
43 8
            path: $attributes['path'] ?? '/',
44
        );
45
    }
46
}
47