Completed
Push — master ( 8206f8...a56904 )
by Stefan
03:17
created

Finalizer::finalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.0078
1
<?php
2
3
namespace OneSheet;
4
5
use OneSheet\Style\Styler;
6
use OneSheet\Xml\DefaultXml;
7
use OneSheet\Xml\SheetXml;
8
9
/**
10
 * Class Finalizer, a rather quick and dirty solution for now.
11
 *
12
 * @package OneSheet
13
 */
14
class Finalizer
15
{
16
    /**
17
     * @var Sheet
18
     */
19
    private $sheet;
20
21
    /**
22
     * @var SheetFile
23
     */
24
    private $sheetFile;
25
26
    /**
27
     * @var \ZipArchive
28
     */
29
    private $zip;
30
31
    /**
32
     * @var Styler
33
     */
34
    private $styler;
35
36
    /**
37
     * Finalizer constructor.
38
     *
39
     * @param Sheet     $sheet
40
     * @param Styler    $styler
41
     * @param SheetFile $sheetFile
42
     */
43 1
    public function __construct(Sheet $sheet, Styler $styler, SheetFile $sheetFile)
44
    {
45 1
        $this->sheet = $sheet;
46 1
        $this->styler = $styler;
47 1
        $this->sheetFile = $sheetFile;
48 1
        $this->zip = new \ZipArchive();
49 1
    }
50
51
    /**
52
     * Finalize the xlsx file.
53
     *
54
     * @param string $fileName
55
     */
56 1
    public function finalize($fileName)
57
    {
58 1
        $this->zip->open($fileName, \ZipArchive::CREATE);
59
60 1
        $this->finalizeSheet();
61 1
        $this->finalizeStyles();
62 1
        $this->finalizeDefaultXmls();
63
64 1
        if (!$this->zip->close()) {
65
            throw new \RuntimeException('Failed to save xlsx file!');
66
        }
67 1
    }
68
69
    /**
70
     * Wrap up the sheet (write header, column xmls).
71
     */
72 1
    private function finalizeSheet()
73
    {
74 1
        $this->sheetFile->fwrite('</sheetData></worksheet>');
75 1
        $this->sheetFile->rewind();
76 1
        $this->sheetFile->fwrite(SheetXml::HEADER_XML);
77 1
        $this->sheetFile->fwrite(sprintf(SheetXml::DIMENSION_XML,
78 1
            $this->sheet->getDimensionUpperBound()));
79 1
        $this->sheetFile->fwrite(sprintf(SheetXml::SHEETVIEWS_XML,
80 1
                $this->sheet->getFreezePaneXml()));
81 1
        $this->writeColumnWidths();
82 1
        $this->zip->addFile($this->sheetFile->getFilePath(), 'xl/worksheets/sheet1.xml');
83 1
    }
84
85
    /**
86
     * Write column widths xml string.
87
     */
88 1
    private function writeColumnWidths()
89
    {
90 1
        if (0 < count($this->sheet->getColumnWidths())) {
91 1
            $this->sheetFile->fwrite('<cols>');
92 1
            foreach ($this->sheet->getColumnWidths() as $columnNumber => $columnWidth) {
93 1
                $this->sheetFile->fwrite(
94 1
                    sprintf(SheetXml::COLUMN_XML, ($columnNumber + 1), ($columnNumber + 1),
95 1
                        $columnWidth)
96 1
                );
97 1
            }
98 1
            $this->sheetFile->fwrite('</cols>');
99 1
        }
100 1
    }
101
102
    /**
103
     * Write style xml file.
104
     */
105 1
    private function finalizeStyles()
106
    {
107 1
        $this->zip->addFromString('xl/styles.xml', $this->styler->getStyleSheetXml());
108 1
    }
109
110
    /**
111
     * Add default xmls to zip archive.
112
     */
113 1
    private function finalizeDefaultXmls()
114
    {
115 1
        $this->zip->addFromString('[Content_Types].xml', DefaultXml::CONTENT_TYPES);
116 1
        $this->zip->addFromString('docProps/core.xml',
117 1
            sprintf(DefaultXml::DOCPROPS_CORE, date(DATE_ISO8601), date(DATE_ISO8601)));
118 1
        $this->zip->addFromString('docProps/app.xml', DefaultXml::DOCPROPS_APP);
119 1
        $this->zip->addFromString('_rels/.rels', DefaultXml::RELS_RELS);
120 1
        $this->zip->addFromString('xl/_rels/workbook.xml.rels', DefaultXml::XL_RELS_WORKBOOK);
121 1
        $this->zip->addFromString('xl/workbook.xml', DefaultXml::XL_WORKBOOK);
122 1
    }
123
}
124