Completed
Push — develop ( 7a734f...e9a151 )
by Stefan
03:10
created

Finalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 103
ccs 44
cts 44
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A finalize() 0 8 1
A finalizeSheet() 0 10 1
A writeColumnWidths() 0 11 3
A finalizeStyles() 0 4 1
A writeDefaultXmls() 0 10 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 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
50 1
    }
51
52
    /**
53
     * Finalize the xlsx file.
54
     *
55
     * @param string $fileName
56
     */
57 1
    public function finalize($fileName)
58
    {
59 1
        $this->zip->open($fileName, \ZipArchive::CREATE);
60 1
        $this->finalizeSheet();
61 1
        $this->finalizeStyles();
62 1
        $this->writeDefaultXmls();
63 1
        $this->zip->close();
64 1
    }
65
66
    /**
67
     * Wrap up the sheet (write header, column xmls).
68
     */
69 1
    private function finalizeSheet()
70
    {
71 1
        $this->sheetFile->fwrite('</sheetData></worksheet>');
72 1
        $this->sheetFile->rewind();
73 1
        $this->sheetFile->fwrite(SheetXml::HEADER_XML);
74 1
        $this->sheetFile->fwrite(sprintf(SheetXml::DIMENSION_XML, $this->sheet->getDimensionUpperBound()));
75 1
        $this->sheetFile->fwrite(sprintf(SheetXml::SHEETVIEWS_XML, '1', 'A2')); // freeze
76 1
        $this->writeColumnWidths();
77 1
        $this->zip->addFile($this->sheetFile->getFilePath(), 'xl/worksheets/sheet1.xml');
78 1
    }
79
80
    /**
81
     * Write column widths xml string.
82
     */
83 1
    private function writeColumnWidths()
84
    {
85 1
        if (0 < count($this->sheet->getCellWidths())) {
86 1
            $this->sheetFile->fwrite('<cols>');
87 1
            foreach ($this->sheet->getCellWidths() as $columnNumber => $columnWidth) {
88 1
                $this->sheetFile->fwrite(sprintf(SheetXml::COLUMN_XML, ($columnNumber + 1), ($columnNumber + 1),
89 1
                    $columnWidth));
90 1
            }
91 1
            $this->sheetFile->fwrite('</cols>');
92 1
        }
93 1
    }
94
95
    /**
96
     * Write style xml file.
97
     */
98 1
    private function finalizeStyles()
99
    {
100 1
        $this->zip->addFromString('xl/styles.xml', $this->styler->getStyleSheetXml());
101 1
    }
102
103
    /**
104
     * Add default xmls to zip archive.
105
     */
106 1
    private function writeDefaultXmls()
107
    {
108 1
        $this->zip->addFromString('[Content_Types].xml', DefaultXml::CONTENT_TYPES);
109 1
        $this->zip->addFromString('docProps/core.xml',
110 1
            sprintf(DefaultXml::DOCPROPS_CORE, date(DATE_ISO8601), date(DATE_ISO8601)));
111 1
        $this->zip->addFromString('docProps/app.xml', DefaultXml::DOCPROPS_APP);
112 1
        $this->zip->addFromString('_rels/.rels', DefaultXml::RELS_RELS);
113 1
        $this->zip->addFromString('xl/_rels/workbook.xml.rels', DefaultXml::XL_RELS_WORKBOOK);
114 1
        $this->zip->addFromString('xl/workbook.xml', DefaultXml::XL_WORKBOOK);
115 1
    }
116
}
117