Completed
Pull Request — develop (#187)
by Franck
19:04 queued 41s
created

ContentTypes::render()   F

Complexity

Conditions 21
Paths 10368

Size

Total Lines 132
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 68
CRAP Score 24.0487

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 132
ccs 68
cts 84
cp 0.8095
rs 2
cc 21
eloc 72
nc 10368
nop 0
crap 24.0487

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
    public function render()
33
    {
34
        $oLayoutPack = new PowerPoint2007\LayoutPack\PackDefault();
35
36
        // Create XML writer
37
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
38
39
        // XML header
40 61
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
41
42 61
        // Types
43 61
        $objWriter->startElement('Types');
44 1
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
45
46
        // Rels
47 60
        $this->writeDefaultContentType($objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml');
48
49
        // XML
50 60
        $this->writeDefaultContentType($objWriter, 'xml', 'application/xml');
51
52
        // Themes
53 60
        $masterSlides = $oLayoutPack->getMasterSlides();
54 60
        foreach ($masterSlides as $masterSlide) {
55
            $this->writeOverrideContentType($objWriter, '/ppt/theme/theme' . $masterSlide['masterid'] . '.xml', 'application/vnd.openxmlformats-officedocument.theme+xml');
56
        }
57 60
58
        // Presentation
59
        $this->writeOverrideContentType($objWriter, '/ppt/presentation.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml');
60 60
61
        // PptProps
62
        $this->writeOverrideContentType($objWriter, '/ppt/presProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presProps+xml');
63 60
        $this->writeOverrideContentType($objWriter, '/ppt/tableStyles.xml', 'application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml');
64 60
        $this->writeOverrideContentType($objWriter, '/ppt/viewProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml');
65 60
66 60
        // DocProps
67
        $this->writeOverrideContentType($objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml');
68
        $this->writeOverrideContentType($objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml');
69 60
        $this->writeOverrideContentType($objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml');
70
71
        // Slide masters
72 60
        $masterSlides = $oLayoutPack->getMasterSlides();
73 60
        foreach ($masterSlides as $masterSlide) {
74 60
            $this->writeOverrideContentType($objWriter, '/ppt/slideMasters/slideMaster' . $masterSlide['masterid'] . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml');
75
        }
76
77 60
        // Slide layouts
78 60
        $slideLayouts = $oLayoutPack->getLayouts();
79 60
        $numSlideLayouts = count($slideLayouts);
80
        for ($i = 0; $i < $numSlideLayouts; ++$i) {
81
            $this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/slideLayout' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml');
82 60
        }
83 60
84 60
        // Slides
85 60
        $slideCount = $this->oPresentation->getSlideCount();
86
        for ($i = 0; $i < $slideCount; ++$i) {
87
            $this->writeOverrideContentType($objWriter, '/ppt/slides/slide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml');
88 60
            if ($this->oPresentation->getSlide($i)->getNote()->getShapeCollection()->count() > 0) {
89 60
                $this->writeOverrideContentType($objWriter, '/ppt/notesSlides/notesSlide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml');
90 60
            }
91 60
        }
92 60
93
        // Add layoutpack content types
94
        $otherRelations = $oLayoutPack->getMasterSlideRelations();
95 60
        foreach ($otherRelations as $otherRelation) {
96 60
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
97 60
                $this->writeOverrideContentType($objWriter, '/ppt/slideMasters/' . $otherRelation['target'], $otherRelation['contentType']);
98 60
            }
99 1
        }
100 1
        $otherRelations = $oLayoutPack->getThemeRelations();
101 60
        foreach ($otherRelations as $otherRelation) {
102
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
103
                $this->writeOverrideContentType($objWriter, '/ppt/theme/' . $otherRelation['target'], $otherRelation['contentType']);
104 60
            }
105 60
        }
106
        $otherRelations = $oLayoutPack->getLayoutRelations();
107
        foreach ($otherRelations as $otherRelation) {
108
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
109 60
                $this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/' . $otherRelation['target'], $otherRelation['contentType']);
110 60
            }
111 60
        }
112
113
        // Add media content-types
114
        $aMediaContentTypes = array();
115 60
116 60
        // GIF, JPEG, PNG
117 60
        $aMediaContentTypes['gif']  = 'image/gif';
118
        $aMediaContentTypes['jpg']  = 'image/jpeg';
119
        $aMediaContentTypes['jpeg'] = 'image/jpeg';
120
        $aMediaContentTypes['png']  = 'image/png';
121 60
        foreach ($aMediaContentTypes as $key => $value) {
122
            $this->writeDefaultContentType($objWriter, $key, $value);
123
        }
124 60
125
        // XLSX
126
        $this->writeDefaultContentType($objWriter, 'xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
127 60
128 60
        // Other media content types
129 60
        $mediaCount = $this->getDrawingHashTable()->count();
130 60
        for ($i = 0; $i < $mediaCount; ++$i) {
131 60
            $extension = '';
132 60
            $mimeType  = '';
133 60
134
            $shapeIndex = $this->getDrawingHashTable()->getByIndex($i);
135
            if ($shapeIndex instanceof ShapeChart) {
136 60
                // Chart content type
137
                $this->writeOverrideContentType($objWriter, '/ppt/charts/chart' . $shapeIndex->getImageIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml');
138
            } else {
139 60
                if ($shapeIndex instanceof ShapeDrawing) {
140 60
                    $extension = strtolower($shapeIndex->getExtension());
141 23
                    $mimeType  = $this->getImageMimeType($shapeIndex->getPath());
142 23
                } elseif ($shapeIndex instanceof MemoryDrawing) {
143
                    $extension = strtolower($shapeIndex->getMimeType());
144 23
                    $extension = explode('/', $extension);
145 23
                    $extension = $extension[1];
146
147 20
                    $mimeType = $shapeIndex->getMimeType();
148 20
                }
149 4
150 4
                if (!isset($aMediaContentTypes[$extension])) {
151 4
                    $aMediaContentTypes[$extension] = $mimeType;
152 4
153
                    $this->writeDefaultContentType($objWriter, $extension, $mimeType);
154
                }
155
            }
156
        }
157
158
        $objWriter->endElement();
159
160 4
        $this->oZip->addFromString('[Content_Types].xml', $objWriter->getData());
161
162
        return $this->oZip;
163
    }
164
165
    /**
166 23
     * Get image mime type
167
     *
168 60
     * @param  string    $pFile Filename
169
     * @return string    Mime Type
170
     * @throws \Exception
171 60
     */
172
    private function getImageMimeType($pFile = '')
173
    {
174
        if (strpos($pFile, 'zip://') === 0) {
175
            $pZIPFile = str_replace('zip://', '', $pFile);
176
            $pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
177
            if (!File::fileExists($pZIPFile)) {
178
                throw new \Exception("File $pFile does not exist");
179
            }
180
            $pImgFile = substr($pFile, strpos($pFile, '#') + 1);
181 4
            $oArchive = new \ZipArchive();
182
            $oArchive->open($pZIPFile);
183 4
            if (!function_exists('getimagesizefromstring')) {
184 4
                $uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
185
                $image = getimagesize($uri);
186
            } else {
187
                $image = getimagesizefromstring($oArchive->getFromName($pImgFile));
188
            }
189
        } else {
190
            if (!File::fileExists($pFile)) {
191
                throw new \Exception("File $pFile does not exist");
192 4
            }
193
            $image = getimagesize($pFile);
194
        }
195 4
196
        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
    private function writeDefaultContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
208
    {
209 60
        if ($pPartname != '' && $pContentType != '') {
210
            // Write content type
211 60
            $objWriter->startElement('Default');
212
            $objWriter->writeAttribute('Extension', $pPartname);
213 60
            $objWriter->writeAttribute('ContentType', $pContentType);
214 60
            $objWriter->endElement();
215 60
        } else {
216 60
            throw new \Exception("Invalid parameters passed.");
217 60
        }
218
    }
219
220 60
    /**
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
    private function writeOverrideContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
229
    {
230 60
        if ($pPartname != '' && $pContentType != '') {
231
            // Write content type
232 60
            $objWriter->startElement('Override');
233
            $objWriter->writeAttribute('PartName', $pPartname);
234 60
            $objWriter->writeAttribute('ContentType', $pContentType);
235 60
            $objWriter->endElement();
236 60
        } else {
237 60
            throw new \Exception("Invalid parameters passed.");
238 60
        }
239
    }
240
}
241