Completed
Pull Request — develop (#230)
by Franck
07:07
created

ContentTypes::render()   C

Complexity

Conditions 9
Paths 72

Size

Total Lines 94
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 9

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 94
ccs 50
cts 50
cp 1
rs 5.0297
cc 9
eloc 51
nc 72
nop 0
crap 9

How to fix   Long Method   

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\Common\XMLWriter;
23
use PhpOffice\PhpPresentation\Writer\PowerPoint2007;
24
25
/**
26
 * \PhpOffice\PhpPresentation\Writer\PowerPoint2007\ContentTypes
27
 */
28
class ContentTypes extends AbstractDecoratorWriter
29
{
30
    /**
31
     * @return \PhpOffice\Common\Adapter\Zip\ZipInterface
32
     * @throws \Exception
33
     */
34 98
    public function render()
35
    {
36
        // Create XML writer
37 98
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
38
39
        // XML header
40 98
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
41
42
        // Types
43 98
        $objWriter->startElement('Types');
44 98
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
45
46
        // Rels
47 98
        $this->writeDefaultContentType($objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml');
48
49
        // XML
50 98
        $this->writeDefaultContentType($objWriter, 'xml', 'application/xml');
51
52
        // Presentation
53 98
        $this->writeOverrideContentType($objWriter, '/ppt/presentation.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml');
54
55
        // PptProps
56 98
        $this->writeOverrideContentType($objWriter, '/ppt/presProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.presProps+xml');
57 98
        $this->writeOverrideContentType($objWriter, '/ppt/tableStyles.xml', 'application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml');
58 98
        $this->writeOverrideContentType($objWriter, '/ppt/viewProps.xml', 'application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml');
59
60
        // DocProps
61 98
        $this->writeOverrideContentType($objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml');
62 98
        $this->writeOverrideContentType($objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml');
63 98
        $this->writeOverrideContentType($objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml');
64
65
        // Slide masters
66 98
        $sldLayoutNr = 0;
67 98
        $sldLayoutId = time() + 689016272; // requires minimum value of 2 147 483 648
68 98
        foreach ($this->oPresentation->getAllMasterSlides() as $idx => $oSlideMaster) {
69 98
            $oSlideMaster->setRelsIndex($idx + 1);
70 98
            $this->writeOverrideContentType($objWriter, '/ppt/slideMasters/slideMaster' . $oSlideMaster->getRelsIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml');
71 98
            $this->writeOverrideContentType($objWriter, '/ppt/theme/theme' . $oSlideMaster->getRelsIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.theme+xml');
72 98
            foreach ($oSlideMaster->getAllSlideLayouts() as $oSlideLayout) {
73 98
                $oSlideLayout->layoutNr = ++$sldLayoutNr;
74 98
                $oSlideLayout->layoutId = ++$sldLayoutId;
75 98
                $this->writeOverrideContentType($objWriter, '/ppt/slideLayouts/slideLayout' . $oSlideLayout->layoutNr . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml');
76
            }
77
        }
78
79
        // Slides
80 98
        $slideCount = $this->oPresentation->getSlideCount();
81 98
        for ($i = 0; $i < $slideCount; ++$i) {
82 98
            $this->writeOverrideContentType($objWriter, '/ppt/slides/slide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.slide+xml');
83 98
            if ($this->oPresentation->getSlide($i)->getNote()->getShapeCollection()->count() > 0) {
84 1
                $this->writeOverrideContentType($objWriter, '/ppt/notesSlides/notesSlide' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml');
85
            }
86
        }
87
88
        // Add media content-types
89 98
        $aMediaContentTypes = array();
90
91
        // GIF, JPEG, PNG
92 98
        $aMediaContentTypes['gif']  = 'image/gif';
93 98
        $aMediaContentTypes['jpg']  = 'image/jpeg';
94 98
        $aMediaContentTypes['jpeg'] = 'image/jpeg';
95 98
        $aMediaContentTypes['png']  = 'image/png';
96 98
        foreach ($aMediaContentTypes as $key => $value) {
97 98
            $this->writeDefaultContentType($objWriter, $key, $value);
98
        }
99
100
        // XLSX
101 98
        $this->writeDefaultContentType($objWriter, 'xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
102
103
        // Other media content types
104 98
        $mediaCount = $this->getDrawingHashTable()->count();
105 98
        for ($i = 0; $i < $mediaCount; ++$i) {
106 35
            $shapeIndex = $this->getDrawingHashTable()->getByIndex($i);
107 35
            if ($shapeIndex instanceof ShapeChart) {
108
                // Chart content type
109 25
                $this->writeOverrideContentType($objWriter, '/ppt/charts/chart' . $shapeIndex->getImageIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml');
110
            } else {
111 10
                $extension = strtolower($shapeIndex->getExtension());
112 10
                $mimeType = $shapeIndex->getMimeType();
113
114 8
                if (!isset($aMediaContentTypes[$extension])) {
115 1
                    $aMediaContentTypes[$extension] = $mimeType;
116
117 1
                    $this->writeDefaultContentType($objWriter, $extension, $mimeType);
118
                }
119
            }
120
        }
121
122 96
        $objWriter->endElement();
123
124 96
        $this->oZip->addFromString('[Content_Types].xml', $objWriter->getData());
125
126 96
        return $this->oZip;
127
    }
128
129
    /**
130
     * Write Default content type
131
     *
132
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
133
     * @param  string                         $pPartname    Part name
134
     * @param  string                         $pContentType Content type
135
     * @throws \Exception
136
     */
137 98
    private function writeDefaultContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
138
    {
139 98
        if ($pPartname == '' || $pContentType == '') {
140
            throw new \Exception("Invalid parameters passed.");
141
        }
142
        // Write content type
143 98
        $objWriter->startElement('Default');
144 98
        $objWriter->writeAttribute('Extension', $pPartname);
145 98
        $objWriter->writeAttribute('ContentType', $pContentType);
146 98
        $objWriter->endElement();
147 98
    }
148
149
    /**
150
     * Write Override content type
151
     *
152
     * @param  \PhpOffice\Common\XMLWriter $objWriter    XML Writer
153
     * @param  string                         $pPartname    Part name
154
     * @param  string                         $pContentType Content type
155
     * @throws \Exception
156
     */
157 98
    private function writeOverrideContentType(XMLWriter $objWriter, $pPartname = '', $pContentType = '')
158
    {
159 98
        if ($pPartname == '' || $pContentType == '') {
160
            throw new \Exception("Invalid parameters passed.");
161
        }
162
        // Write content type
163 98
        $objWriter->startElement('Override');
164 98
        $objWriter->writeAttribute('PartName', $pPartname);
165 98
        $objWriter->writeAttribute('ContentType', $pContentType);
166 98
        $objWriter->endElement();
167 98
    }
168
}
169