Completed
Pull Request — develop (#207)
by Franck
06:53
created

ContentTypes::getImageMimeType()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 36
ccs 21
cts 21
cp 1
rs 6.7272
cc 7
eloc 28
nc 7
nop 1
crap 7

1 Method

Rating   Name   Duplication   Size   Complexity  
A ContentTypes::writeOverrideContentType() 0 11 3
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\Common\File;
23
use PhpOffice\Common\XMLWriter;
24
use PhpOffice\PhpPresentation\Writer\PowerPoint2007;
25
26
/**
27
 * \PhpOffice\PhpPresentation\Writer\PowerPoint2007\ContentTypes
28
 */
29
class ContentTypes extends AbstractDecoratorWriter
30
{
31
    /**
32
     * @return \PhpOffice\Common\Adapter\Zip\ZipInterface
33
     * @throws \Exception
34
     */
35 97
    public function render()
36
    {
37 97
        $oLayoutPack = new PowerPoint2007\LayoutPack\PackDefault();
38
39
        // Create XML writer
40 97
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
41
42
        // XML header
43 97
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
44
45
        // Types
46 97
        $objWriter->startElement('Types');
47 97
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
48
49
        // Rels
50 97
        $this->writeDefaultContentType($objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml');
51
52
        // XML
53 97
        $this->writeDefaultContentType($objWriter, 'xml', 'application/xml');
54
55
        // Themes
56 97
        $masterSlides = $oLayoutPack->getMasterSlides();
57 97
        foreach ($masterSlides as $masterSlide) {
58 97
            $this->writeOverrideContentType($objWriter, '/ppt/theme/theme' . $masterSlide['masterid'] . '.xml', 'application/vnd.openxmlformats-officedocument.theme+xml');
59
        }
60
61
        // Presentation
62 97
        $this->writeOverrideContentType($objWriter, '/ppt/presentation.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml');
63
64
        // PptProps
65 97
        $this->writeOverrideContentType($objWriter, '/ppt/presProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presProps+xml');
66 97
        $this->writeOverrideContentType($objWriter, '/ppt/tableStyles.xml', 'application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml');
67 97
        $this->writeOverrideContentType($objWriter, '/ppt/viewProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml');
68
69
        // DocProps
70 97
        $this->writeOverrideContentType($objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml');
71 97
        $this->writeOverrideContentType($objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml');
72 97
        $this->writeOverrideContentType($objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml');
73
74
        // Slide masters
75 97
        $masterSlides = $oLayoutPack->getMasterSlides();
76 97
        foreach ($masterSlides as $masterSlide) {
77 97
            $this->writeOverrideContentType($objWriter, '/ppt/slideMasters/slideMaster' . $masterSlide['masterid'] . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml');
78
        }
79
80
        // Slide layouts
81 97
        $slideLayouts = $oLayoutPack->getLayouts();
82 97
        $numSlideLayouts = count($slideLayouts);
83 97
        for ($i = 0; $i < $numSlideLayouts; ++$i) {
84 97
            $this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/slideLayout' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml');
85
        }
86
87
        // Slides
88 97
        $slideCount = $this->oPresentation->getSlideCount();
89 97
        for ($i = 0; $i < $slideCount; ++$i) {
90 97
            $this->writeOverrideContentType($objWriter, '/ppt/slides/slide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml');
91 97
            if ($this->oPresentation->getSlide($i)->getNote()->getShapeCollection()->count() > 0) {
92 1
                $this->writeOverrideContentType($objWriter, '/ppt/notesSlides/notesSlide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml');
93
            }
94
        }
95
96
        // Add layoutpack content types
97 97
        $otherRelations = $oLayoutPack->getMasterSlideRelations();
98 97
        foreach ($otherRelations as $otherRelation) {
99
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
100
                $this->writeOverrideContentType($objWriter, '/ppt/slideMasters/' . $otherRelation['target'], $otherRelation['contentType']);
101
            }
102
        }
103 97
        $otherRelations = $oLayoutPack->getThemeRelations();
104 97
        foreach ($otherRelations as $otherRelation) {
105
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
106
                $this->writeOverrideContentType($objWriter, '/ppt/theme/' . $otherRelation['target'], $otherRelation['contentType']);
107
            }
108
        }
109 97
        $otherRelations = $oLayoutPack->getLayoutRelations();
110 97
        foreach ($otherRelations as $otherRelation) {
111
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
112
                $this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/' . $otherRelation['target'], $otherRelation['contentType']);
113
            }
114
        }
115
116
        // Add media content-types
117 97
        $aMediaContentTypes = array();
118
119
        // GIF, JPEG, PNG
120 97
        $aMediaContentTypes['gif']  = 'image/gif';
121 97
        $aMediaContentTypes['jpg']  = 'image/jpeg';
122 97
        $aMediaContentTypes['jpeg'] = 'image/jpeg';
123 97
        $aMediaContentTypes['png']  = 'image/png';
124 97
        foreach ($aMediaContentTypes as $key => $value) {
125 97
            $this->writeDefaultContentType($objWriter, $key, $value);
126
        }
127
128
        // XLSX
129 97
        $this->writeDefaultContentType($objWriter, 'xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
130
131
        // Other media content types
132 97
        $mediaCount = $this->getDrawingHashTable()->count();
133 97
        for ($i = 0; $i < $mediaCount; ++$i) {
134 35
            $extension = '';
0 ignored issues
show
Unused Code introduced by
$extension is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135 35
            $mimeType  = '';
0 ignored issues
show
Unused Code introduced by
$mimeType is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
136
137 35
            $shapeIndex = $this->getDrawingHashTable()->getByIndex($i);
138 35
            if ($shapeIndex instanceof ShapeChart) {
139
                // Chart content type
140 25
                $this->writeOverrideContentType($objWriter, '/ppt/charts/chart' . $shapeIndex->getImageIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml');
141
            } else {
142 10
                $extension = strtolower($shapeIndex->getExtension());
143 10
                $mimeType = $shapeIndex->getMimeType();
144
145 8
                if (!isset($aMediaContentTypes[$extension])) {
146 1
                    $aMediaContentTypes[$extension] = $mimeType;
147
148 1
                    $this->writeDefaultContentType($objWriter, $extension, $mimeType);
149
                }
150
            }
151
        }
152
153 95
        $objWriter->endElement();
154
155 95
        $this->oZip->addFromString('[Content_Types].xml', $objWriter->getData());
156
157 95
        return $this->oZip;
158
    }
159
160
    /**
161
     * Write Default content type
162
     *
163
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
164
     * @param  string                         $pPartname    Part name
165
     * @param  string                         $pContentType Content type
166
     * @throws \Exception
167
     */
168 97
    private function writeDefaultContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
169
    {
170 97
        if ($pPartname == '' || $pContentType == '') {
171
            throw new \Exception("Invalid parameters passed.");
172
        }
173
        // Write content type
174 97
        $objWriter->startElement('Default');
175 97
        $objWriter->writeAttribute('Extension', $pPartname);
176 97
        $objWriter->writeAttribute('ContentType', $pContentType);
177 97
        $objWriter->endElement();
178 97
    }
179
180
    /**
181
     * Write Override content type
182
     *
183
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
184
     * @param  string                         $pPartname    Part name
185
     * @param  string                         $pContentType Content type
186
     * @throws \Exception
187
     */
188 97
    private function writeOverrideContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
189
    {
190 97
        if ($pPartname == '' || $pContentType == '') {
191
            throw new \Exception("Invalid parameters passed.");
192
        }
193
        // Write content type
194 97
        $objWriter->startElement('Override');
195 97
        $objWriter->writeAttribute('PartName', $pPartname);
196 97
        $objWriter->writeAttribute('ContentType', $pContentType);
197 97
        $objWriter->endElement();
198 97
    }
199
}
200