Completed
Push — master ( 4b5c92...896769 )
by Adrien
16:06 queued 09:43
created

Ods::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 16
ccs 5
cts 5
cp 1
crap 2
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer;
4
5
use PhpOffice\PhpSpreadsheet\Shared\File;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
8
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content;
9
use PhpOffice\PhpSpreadsheet\Writer\Ods\Meta;
10
use PhpOffice\PhpSpreadsheet\Writer\Ods\MetaInf;
11
use PhpOffice\PhpSpreadsheet\Writer\Ods\Mimetype;
12
use PhpOffice\PhpSpreadsheet\Writer\Ods\Settings;
13
use PhpOffice\PhpSpreadsheet\Writer\Ods\Styles;
14
use PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails;
15
use ZipStream\Exception\OverflowException;
16
use ZipStream\Option\Archive;
17
use ZipStream\ZipStream;
18
19
class Ods extends BaseWriter
20
{
21
    /**
22
     * Private writer parts.
23
     *
24
     * @var Ods\WriterPart[]
25
     */
26
    private $writerParts = [];
27
28
    /**
29
     * Private PhpSpreadsheet.
30
     *
31
     * @var Spreadsheet
32
     */
33
    private $spreadSheet;
34
35
    /**
36
     * Create a new Ods.
37
     *
38
     * @param Spreadsheet $spreadsheet
39
     */
40 9
    public function __construct(Spreadsheet $spreadsheet)
41
    {
42 9
        $this->setSpreadsheet($spreadsheet);
43
44
        $writerPartsArray = [
45 9
            'content' => Content::class,
46
            'meta' => Meta::class,
47
            'meta_inf' => MetaInf::class,
48
            'mimetype' => Mimetype::class,
49
            'settings' => Settings::class,
50
            'styles' => Styles::class,
51
            'thumbnails' => Thumbnails::class,
52
        ];
53
54 9
        foreach ($writerPartsArray as $writer => $class) {
55 9
            $this->writerParts[$writer] = new $class($this);
56
        }
57 9
    }
58
59
    /**
60
     * Get writer part.
61
     *
62
     * @param string $pPartName Writer part name
63
     *
64
     * @return null|Ods\WriterPart
65
     */
66 6
    public function getWriterPart($pPartName)
67
    {
68 6
        if ($pPartName != '' && isset($this->writerParts[strtolower($pPartName)])) {
69 6
            return $this->writerParts[strtolower($pPartName)];
70
        }
71
72
        return null;
73
    }
74
75
    /**
76
     * Save PhpSpreadsheet to file.
77
     *
78
     * @param resource|string $pFilename
79
     *
80
     * @throws WriterException
81
     */
82 6
    public function save($pFilename)
83
    {
84 6
        if (!$this->spreadSheet) {
85
            throw new WriterException('PhpSpreadsheet object unassigned.');
86
        }
87
88
        // garbage collect
89 6
        $this->spreadSheet->garbageCollect();
90
91 6
        $this->openFileHandle($pFilename);
92
93 6
        $zip = $this->createZip();
94
95 6
        $zip->addFile('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->writeManifest());
0 ignored issues
show
Bug introduced by
The method writeManifest() does not exist on PhpOffice\PhpSpreadsheet\Writer\Ods\WriterPart. It seems like you code against a sub-type of PhpOffice\PhpSpreadsheet\Writer\Ods\WriterPart such as PhpOffice\PhpSpreadsheet\Writer\Ods\MetaInf. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        $zip->addFile('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->/** @scrutinizer ignore-call */ writeManifest());
Loading history...
96 6
        $zip->addFile('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->writeThumbnail());
0 ignored issues
show
Bug introduced by
The method writeThumbnail() does not exist on PhpOffice\PhpSpreadsheet\Writer\Ods\WriterPart. It seems like you code against a sub-type of PhpOffice\PhpSpreadsheet\Writer\Ods\WriterPart such as PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        $zip->addFile('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->/** @scrutinizer ignore-call */ writeThumbnail());
Loading history...
97 6
        $zip->addFile('content.xml', $this->getWriterPart('content')->write());
0 ignored issues
show
Bug introduced by
The method write() does not exist on PhpOffice\PhpSpreadsheet\Writer\Ods\WriterPart. It seems like you code against a sub-type of said class. However, the method does not exist in PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails or PhpOffice\PhpSpreadsheet\Writer\Ods\MetaInf. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        $zip->addFile('content.xml', $this->getWriterPart('content')->/** @scrutinizer ignore-call */ write());
Loading history...
98 6
        $zip->addFile('meta.xml', $this->getWriterPart('meta')->write());
99 6
        $zip->addFile('mimetype', $this->getWriterPart('mimetype')->write());
100 6
        $zip->addFile('settings.xml', $this->getWriterPart('settings')->write());
101 6
        $zip->addFile('styles.xml', $this->getWriterPart('styles')->write());
102
103
        // Close file
104
        try {
105 6
            $zip->finish();
106
        } catch (OverflowException $e) {
107
            throw new WriterException('Could not close resource.');
108
        }
109
110 6
        $this->maybeCloseFileHandle();
111 6
    }
112
113
    /**
114
     * Create zip object.
115
     *
116
     * @throws WriterException
117
     *
118
     * @return ZipStream
119
     */
120 6
    private function createZip()
121
    {
122
        // Try opening the ZIP file
123 6
        if (!is_resource($this->fileHandle)) {
124
            throw new WriterException('Could not open resource for writing.');
125
        }
126
127
        // Create new ZIP stream
128 6
        $options = new Archive();
129 6
        $options->setEnableZip64(false);
130 6
        $options->setOutputStream($this->fileHandle);
131
132 6
        return new ZipStream(null, $options);
133
    }
134
135
    /**
136
     * Get Spreadsheet object.
137
     *
138
     * @throws WriterException
139
     *
140
     * @return Spreadsheet
141
     */
142 8
    public function getSpreadsheet()
143
    {
144 8
        if ($this->spreadSheet !== null) {
145 8
            return $this->spreadSheet;
146
        }
147
148
        throw new WriterException('No PhpSpreadsheet assigned.');
149
    }
150
151
    /**
152
     * Set Spreadsheet object.
153
     *
154
     * @param Spreadsheet $spreadsheet PhpSpreadsheet object
155
     *
156
     * @return $this
157
     */
158 9
    public function setSpreadsheet(Spreadsheet $spreadsheet)
159
    {
160 9
        $this->spreadSheet = $spreadsheet;
161
162 9
        return $this;
163
    }
164
}
165