Completed
Push — master ( 1ff41e...4d0f8f )
by Stefan
02:53
created

Finalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

6 Methods

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