AbstractDocumentFromTemplate::generate()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
1
<?php
2
3
namespace LPDFBuilder\Generation;
4
5
use setasign\Fpdi\Fpdi;
6
7
abstract class AbstractDocumentFromTemplate extends AbstractDocumentFromImage
8
{
9
    use HasTemplateWithImmutablePagesSize;
10
11
    /**
12
     * @inerhitDoc
13
     */
14 6
    public function generate(): Fpdi
15
    {
16 6
        $templatePath = $this->sourceFileTemplatePath();
17
18 5
        $pdf = new Fpdi();
19
20 5
        $pagesCount = $templatePath ? $pdf->setSourceFile($templatePath) : 1;
21
22 4
        for ($page = 1; $page <= $pagesCount; $page++) {
23 4
            $pdf->AddPage('', [$this->templatePageWidth, $this->templatePageHeight]);
24 4
            if ($templatePath) {
25 4
                $tplId = $pdf->importPage($page);
26 4
                $pdf->useTemplate($tplId, 0, 0, $this->templatePageWidth);
27
            }
28
29 4
            $pdf = $this->applyContent($pdf, $page);
30
        }
31
32 4
        return $pdf;
33
    }
34
35
    /**
36
     * @param  Fpdi  $pdf
37
     * @param  int  $page
38
     * @return Fpdi
39
     */
40
    abstract protected function applyContent(Fpdi $pdf, int $page = 1): Fpdi;
41
}
42