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

AbstractDocumentFromHtml::file()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 10
nop 0
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.9332
1
<?php
2
3
namespace LPDFBuilder\Generation;
4
5
use Barryvdh\Snappy\Facades\SnappyImage;
6
use Barryvdh\Snappy\Facades\SnappyPdf;
7
use Illuminate\Http\Response;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Facades\Storage;
10
use Illuminate\Support\Facades\View;
11
use Illuminate\Support\Str;
12
13
abstract class AbstractDocumentFromHtml implements DocumentGenerator
14
{
15
    protected string $fileExtension = 'pdf';
16
17
    protected mixed $certificateData = [];
18
19
    /**
20
     * List of possible extensions.
21
     *
22
     * @return array
23
     */
24
    public function possibleExtensions(): array
25
    {
26
        return [Extension::PDF->value, Extension::JPG->value];
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function extension(string|Extension $fileExtension = 'pdf'): static
33
    {
34
        if ($fileExtension instanceof Extension) {
35
            $fileExtension = $fileExtension->value;
36
        }
37
        if (!in_array($fileExtension, $this->possibleExtensions())) {
38
            throw new \Exception('Not valid extension. Allowed: '.implode(', ', $this->possibleExtensions()));
39
        }
40
        $this->fileExtension = $fileExtension;
41
42
        return $this;
43
    }
44
45
46
    abstract public function viewName(): string;
47
48
    public function headerViewName(): ?string
49
    {
50
        return null;
51
    }
52
53
    public function footerViewName(): ?string
54
    {
55
        return null;
56
    }
57
58 1
    public function viewData(): array
59
    {
60
        return [
61 1
            'certificate' => $this->certificateData,
62
        ];
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function asView(): ?\Illuminate\Contracts\View\View
69
    {
70
        return View::make($this->viewName(), $this->viewData());
71
    }
72
73
    /**
74
     * @return \Barryvdh\Snappy\PdfWrapper|\Barryvdh\Snappy\ImageWrapper
75
     */
76 1
    public function file(): \Barryvdh\Snappy\PdfWrapper|\Barryvdh\Snappy\ImageWrapper
77
    {
78 1
        $file = match ($this->fileExtension) {
79 1
            Extension::JPG->value => SnappyImage::loadView($this->viewName(), $this->viewData()),
80 1
            default               => SnappyPdf::loadView($this->viewName(), $this->viewData()),
81
        };
82
83
        try {
84 1
            if ($headerViewName = $this->headerViewName()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $headerViewName is correct as $this->headerViewName() targeting LPDFBuilder\Generation\A...mHtml::headerViewName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85 1
                $file->setOption('header-html', view($headerViewName, $this->viewData())->render());
0 ignored issues
show
Bug introduced by
$headerViewName of type void is incompatible with the type null|string expected by parameter $view of view(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
                $file->setOption('header-html', view(/** @scrutinizer ignore-type */ $headerViewName, $this->viewData())->render());
Loading history...
86
            }
87 1
            if ($footerViewName = $this->footerViewName()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $footerViewName is correct as $this->footerViewName() targeting LPDFBuilder\Generation\A...mHtml::footerViewName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88 1
                $file->setOption('footer-html', view($footerViewName, $this->viewData())->render());
89
            }
90
        } catch (\InvalidArgumentException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
91
        }
92
93 1
        return $file;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function temporalFile(?string $filename = null): ?string
100
    {
101
        $filePath = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR)
102
                    .DIRECTORY_SEPARATOR
103
                    .ltrim($this->generateName($filename), DIRECTORY_SEPARATOR);
104
        $this->file()->save($filePath, true);
105
106
        return $filePath;
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function inline(?string $filename = null): Response
113
    {
114
        return $this->file()->inline(Str::afterLast($this->generateName($filename), DIRECTORY_SEPARATOR));
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120
    public function download(?string $filename = null): Response
121
    {
122
        return $this->file()->download(Str::afterLast($this->generateName($filename), DIRECTORY_SEPARATOR));
123
    }
124
125
    /**
126
     * @inheritDoc
127
     */
128 1
    public function save(?string $disc = null, ?string $filename = null, $overwrite = false, array $options = []): static
129
    {
130 1
        $storage = Storage::disk($disc);
131 1
        $this->file()->save($storage->path($this->generateName($filename)), $overwrite);
132
133
        return $this;
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139 1
    public function generateName(?string $defaultName = null): string
140
    {
141 1
        if ($defaultName) {
142 1
            if (Str::endsWith($defaultName, ".{$this->fileExtension}")) {
143
                $defaultName = Str::beforeLast($defaultName, ".{$this->fileExtension}");
144
            }
145
146 1
            return $defaultName.'.'.$this->fileExtension;
147
        }
148
149
        return Arr::get($this->certificateData, 'uuid', Arr::get($this->certificateData, 'id', Str::random())).".{$this->fileExtension}";
150
    }
151
}
152