Completed
Push — master ( 5fb76c...11bae5 )
by Mark
32s queued 28s
created

Ods::getWriterPartMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer;
4
5
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
7
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content;
8
use PhpOffice\PhpSpreadsheet\Writer\Ods\Meta;
9
use PhpOffice\PhpSpreadsheet\Writer\Ods\MetaInf;
10
use PhpOffice\PhpSpreadsheet\Writer\Ods\Mimetype;
11
use PhpOffice\PhpSpreadsheet\Writer\Ods\Settings;
12
use PhpOffice\PhpSpreadsheet\Writer\Ods\Styles;
13
use PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails;
14
use ZipStream\Exception\OverflowException;
15
use ZipStream\Option\Archive;
16
use ZipStream\ZipStream;
17
18
class Ods extends BaseWriter
19
{
20
    /**
21
     * Private PhpSpreadsheet.
22
     *
23
     * @var Spreadsheet
24
     */
25
    private $spreadSheet;
26
27
    /**
28
     * @var Content
29
     */
30
    private $writerPartContent;
31
32
    /**
33
     * @var Meta
34
     */
35
    private $writerPartMeta;
36
37
    /**
38
     * @var MetaInf
39
     */
40
    private $writerPartMetaInf;
41
42
    /**
43
     * @var Mimetype
44
     */
45
    private $writerPartMimetype;
46
47
    /**
48
     * @var Settings
49
     */
50
    private $writerPartSettings;
51
52
    /**
53
     * @var Styles
54
     */
55
    private $writerPartStyles;
56
57
    /**
58
     * @var Thumbnails
59
     */
60
    private $writerPartThumbnails;
61
62
    /**
63
     * Create a new Ods.
64
     */
65 24
    public function __construct(Spreadsheet $spreadsheet)
66
    {
67 24
        $this->setSpreadsheet($spreadsheet);
68
69 24
        $this->writerPartContent = new Content($this);
70 24
        $this->writerPartMeta = new Meta($this);
71 24
        $this->writerPartMetaInf = new MetaInf($this);
72 24
        $this->writerPartMimetype = new Mimetype($this);
73 24
        $this->writerPartSettings = new Settings($this);
74 24
        $this->writerPartStyles = new Styles($this);
75 24
        $this->writerPartThumbnails = new Thumbnails($this);
76
    }
77
78 20
    public function getWriterPartContent(): Content
79
    {
80 20
        return $this->writerPartContent;
81
    }
82
83 20
    public function getWriterPartMeta(): Meta
84
    {
85 20
        return $this->writerPartMeta;
86
    }
87
88 20
    public function getWriterPartMetaInf(): MetaInf
89
    {
90 20
        return $this->writerPartMetaInf;
91
    }
92
93 20
    public function getWriterPartMimetype(): Mimetype
94
    {
95 20
        return $this->writerPartMimetype;
96
    }
97
98 20
    public function getWriterPartSettings(): Settings
99
    {
100 20
        return $this->writerPartSettings;
101
    }
102
103 20
    public function getWriterPartStyles(): Styles
104
    {
105 20
        return $this->writerPartStyles;
106
    }
107
108 20
    public function getWriterPartThumbnails(): Thumbnails
109
    {
110 20
        return $this->writerPartThumbnails;
111
    }
112
113
    /**
114
     * Save PhpSpreadsheet to file.
115
     *
116
     * @param resource|string $filename
117
     */
118 20
    public function save($filename, int $flags = 0): void
119
    {
120 20
        $this->processFlags($flags);
121
122
        // garbage collect
123 20
        $this->spreadSheet->garbageCollect();
124
125 20
        $this->openFileHandle($filename);
126
127 20
        $zip = $this->createZip();
128
129 20
        $zip->addFile('META-INF/manifest.xml', $this->getWriterPartMetaInf()->write());
130 20
        $zip->addFile('Thumbnails/thumbnail.png', $this->getWriterPartthumbnails()->write());
131
        // Settings always need to be written before Content; Styles after Content
132 20
        $zip->addFile('settings.xml', $this->getWriterPartsettings()->write());
133 20
        $zip->addFile('content.xml', $this->getWriterPartcontent()->write());
134 20
        $zip->addFile('meta.xml', $this->getWriterPartmeta()->write());
135 20
        $zip->addFile('mimetype', $this->getWriterPartmimetype()->write());
136 20
        $zip->addFile('styles.xml', $this->getWriterPartstyles()->write());
137
138
        // Close file
139
        try {
140 20
            $zip->finish();
141
        } catch (OverflowException $e) {
142
            throw new WriterException('Could not close resource.');
143
        }
144
145 20
        $this->maybeCloseFileHandle();
146
    }
147
148
    /**
149
     * Create zip object.
150
     *
151
     * @return ZipStream
152
     */
153 20
    private function createZip()
154
    {
155
        // Try opening the ZIP file
156 20
        if (!is_resource($this->fileHandle)) {
157
            throw new WriterException('Could not open resource for writing.');
158
        }
159
160
        // Create new ZIP stream
161 20
        $options = new Archive();
162 20
        $options->setEnableZip64(false);
163 20
        $options->setOutputStream($this->fileHandle);
164
165 20
        return new ZipStream(null, $options);
166
    }
167
168
    /**
169
     * Get Spreadsheet object.
170
     *
171
     * @return Spreadsheet
172
     */
173 24
    public function getSpreadsheet()
174
    {
175 24
        return $this->spreadSheet;
176
    }
177
178
    /**
179
     * Set Spreadsheet object.
180
     *
181
     * @param Spreadsheet $spreadsheet PhpSpreadsheet object
182
     *
183
     * @return $this
184
     */
185 24
    public function setSpreadsheet(Spreadsheet $spreadsheet)
186
    {
187 24
        $this->spreadSheet = $spreadsheet;
188
189 24
        return $this;
190
    }
191
}
192