Failed Conditions
Push — master ( 42761f...d02352 )
by Adrien
16:26 queued 08:32
created

Ods::getWriterPartMimetype()   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 writer parts.
22
     *
23
     * @var Ods\WriterPart[]
24
     */
25
    private $writerParts = [];
0 ignored issues
show
introduced by
The private property $writerParts is not used, and could be removed.
Loading history...
26
27
    /**
28
     * Private PhpSpreadsheet.
29
     *
30
     * @var Spreadsheet
31
     */
32
    private $spreadSheet;
33
34
    /**
35
     * @var Content
36
     */
37
    private $writerPartContent;
38
39
    /**
40
     * @var Meta
41
     */
42
    private $writerPartMeta;
43
44
    /**
45
     * @var MetaInf
46
     */
47
    private $writerPartMetaInf;
48
49
    /**
50
     * @var Mimetype
51
     */
52
    private $writerPartMimetype;
53
54
    /**
55
     * @var Settings
56
     */
57
    private $writerPartSettings;
58
59
    /**
60
     * @var Styles
61
     */
62
    private $writerPartStyles;
63
64
    /**
65
     * @var Thumbnails
66
     */
67
    private $writerPartThumbnails;
68
69
    /**
70
     * Create a new Ods.
71
     */
72 10
    public function __construct(Spreadsheet $spreadsheet)
73
    {
74 10
        $this->setSpreadsheet($spreadsheet);
75
76 10
        $this->writerPartContent = new Content($this);
77 10
        $this->writerPartMeta = new Meta($this);
78 10
        $this->writerPartMetaInf = new MetaInf($this);
79 10
        $this->writerPartMimetype = new Mimetype($this);
80 10
        $this->writerPartSettings = new Settings($this);
81 10
        $this->writerPartStyles = new Styles($this);
82 10
        $this->writerPartThumbnails = new Thumbnails($this);
83 10
    }
84
85 7
    public function getWriterPartContent(): Content
86
    {
87 7
        return $this->writerPartContent;
88
    }
89
90 7
    public function getWriterPartMeta(): Meta
91
    {
92 7
        return $this->writerPartMeta;
93
    }
94
95 7
    public function getWriterPartMetaInf(): MetaInf
96
    {
97 7
        return $this->writerPartMetaInf;
98
    }
99
100 7
    public function getWriterPartMimetype(): Mimetype
101
    {
102 7
        return $this->writerPartMimetype;
103
    }
104
105 7
    public function getWriterPartSettings(): Settings
106
    {
107 7
        return $this->writerPartSettings;
108
    }
109
110 7
    public function getWriterPartStyles(): Styles
111
    {
112 7
        return $this->writerPartStyles;
113
    }
114
115 7
    public function getWriterPartThumbnails(): Thumbnails
116
    {
117 7
        return $this->writerPartThumbnails;
118
    }
119
120
    /**
121
     * Save PhpSpreadsheet to file.
122
     *
123
     * @param resource|string $pFilename
124
     */
125 7
    public function save($pFilename): void
126
    {
127 7
        if (!$this->spreadSheet) {
128
            throw new WriterException('PhpSpreadsheet object unassigned.');
129
        }
130
131
        // garbage collect
132 7
        $this->spreadSheet->garbageCollect();
133
134 7
        $this->openFileHandle($pFilename);
135
136 7
        $zip = $this->createZip();
137
138 7
        $zip->addFile('META-INF/manifest.xml', $this->getWriterPartMetaInf()->write());
139 7
        $zip->addFile('Thumbnails/thumbnail.png', $this->getWriterPartthumbnails()->write());
140 7
        $zip->addFile('content.xml', $this->getWriterPartcontent()->write());
141 7
        $zip->addFile('meta.xml', $this->getWriterPartmeta()->write());
142 7
        $zip->addFile('mimetype', $this->getWriterPartmimetype()->write());
143 7
        $zip->addFile('settings.xml', $this->getWriterPartsettings()->write());
144 7
        $zip->addFile('styles.xml', $this->getWriterPartstyles()->write());
145
146
        // Close file
147
        try {
148 7
            $zip->finish();
149
        } catch (OverflowException $e) {
150
            throw new WriterException('Could not close resource.');
151
        }
152
153 7
        $this->maybeCloseFileHandle();
154 7
    }
155
156
    /**
157
     * Create zip object.
158
     *
159
     * @return ZipStream
160
     */
161 7
    private function createZip()
162
    {
163
        // Try opening the ZIP file
164 7
        if (!is_resource($this->fileHandle)) {
165
            throw new WriterException('Could not open resource for writing.');
166
        }
167
168
        // Create new ZIP stream
169 7
        $options = new Archive();
170 7
        $options->setEnableZip64(false);
171 7
        $options->setOutputStream($this->fileHandle);
172
173 7
        return new ZipStream(null, $options);
174
    }
175
176
    /**
177
     * Get Spreadsheet object.
178
     *
179
     * @return Spreadsheet
180
     */
181 10
    public function getSpreadsheet()
182
    {
183 10
        if ($this->spreadSheet !== null) {
184 10
            return $this->spreadSheet;
185
        }
186
187
        throw new WriterException('No PhpSpreadsheet assigned.');
188
    }
189
190
    /**
191
     * Set Spreadsheet object.
192
     *
193
     * @param Spreadsheet $spreadsheet PhpSpreadsheet object
194
     *
195
     * @return $this
196
     */
197 10
    public function setSpreadsheet(Spreadsheet $spreadsheet)
198
    {
199 10
        $this->spreadSheet = $spreadsheet;
200
201 10
        return $this;
202
    }
203
}
204