Completed
Pull Request — develop (#186)
by
unknown
07:55
created

ContentTypes::render()   F

Complexity

Conditions 21
Paths 10368

Size

Total Lines 132
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 62
CRAP Score 21.899

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 132
ccs 62
cts 71
cp 0.8732
rs 2
cc 21
eloc 72
nc 10368
nop 0
crap 21.899

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
19
20
use PhpOffice\PhpPresentation\Shape\Chart as ShapeChart;
21
use PhpOffice\PhpPresentation\Shape\Drawing as ShapeDrawing;
22
use PhpOffice\PhpPresentation\Shape\MemoryDrawing;
23
use PhpOffice\Common\File;
24
use PhpOffice\Common\XMLWriter;
25
use PhpOffice\PhpPresentation\Writer\PowerPoint2007;
26
27
/**
28
 * \PhpOffice\PhpPresentation\Writer\PowerPoint2007\ContentTypes
29
 */
30
class ContentTypes extends AbstractDecoratorWriter
31
{
32 75
    public function render()
33
    {
34 75
        $oLayoutPack = new PowerPoint2007\LayoutPack\PackDefault();
35
36
        // Create XML writer
37 75
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
38
39
        // XML header
40 75
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
41
42
        // Types
43 75
        $objWriter->startElement('Types');
44 75
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
45
46
        // Rels
47 75
        $this->writeDefaultContentType($objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml');
48
49
        // XML
50 75
        $this->writeDefaultContentType($objWriter, 'xml', 'application/xml');
51
52
        // Themes
53 75
        $masterSlides = $oLayoutPack->getMasterSlides();
54 75
        foreach ($masterSlides as $masterSlide) {
55 75
            $this->writeOverrideContentType($objWriter, '/ppt/theme/theme' . $masterSlide['masterid'] . '.xml', 'application/vnd.openxmlformats-officedocument.theme+xml');
56
        }
57
58
        // Presentation
59 75
        $this->writeOverrideContentType($objWriter, '/ppt/presentation.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml');
60
61
        // PptProps
62 75
        $this->writeOverrideContentType($objWriter, '/ppt/presProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presProps+xml');
63 75
        $this->writeOverrideContentType($objWriter, '/ppt/tableStyles.xml', 'application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml');
64 75
        $this->writeOverrideContentType($objWriter, '/ppt/viewProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml');
65
66
        // DocProps
67 75
        $this->writeOverrideContentType($objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml');
68 75
        $this->writeOverrideContentType($objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml');
69 75
        $this->writeOverrideContentType($objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml');
70
71
        // Slide masters
72 75
        $masterSlides = $oLayoutPack->getMasterSlides();
73 75
        foreach ($masterSlides as $masterSlide) {
74 75
            $this->writeOverrideContentType($objWriter, '/ppt/slideMasters/slideMaster' . $masterSlide['masterid'] . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml');
75
        }
76
77
        // Slide layouts
78 75
        $slideLayouts = $oLayoutPack->getLayouts();
79 75
        $numSlideLayouts = count($slideLayouts);
80 75
        for ($i = 0; $i < $numSlideLayouts; ++$i) {
81 75
            $this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/slideLayout' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml');
82
        }
83
84
        // Slides
85 75
        $slideCount = $this->oPresentation->getSlideCount();
86 75
        for ($i = 0; $i < $slideCount; ++$i) {
87 75
            $this->writeOverrideContentType($objWriter, '/ppt/slides/slide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml');
88 75
            if ($this->oPresentation->getSlide($i)->getNote()->getShapeCollection()->count() > 0) {
89 1
                $this->writeOverrideContentType($objWriter, '/ppt/notesSlides/notesSlide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml');
90
            }
91
        }
92
93
        // Add layoutpack content types
94 75
        $otherRelations = $oLayoutPack->getMasterSlideRelations();
95 75
        foreach ($otherRelations as $otherRelation) {
96
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
97
                $this->writeOverrideContentType($objWriter, '/ppt/slideMasters/' . $otherRelation['target'], $otherRelation['contentType']);
98
            }
99
        }
100 75
        $otherRelations = $oLayoutPack->getThemeRelations();
101 75
        foreach ($otherRelations as $otherRelation) {
102
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
103
                $this->writeOverrideContentType($objWriter, '/ppt/theme/' . $otherRelation['target'], $otherRelation['contentType']);
104
            }
105
        }
106 75
        $otherRelations = $oLayoutPack->getLayoutRelations();
107 75
        foreach ($otherRelations as $otherRelation) {
108
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
109
                $this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/' . $otherRelation['target'], $otherRelation['contentType']);
110
            }
111
        }
112
113
        // Add media content-types
114 75
        $aMediaContentTypes = array();
115
116
        // GIF, JPEG, PNG
117 75
        $aMediaContentTypes['gif']  = 'image/gif';
118 75
        $aMediaContentTypes['jpg']  = 'image/jpeg';
119 75
        $aMediaContentTypes['jpeg'] = 'image/jpeg';
120 75
        $aMediaContentTypes['png']  = 'image/png';
121 75
        foreach ($aMediaContentTypes as $key => $value) {
122 75
            $this->writeDefaultContentType($objWriter, $key, $value);
123
        }
124
125
        // XLSX
126 75
        $this->writeDefaultContentType($objWriter, 'xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
127
128
        // Other media content types
129 75
        $mediaCount = $this->getDrawingHashTable()->count();
130 75
        for ($i = 0; $i < $mediaCount; ++$i) {
131 26
            $extension = '';
132 26
            $mimeType  = '';
133
134 26
            $shapeIndex = $this->getDrawingHashTable()->getByIndex($i);
135 26
            if ($shapeIndex instanceof ShapeChart) {
136
                // Chart content type
137 20
                $this->writeOverrideContentType($objWriter, '/ppt/charts/chart' . $shapeIndex->getImageIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml');
138
            } else {
139 6
                if ($shapeIndex instanceof ShapeDrawing) {
140 5
                    $extension = strtolower($shapeIndex->getExtension());
141 5
                    $mimeType  = $this->getImageMimeType($shapeIndex->getPath());
142
                } elseif ($shapeIndex instanceof MemoryDrawing) {
143 1
                    $extension = strtolower($shapeIndex->getMimeType());
144 1
                    $extension = explode('/', $extension);
145 1
                    $extension = $extension[1];
146
147 1
                    $mimeType = $shapeIndex->getMimeType();
148
                }
149
150 6
                if (!isset($aMediaContentTypes[$extension])) {
151
                    $aMediaContentTypes[$extension] = $mimeType;
152
153
                    $this->writeDefaultContentType($objWriter, $extension, $mimeType);
154
                }
155
            }
156
        }
157
158 75
        $objWriter->endElement();
159
160 75
        $this->oZip->addFromString('[Content_Types].xml', $objWriter->getData());
161
162 75
        return $this->oZip;
163
    }
164
165
    /**
166
     * Get image mime type
167
     *
168
     * @param  string    $pFile Filename
169
     * @return string    Mime Type
170
     * @throws \Exception
171
     */
172 5
    private function getImageMimeType($pFile = '')
173
    {
174 5
        if (strpos($pFile, 'zip://') === 0) {
175 1
            $pZIPFile = str_replace('zip://', '', $pFile);
176 1
            $pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
177 1
            if (!File::fileExists($pZIPFile)) {
178
                throw new \Exception("File $pFile does not exist");
179
            }
180 1
            $pImgFile = substr($pFile, strpos($pFile, '#') + 1);
181 1
            $oArchive = new \ZipArchive();
182 1
            $oArchive->open($pZIPFile);
183 1
            if (!function_exists('getimagesizefromstring')) {
184
                $uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
185
                $image = getimagesize($uri);
186
            } else {
187 1
                $image = getimagesizefromstring($oArchive->getFromName($pImgFile));
188
            }
189
        } else {
190 4
            if (!File::fileExists($pFile)) {
191
                throw new \Exception("File $pFile does not exist");
192
            }
193 4
            $image = getimagesize($pFile);
194
        }
195
196 5
        return image_type_to_mime_type($image[2]);
197
    }
198
199
    /**
200
     * Write Default content type
201
     *
202
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
203
     * @param  string                         $pPartname    Part name
204
     * @param  string                         $pContentType Content type
205
     * @throws \Exception
206
     */
207 75
    private function writeDefaultContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
208
    {
209 75
        if ($pPartname != '' && $pContentType != '') {
210
            // Write content type
211 75
            $objWriter->startElement('Default');
212 75
            $objWriter->writeAttribute('Extension', $pPartname);
213 75
            $objWriter->writeAttribute('ContentType', $pContentType);
214 75
            $objWriter->endElement();
215
        } else {
216
            throw new \Exception("Invalid parameters passed.");
217
        }
218 75
    }
219
220
    /**
221
     * Write Override content type
222
     *
223
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
224
     * @param  string                         $pPartname    Part name
225
     * @param  string                         $pContentType Content type
226
     * @throws \Exception
227
     */
228 75
    private function writeOverrideContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
229
    {
230 75
        if ($pPartname != '' && $pContentType != '') {
231
            // Write content type
232 75
            $objWriter->startElement('Override');
233 75
            $objWriter->writeAttribute('PartName', $pPartname);
234 75
            $objWriter->writeAttribute('ContentType', $pContentType);
235 75
            $objWriter->endElement();
236
        } else {
237
            throw new \Exception("Invalid parameters passed.");
238
        }
239 75
    }
240
}
241