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