Completed
Push — develop ( 50ed76...6a48b5 )
by Adrien
63:50
created

Ods::getWriterPart()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
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 ZipArchive;
16
17
class Ods extends BaseWriter
18
{
19
    /**
20
     * Private writer parts.
21
     *
22
     * @var Ods\WriterPart[]
23
     */
24
    private $writerParts = [];
25
26
    /**
27
     * Private PhpSpreadsheet.
28
     *
29
     * @var Spreadsheet
30
     */
31
    private $spreadSheet;
32
33
    /**
34
     * Create a new Ods.
35
     *
36
     * @param Spreadsheet $spreadsheet
37
     */
38 8
    public function __construct(Spreadsheet $spreadsheet)
39
    {
40 8
        $this->setSpreadsheet($spreadsheet);
41
42
        $writerPartsArray = [
43 8
            'content' => Content::class,
44
            'meta' => Meta::class,
45
            'meta_inf' => MetaInf::class,
46
            'mimetype' => Mimetype::class,
47
            'settings' => Settings::class,
48
            'styles' => Styles::class,
49
            'thumbnails' => Thumbnails::class,
50
        ];
51
52 8
        foreach ($writerPartsArray as $writer => $class) {
53 8
            $this->writerParts[$writer] = new $class($this);
54
        }
55 8
    }
56
57
    /**
58
     * Get writer part.
59
     *
60
     * @param string $pPartName Writer part name
61
     *
62
     * @return null|Ods\WriterPart
63
     */
64 5
    public function getWriterPart($pPartName)
65
    {
66 5
        if ($pPartName != '' && isset($this->writerParts[strtolower($pPartName)])) {
67 5
            return $this->writerParts[strtolower($pPartName)];
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * Save PhpSpreadsheet to file.
75
     *
76
     * @param string $pFilename
77
     *
78
     * @throws WriterException
79
     */
80 5
    public function save($pFilename)
81
    {
82 5
        if (!$this->spreadSheet) {
83
            throw new WriterException('PhpSpreadsheet object unassigned.');
84
        }
85
86
        // garbage collect
87 5
        $this->spreadSheet->garbageCollect();
88
89
        // If $pFilename is php://output or php://stdout, make it a temporary file...
90 5
        $originalFilename = $pFilename;
91 5
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
92
            $pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
93
            if ($pFilename == '') {
94
                $pFilename = $originalFilename;
95
            }
96
        }
97
98 5
        $zip = $this->createZip($pFilename);
99
100 5
        $zip->addFromString('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

100
        $zip->addFromString('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->/** @scrutinizer ignore-call */ writeManifest());
Loading history...
101 5
        $zip->addFromString('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

101
        $zip->addFromString('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->/** @scrutinizer ignore-call */ writeThumbnail());
Loading history...
102 5
        $zip->addFromString('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

102
        $zip->addFromString('content.xml', $this->getWriterPart('content')->/** @scrutinizer ignore-call */ write());
Loading history...
103 5
        $zip->addFromString('meta.xml', $this->getWriterPart('meta')->write());
104 5
        $zip->addFromString('mimetype', $this->getWriterPart('mimetype')->write());
105 5
        $zip->addFromString('settings.xml', $this->getWriterPart('settings')->write());
106 5
        $zip->addFromString('styles.xml', $this->getWriterPart('styles')->write());
107
108
        // Close file
109 5
        if ($zip->close() === false) {
110
            throw new WriterException("Could not close zip file $pFilename.");
111
        }
112
113
        // If a temporary file was used, copy it to the correct file stream
114 5
        if ($originalFilename != $pFilename) {
115
            if (copy($pFilename, $originalFilename) === false) {
116
                throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename.");
117
            }
118
            @unlink($pFilename);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

118
            /** @scrutinizer ignore-unhandled */ @unlink($pFilename);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
119
        }
120 5
    }
121
122
    /**
123
     * Create zip object.
124
     *
125
     * @param string $pFilename
126
     *
127
     * @throws WriterException
128
     *
129
     * @return ZipArchive
130
     */
131 5
    private function createZip($pFilename)
132
    {
133
        // Create new ZIP file and open it for writing
134 5
        $zip = new ZipArchive();
135
136 5
        if (file_exists($pFilename)) {
137 5
            unlink($pFilename);
138
        }
139
        // Try opening the ZIP file
140 5
        if ($zip->open($pFilename, ZipArchive::OVERWRITE) !== true) {
141 5
            if ($zip->open($pFilename, ZipArchive::CREATE) !== true) {
142
                throw new WriterException("Could not open $pFilename for writing.");
143
            }
144
        }
145
146 5
        return $zip;
147
    }
148
149
    /**
150
     * Get Spreadsheet object.
151
     *
152
     * @throws WriterException
153
     *
154
     * @return Spreadsheet
155
     */
156 7
    public function getSpreadsheet()
157
    {
158 7
        if ($this->spreadSheet !== null) {
159 7
            return $this->spreadSheet;
160
        }
161
162
        throw new WriterException('No PhpSpreadsheet assigned.');
163
    }
164
165
    /**
166
     * Set Spreadsheet object.
167
     *
168
     * @param Spreadsheet $spreadsheet PhpSpreadsheet object
169
     *
170
     * @return self
171
     */
172 8
    public function setSpreadsheet(Spreadsheet $spreadsheet)
173
    {
174 8
        $this->spreadSheet = $spreadsheet;
175
176 8
        return $this;
177
    }
178
}
179