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

PptTheme::writeTheme()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 87
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 4.5101

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 87
ccs 28
cts 41
cp 0.6828
rs 8.4157
c 2
b 0
f 0
cc 4
eloc 39
nc 3
nop 1
crap 4.5101

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
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\PhpPresentation\Slide;
6
use PhpOffice\Common\XMLWriter;
7
8
class PptTheme extends AbstractDecoratorWriter
9
{
10
    /**
11
     * @return \PhpOffice\Common\Adapter\Zip\ZipInterface
12
     * @throws \Exception
13
     */
14 95
    public function render()
15
    {
16 95
        foreach ($this->oPresentation->getAllMasterSlides() as $oMasterSlide) {
17 95
            $this->getZip()->addFromString('ppt/theme/_rels/theme' . $oMasterSlide->getRelsIndex() . '.xml.rels', $this->writeThemeRelationships($oMasterSlide->getRelsIndex()));
18 95
            $this->getZip()->addFromString('ppt/theme/theme' . $oMasterSlide->getRelsIndex() . '.xml', $this->writeTheme($oMasterSlide));
19 95
        }
20
21
        /*
22
        $otherRelations = $oLayoutPack->getThemeRelations();
23
        foreach ($otherRelations as $otherRelation) {
24
            if (strpos($otherRelation['target'], 'http://') !== 0) {
25
                $this->getZip()->addFromString($this->absoluteZipPath('ppt/theme/' . $otherRelation['target']), $otherRelation['contents']);
26
            }
27
        }
28
        */
29
30 95
        return $this->getZip();
31
    }
32
33
34
    /**
35
     * Write theme to XML format
36
     *
37
     * @param  Slide\SlideMaster $oMasterSlide
38
     * @return string XML Output
39
     */
40 95
    protected function writeTheme(Slide\SlideMaster $oMasterSlide)
41
    {
42
        // Create XML writer
43 95
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
44
45 95
        $name = 'Theme'.rand(1, 100);
46
47
        // XML header
48 95
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
49
50
        // a:theme
51 95
        $objWriter->startElement('a:theme');
52 95
        $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
53 95
        $objWriter->writeAttribute('name', $name);
54
55
        // a:theme/a:themeElements
56 95
        $objWriter->startElement('a:themeElements');
57
58
        // a:theme/a:themeElements/a:clrScheme
59 95
        $objWriter->startElement('a:clrScheme');
60 95
        $objWriter->writeAttribute('name', $name);
61
62 95
        foreach ($oMasterSlide->getAllSchemeColors() as $oSchemeColor) {
63
            // a:theme/a:themeElements/a:clrScheme/a:*
64
            $objWriter->startElement('a:'.$oSchemeColor->getValue());
65
66
            if (in_array($oSchemeColor->getValue(), array(
67
                'dk1', 'lt1'
68
            ))) {
69
                $objWriter->startElement('a:sysClr');
70
                $objWriter->writeAttribute('val', ($oSchemeColor->getValue() == 'dk1' ? 'windowText' : 'window'));
71
                $objWriter->writeAttribute('lastClr', $oSchemeColor->getRGB());
72
                $objWriter->endElement();
73
            } else {
74
                $objWriter->startElement('a:srgbClr');
75
                $objWriter->writeAttribute('val', $oSchemeColor->getRGB());
76
                $objWriter->endElement();
77
            }
78
79
            // a:theme/a:themeElements/a:clrScheme/a:*/
80
            $objWriter->endElement();
81 95
        }
82
83
        // a:theme/a:themeElements/a:clrScheme/
84 95
        $objWriter->endElement();
85
86
        // a:theme/a:themeElements/a:fmtScheme
87 95
        $objWriter->startElement('a:fmtScheme');
88 95
        $objWriter->writeAttribute('name', $name);
89
90
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst
91 95
        $objWriter->startElement('a:bgFillStyleLst');
92
93
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/a:solidFill
94 95
        $objWriter->startElement('a:solidFill');
95
96
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/a:solidFill/a:schemeClr
97 95
        $objWriter->startElement('a:schemeClr');
98 95
        $objWriter->writeAttribute('val', 'phClr');
99
100
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/a:solidFill/a:schemeClr/
101 95
        $objWriter->endElement();
102
103
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/a:solidFill/
104 95
        $objWriter->endElement();
105
106
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/
107 95
        $objWriter->endElement();
108
109
        // a:theme/a:themeElements/a:fmtScheme/
110 95
        $objWriter->endElement();
111
112
        // a:theme/a:themeElements/
113 95
        $objWriter->endElement();
114
115
        // a:theme/a:themeElements
116 95
        $objWriter->writeElement('a:objectDefaults');
117
118
        // a:theme/a:extraClrSchemeLst
119 95
        $objWriter->writeElement('a:extraClrSchemeLst');
120
121
        // a:theme/
122 95
        $objWriter->endElement();
123
124
        // Return
125 95
        return $objWriter->getData();
126
    }
127
128
129
    /**
130
     * Write theme relationships to XML format
131
     *
132
     * @param  int       $masterId Master slide id
133
     * @return string    XML Output
134
     * @throws \Exception
135
     */
136 95
    public function writeThemeRelationships($masterId = 1)
137
    {
138
        // Create XML writer
139 95
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
140
141
        // XML header
142 95
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
143
144
        // Relationships
145 95
        $objWriter->startElement('Relationships');
146 95
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
147
148
        // Other relationships
149
        //@todo
150
        $masterId;
151
        /*
152
        $otherRelations = $oLayoutPack->getThemeRelations();
153
        foreach ($otherRelations as $otherRelation) {
154
            if ($otherRelation['masterid'] == $masterId) {
155
                $this->writeRelationship($objWriter, $otherRelation['id'], $otherRelation['type'], $otherRelation['target']);
156
            }
157
        }
158
        */
159 95
        $objWriter->endElement();
160
161
        // Return
162 95
        return $objWriter->getData();
163
    }
164
}
165