Test Failed
Push — develop ( 90366f...812a46 )
by Adrien
28:16
created

Mpdf::save()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 61
Code Lines 37

Duplication

Lines 9
Ratio 14.75 %

Code Coverage

Tests 32
CRAP Score 8.1578

Importance

Changes 0
Metric Value
cc 8
eloc 37
nc 48
nop 1
dl 9
loc 61
ccs 32
cts 37
cp 0.8649
crap 8.1578
rs 7.0047
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;
4
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
7
use PhpOffice\PhpSpreadsheet\Writer\Pdf;
8
9
class Mpdf extends Pdf
10
{
11
    /**
12
     * Gets the implementation of external PDF library that should be used.
13
     *
14
     * @param array $config Configuration array
15
     *
16
     * @return \Mpdf\Mpdf implementation
17
     */
18 1
    protected function createExternalWriterInstance($config)
19
    {
20 1
        return new \Mpdf\Mpdf($config);
21
    }
22
23
    /**
24
     * Save Spreadsheet to file.
25
     *
26
     * @param string $pFilename Name of the file to save as
27
     *
28
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
29
     * @throws PhpSpreadsheetException
30
     */
31 1
    public function save($pFilename)
32
    {
33 1
        $fileHandle = parent::prepareForSave($pFilename);
34
35
        //  Default PDF paper size
36 1
        $paperSize = 'LETTER'; //    Letter    (8.5 in. by 11 in.)
37
38
        //  Check for paper size and page orientation
39 1 View Code Duplication
        if (null === $this->getSheetIndex()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
            $orientation = ($this->spreadsheet->getSheet(0)->getPageSetup()->getOrientation()
41
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
42
            $printPaperSize = $this->spreadsheet->getSheet(0)->getPageSetup()->getPaperSize();
43
        } else {
44 1
            $orientation = ($this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation()
45 1
                == PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
46 1
            $printPaperSize = $this->spreadsheet->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
47
        }
48 1
        $this->setOrientation($orientation);
49
50
        //  Override Page Orientation
51 1
        if (null !== $this->getOrientation()) {
52 1
            $orientation = ($this->getOrientation() == PageSetup::ORIENTATION_DEFAULT)
53
                ? PageSetup::ORIENTATION_PORTRAIT
54 1
                : $this->getOrientation();
55
        }
56 1
        $orientation = strtoupper($orientation);
57
58
        //  Override Paper Size
59 1
        if (null !== $this->getPaperSize()) {
60
            $printPaperSize = $this->getPaperSize();
61
        }
62
63 1
        if (isset(self::$paperSizes[$printPaperSize])) {
64 1
            $paperSize = self::$paperSizes[$printPaperSize];
65
        }
66
67
        //  Create PDF
68 1
        $config = ['tempDir' => $this->tempDir];
69 1
        $pdf = $this->createExternalWriterInstance($config);
70 1
        $ortmp = $orientation;
71 1
        $pdf->_setPageSize(strtoupper($paperSize), $ortmp);
72 1
        $pdf->DefOrientation = $orientation;
73 1
        $pdf->AddPage($orientation);
74
75
        //  Document info
76 1
        $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
77 1
        $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
78 1
        $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
79 1
        $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
80 1
        $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
81
82 1
        $pdf->WriteHTML(
83 1
            $this->generateHTMLHeader(false) .
84 1
            $this->generateSheetData() .
85 1
            $this->generateHTMLFooter()
86
        );
87
88
        //  Write to file
89 1
        fwrite($fileHandle, $pdf->Output('', 'S'));
90
91 1
        parent::restoreStateAfterSave($fileHandle);
92 1
    }
93
}
94