Completed
Push — master ( a79865...f1a019 )
by Adrien
09:59
created

Mpdf::save()   B

Complexity

Conditions 9
Paths 96

Size

Total Lines 68
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 9.1274

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 43
c 2
b 0
f 0
nc 96
nop 1
dl 0
loc 68
ccs 38
cts 43
cp 0.8837
crap 9.1274
rs 7.6764

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
        if (null === $this->getSheetIndex()) {
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->AddPageByArray([
74 1
            'orientation' => $orientation,
75 1
            'margin-left' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getLeft()),
76 1
            'margin-right' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getRight()),
77 1
            'margin-top' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getTop()),
78 1
            'margin-bottom' => $this->inchesToMm($this->spreadsheet->getActiveSheet()->getPageMargins()->getBottom()),
79
        ]);
80
81
        //  Document info
82 1
        $pdf->SetTitle($this->spreadsheet->getProperties()->getTitle());
83 1
        $pdf->SetAuthor($this->spreadsheet->getProperties()->getCreator());
84 1
        $pdf->SetSubject($this->spreadsheet->getProperties()->getSubject());
85 1
        $pdf->SetKeywords($this->spreadsheet->getProperties()->getKeywords());
86 1
        $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());
87
88 1
        $pdf->WriteHTML($this->generateHTMLHeader(false));
89 1
        $html = $this->generateSheetData();
90 1
        foreach (\array_chunk(\explode(PHP_EOL, $html), 1000) as $lines) {
91 1
            $pdf->WriteHTML(\implode(PHP_EOL, $lines));
92
        }
93 1
        $pdf->WriteHTML($this->generateHTMLFooter());
94
95
        //  Write to file
96 1
        fwrite($fileHandle, $pdf->Output('', 'S'));
97
98 1
        parent::restoreStateAfterSave($fileHandle);
99 1
    }
100
101
    /**
102
     * Convert inches to mm.
103
     *
104
     * @param float $inches
105
     *
106
     * @return float
107
     */
108 1
    private function inchesToMm($inches)
109
    {
110 1
        return $inches * 25.4;
111
    }
112
}
113