Completed
Push — master ( fb7d68...1391ca )
by Stefan
24:27
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
     * @throws \RuntimeException
56
     */
57 1
    public function finalize($fileName)
58
    {
59 1
        $this->zip->open($fileName, \ZipArchive::CREATE);
60
61 1
        $this->finalizeSheet();
62 1
        $this->finalizeStyles();
63 1
        $this->finalizeDefaultXmls();
64
65 1
        if (!$this->zip->close()) {
66
            throw new \RuntimeException('Failed to save xlsx file!');
67
        }
68 1
    }
69
70
    /**
71
     * Wrap up the sheet (write header, column xmls).
72
     */
73 1
    private function finalizeSheet()
74
    {
75 1
        $this->sheetFile->fwrite('</sheetData></worksheet>');
76 1
        $this->sheetFile->rewind();
77 1
        $this->sheetFile->fwrite(SheetXml::HEADER_XML);
78 1
        $this->sheetFile->fwrite(sprintf(SheetXml::DIMENSION_XML,
79 1
            $this->sheet->getDimensionUpperBound()));
80 1
        $this->sheetFile->fwrite(sprintf(SheetXml::SHEETVIEWS_XML,
81 1
                $this->sheet->getFreezePaneXml()));
82 1
        $this->writeColumnWidths();
83 1
        $this->zip->addFile($this->sheetFile->getFilePath(), 'xl/worksheets/sheet1.xml');
84 1
    }
85
86
    /**
87
     * Write column widths xml string.
88
     */
89 1
    private function writeColumnWidths()
90
    {
91 1
        if (0 < count($this->sheet->getColumnWidths())) {
92 1
            $this->sheetFile->fwrite('<cols>');
93 1
            foreach ($this->sheet->getColumnWidths() as $columnNumber => $columnWidth) {
94 1
                $this->sheetFile->fwrite(
95 1
                    sprintf(SheetXml::COLUMN_XML, ($columnNumber + 1), ($columnNumber + 1),
96 1
                        $columnWidth)
97 1
                );
98 1
            }
99 1
            $this->sheetFile->fwrite('</cols>');
100 1
        }
101 1
    }
102
103
    /**
104
     * Write style xml file.
105
     */
106 1
    private function finalizeStyles()
107
    {
108 1
        $this->zip->addFromString('xl/styles.xml', $this->styler->getStyleSheetXml());
109 1
    }
110
111
    /**
112
     * Add default xmls to zip archive.
113
     */
114 1
    private function finalizeDefaultXmls()
115
    {
116 1
        $this->zip->addFromString('[Content_Types].xml', DefaultXml::CONTENT_TYPES);
117 1
        $this->zip->addFromString('docProps/core.xml',
118 1
            sprintf(DefaultXml::DOCPROPS_CORE, date(DATE_ISO8601), date(DATE_ISO8601)));
119 1
        $this->zip->addFromString('docProps/app.xml', DefaultXml::DOCPROPS_APP);
120 1
        $this->zip->addFromString('_rels/.rels', DefaultXml::RELS_RELS);
121 1
        $this->zip->addFromString('xl/_rels/workbook.xml.rels', DefaultXml::XL_RELS_WORKBOOK);
122 1
        $this->zip->addFromString('xl/workbook.xml', DefaultXml::XL_WORKBOOK);
123 1
    }
124
}
125