Test Failed
Push — main ( c1b6b4...9835d1 )
by Yaroslav
12:46
created

AbstractDocumentFromImage   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 65.63%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 80
ccs 21
cts 32
cp 0.6563
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A temporalFile() 0 8 1
A download() 0 7 1
A generateName() 0 11 3
A save() 0 12 3
A inline() 0 7 1
A extension() 0 11 3
A asView() 0 3 1
1
<?php
2
3
namespace LPDFBuilder\Generation;
4
5
use Illuminate\Http\Response;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\Str;
8
use Knp\Snappy\Exception\FileAlreadyExistsException;
9
use setasign\Fpdi\Fpdi;
10
11
abstract class AbstractDocumentFromImage implements DocumentGenerator
12
{
13
    protected string $fileExtension = 'pdf';
14
15
    public function extension(string|Extension $fileExtension = 'pdf'): static
16
    {
17
        if ($fileExtension instanceof Extension) {
18
            $fileExtension = $fileExtension->value;
19
        }
20
21
        if ($fileExtension !== Extension::PDF->value) {
22
            throw new \Exception('Supports only pdf extension');
23
        }
24
25
        return $this;
26
    }
27
28 4
    public function generateName(?string $defaultName = null): string
29
    {
30 4
        if ($defaultName) {
31 4
            if (Str::endsWith($defaultName, ".{$this->fileExtension}")) {
32
                $defaultName = Str::beforeLast($defaultName, ".{$this->fileExtension}");
33
            }
34
35 4
            return $defaultName.'.'.$this->fileExtension;
36
        }
37
38
        return Str::random().'.'.$this->fileExtension;
39
    }
40
41
    public function asView(): ?\Illuminate\Contracts\View\View
42
    {
43
        return null;
44
    }
45
46 1
    public function temporalFile(?string $filename = null): ?string
47
    {
48 1
        $filePath = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR)
49
                    .DIRECTORY_SEPARATOR
50 1
                    .ltrim($this->generateName($filename), DIRECTORY_SEPARATOR);
51 1
        $this->generate()->Output('F', $filePath);
52
53 1
        return $filePath;
54
    }
55
56 1
    public function inline(?string $filename = null): Response
57
    {
58 1
        return new Response($this->generate()->Output('S'), 200, [
59
            'Content-Type'        => 'application/pdf',
60 1
            'Content-Disposition' => 'inline; filename="'.Str::afterLast($this->generateName($filename), DIRECTORY_SEPARATOR).'"',
61
            'Cache-Control'       => 'private, max-age=0, must-revalidate',
62
            'Pragma'              => 'public',
63
        ]);
64
    }
65
66 1
    public function download(?string $filename = null): Response
67
    {
68 1
        return new Response($this->generate()->Output('S'), 200, [
69
            'Content-Type'        => 'application/pdf',
70 1
            'Content-Disposition' => 'attachment; filename="'.Str::afterLast($this->generateName($filename), DIRECTORY_SEPARATOR).'"',
71
            'Cache-Control'       => 'private, max-age=0, must-revalidate',
72
            'Pragma'              => 'public',
73
        ]);
74
    }
75
76 1
    public function save(?string $disc = null, ?string $filename = null, bool $overwrite = false, array $options = []): static
77
    {
78 1
        $filename = $this->generateName($filename);
79 1
        $storage  = Storage::disk($disc);
80 1
        if (!$overwrite && $storage->exists($filename)) {
81
            // tangled with snappy, maybe good to create internal own exception?
82
            throw new FileAlreadyExistsException(\sprintf('The output file \'%s\' already exists.', $storage->path($filename)));
83
        }
84
85 1
        $storage->put($filename, $this->generate()->Output('S'));
86
87 1
        return $this;
88
    }
89
90
    abstract protected function generate(): Fpdi;
91
}
92