Completed
Pull Request — develop (#207)
by Franck
14:46 queued 07:07
created

ContentTypes   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 86.36%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 25
c 8
b 0
f 0
lcom 1
cbo 8
dl 0
loc 173
ccs 95
cts 110
cp 0.8636
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A writeDefaultContentType() 0 12 3
F render() 0 124 19
A writeOverrideContentType() 0 12 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
    public function render()
36 97
    {
37
        $oLayoutPack = new PowerPoint2007\LayoutPack\PackDefault();
38 97
39
        // Create XML writer
40
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
41 97
42
        // XML header
43
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
44 97
45
        // Types
46
        $objWriter->startElement('Types');
47 97
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
48 97
49
        // Rels
50
        $this->writeDefaultContentType($objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml');
51 97
52
        // XML
53
        $this->writeDefaultContentType($objWriter, 'xml', 'application/xml');
54 97
55
        // Themes
56
        $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 97
        }
60 97
61
        // Presentation
62
        $this->writeOverrideContentType($objWriter, '/ppt/presentation.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml');
63 97
64
        // PptProps
65
        $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 97
69
        // DocProps
70
        $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 97
74
        // Slide masters
75
        $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 97
        }
79 97
80
        // Slide layouts
81
        $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 97
        }
86 97
87
        // Slides
88
        $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 97
                $this->writeOverrideContentType($objWriter, '/ppt/notesSlides/notesSlide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml');
93 1
            }
94 1
        }
95 97
96
        // Add layoutpack content types
97
        $otherRelations = $oLayoutPack->getMasterSlideRelations();
98 97
        foreach ($otherRelations as $otherRelation) {
99 97
            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 97
            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 97
            if (strpos($otherRelation['target'], 'http://') !== 0 && $otherRelation['contentType'] != '') {
112
                $this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/' . $otherRelation['target'], $otherRelation['contentType']);
113
            }
114
        }
115 97
116
        // Add media content-types
117
        $aMediaContentTypes = array();
118 97
119
        // GIF, JPEG, PNG
120
        $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 97
        }
127 97
128
        // XLSX
129
        $this->writeDefaultContentType($objWriter, 'xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
130 97
131
        // Other media content types
132
        $mediaCount = $this->getDrawingHashTable()->count();
133 97
        for ($i = 0; $i < $mediaCount; ++$i) {
134 97
            $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 35
137
            $shapeIndex = $this->getDrawingHashTable()->getByIndex($i);
138 35
            if ($shapeIndex instanceof ShapeChart) {
139 35
                // Chart content type
140
                $this->writeOverrideContentType($objWriter, '/ppt/charts/chart' . $shapeIndex->getImageIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml');
141 25
            } else {
142 25
                $extension = strtolower($shapeIndex->getExtension());
143 10
                $mimeType = $shapeIndex->getMimeType();
144 9
145 9
                if (!isset($aMediaContentTypes[$extension])) {
146 8
                    $aMediaContentTypes[$extension] = $mimeType;
147 1
148 1
                    $this->writeDefaultContentType($objWriter, $extension, $mimeType);
149 1
                }
150
            }
151 1
        }
152 1
153
        $objWriter->endElement();
154 8
155 2
        $this->oZip->addFromString('[Content_Types].xml', $objWriter->getData());
156
157 2
        return $this->oZip;
158 2
    }
159
160 33
    /**
161
     * Write Default content type
162 95
     *
163
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
164 95
     * @param  string                         $pPartname    Part name
165
     * @param  string                         $pContentType Content type
166 95
     * @throws \Exception
167
     */
168
    private function writeDefaultContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
169
    {
170
        if ($pPartname != '' && $pContentType != '') {
171
            // Write content type
172
            $objWriter->startElement('Default');
173
            $objWriter->writeAttribute('Extension', $pPartname);
174
            $objWriter->writeAttribute('ContentType', $pContentType);
175
            $objWriter->endElement();
176 9
        } else {
177
            throw new \Exception("Invalid parameters passed.");
178 9
        }
179 2
    }
180 2
181 2
    /**
182 1
     * Write Override content type
183
     *
184 1
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
185 1
     * @param  string                         $pPartname    Part name
186 1
     * @param  string                         $pContentType Content type
187 1
     * @throws \Exception
188
     */
189
    private function writeOverrideContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
190
    {
191 1
        if ($pPartname != '' && $pContentType != '') {
192
            // Write content type
193 8
            $objWriter->startElement('Override');
194 1
            $objWriter->writeAttribute('PartName', $pPartname);
195 1
            $objWriter->writeAttribute('ContentType', $pContentType);
196 1
            $objWriter->endElement();
197 1
        } else {
198
            throw new \Exception("Invalid parameters passed.");
199
        }
200
    }
201
}
202