Completed
Push — master ( 01c44a...cc980e )
by Stefan
03:21 queued 40s
created

Finalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 5
Bugs 2 Features 3
Metric Value
c 5
b 2
f 3
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
crap 1
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 2
    public function __construct(Sheet $sheet, Styler $styler, SheetFile $sheetFile)
44
    {
45 2
        $this->sheet = $sheet;
46 2
        $this->styler = $styler;
47 2
        $this->sheetFile = $sheetFile;
48 2
        $this->zip = new \ZipArchive();
49 2
    }
50
51
    /**
52
     * Finalize the xlsx file.
53
     *
54
     * @param string $fileName
55
     * @throws \RuntimeException
56
     */
57 2
    public function finalize($fileName)
58
    {
59 2
        $this->zip->open($fileName, \ZipArchive::CREATE);
60
61 2
        $this->finalizeSheet();
62 2
        $this->finalizeStyles();
63 2
        $this->finalizeDefaultXmls();
64
65 2
        if (!$this->zip->close()) {
66
            throw new \RuntimeException('Failed to save xlsx file!');
67
        }
68 2
    }
69
70
    /**
71
     * Wrap up the sheet (write header, column xmls).
72
     */
73 2
    private function finalizeSheet()
74
    {
75 2
        $this->sheetFile->fwrite('</sheetData></worksheet>');
76 2
        $this->sheetFile->rewind();
77 2
        $this->sheetFile->fwrite(SheetXml::HEADER_XML);
78 2
        $this->sheetFile->fwrite($this->sheet->getDimensionXml());
79 2
        $this->sheetFile->fwrite($this->sheet->getSheetViewsXml());
80 2
        $this->writeColumnWidths();
81 2
        $this->zip->addFile($this->sheetFile->getFilePath(), 'xl/worksheets/sheet1.xml');
82 2
    }
83
84
    /**
85
     * Write column widths xml string.
86
     */
87 2
    private function writeColumnWidths()
88
    {
89 2
        if (0 < count($this->sheet->getColumnWidths())) {
90 1
            $this->sheetFile->fwrite('<cols>');
91 1
            foreach ($this->sheet->getColumnWidths() as $columnNumber => $columnWidth) {
92 1
                $this->sheetFile->fwrite(
93 1
                    sprintf(SheetXml::COLUMN_XML, ($columnNumber + 1), ($columnNumber + 1),
94 1
                        $columnWidth)
95 1
                );
96 1
            }
97 1
            $this->sheetFile->fwrite('</cols>');
98 1
        }
99 2
    }
100
101
    /**
102
     * Write style xml file.
103
     */
104 2
    private function finalizeStyles()
105
    {
106 2
        $this->zip->addFromString('xl/styles.xml', $this->styler->getStyleSheetXml());
107 2
    }
108
109
    /**
110
     * Add default xmls to zip archive.
111
     */
112 2
    private function finalizeDefaultXmls()
113
    {
114 2
        $this->zip->addFromString('[Content_Types].xml', DefaultXml::CONTENT_TYPES);
115 2
        $this->zip->addFromString('docProps/core.xml',
116 2
            sprintf(DefaultXml::DOCPROPS_CORE, date(DATE_ISO8601), date(DATE_ISO8601)));
117 2
        $this->zip->addFromString('docProps/app.xml', DefaultXml::DOCPROPS_APP);
118 2
        $this->zip->addFromString('_rels/.rels', DefaultXml::RELS_RELS);
119 2
        $this->zip->addFromString('xl/_rels/workbook.xml.rels', DefaultXml::XL_RELS_WORKBOOK);
120 2
        $this->zip->addFromString('xl/workbook.xml', DefaultXml::XL_WORKBOOK);
121 2
    }
122
}
123