Completed
Push — develop ( aca4a2...633916 )
by Franck
11s
created

ContentTypes::render()   F

Complexity

Conditions 12
Paths 336

Size

Total Lines 107
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 59
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 59
cts 59
cp 1
rs 3.7956
c 0
b 0
f 0
cc 12
eloc 60
nc 336
nop 0
crap 12

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