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

PptTheme   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 77.78%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
c 3
b 0
f 0
lcom 1
cbo 6
dl 0
loc 155
ccs 28
cts 36
cp 0.7778
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 18 2
B writeTheme() 0 87 4
B writeThemeRelationships() 0 26 1
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
            $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 95
        /*
22 95
        $otherRelations = $oLayoutPack->getThemeRelations();
23 95
        foreach ($otherRelations as $otherRelation) {
24
            if (strpos($otherRelation['target'], 'http://') !== 0) {
25 95
                $this->getZip()->addFromString($this->absoluteZipPath('ppt/theme/' . $otherRelation['target']), $otherRelation['contents']);
26 95
            }
27
        }
28
        */
29
30 95
        return $this->getZip();
31
    }
32 95
33
34
    /**
35
     * Write theme to XML format
36
     *
37
     * @param  Slide\SlideMaster $oMasterSlide
38
     * @return string XML Output
39
     */
40
    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 95
47 95
        // XML header
48 95
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
49
50
        // a:theme
51
        $objWriter->startElement('a:theme');
52
        $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
53
        $objWriter->writeAttribute('name', $name);
54
55
        // a:theme/a:themeElements
56
        $objWriter->startElement('a:themeElements');
57
58
        // a:theme/a:themeElements/a:clrScheme
59
        $objWriter->startElement('a:clrScheme');
60
        $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 95
66
            if (in_array($oSchemeColor->getValue(), array(
67
                'dk1', 'lt1'
68 95
            ))) {
69
                $objWriter->startElement('a:sysClr');
70
                $objWriter->writeAttribute('val', ($oSchemeColor->getValue() == 'dk1' ? 'windowText' : 'window'));
71 95
                $objWriter->writeAttribute('lastClr', $oSchemeColor->getRGB());
72
                $objWriter->endElement();
73
            } else {
74 95
                $objWriter->startElement('a:srgbClr');
75 95
                $objWriter->writeAttribute('val', $oSchemeColor->getRGB());
76
                $objWriter->endElement();
77
            }
78 95
79 95
            // a:theme/a:themeElements/a:clrScheme/a:*/
80
            $objWriter->endElement();
81
        }
82
83 95
        // a:theme/a:themeElements/a:clrScheme/
84
        $objWriter->endElement();
85 95
86
        // a:theme/a:themeElements/a:fmtScheme
87
        $objWriter->startElement('a:fmtScheme');
88 95
        $objWriter->writeAttribute('name', $name);
89
90
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst
91 1
        $objWriter->startElement('a:bgFillStyleLst');
92
93
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/a:solidFill
94
        $objWriter->startElement('a:solidFill');
95
96
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/a:solidFill/a:schemeClr
97
        $objWriter->startElement('a:schemeClr');
98
        $objWriter->writeAttribute('val', 'phClr');
99
100
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/a:solidFill/a:schemeClr/
101
        $objWriter->endElement();
102
103
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/a:solidFill/
104
        $objWriter->endElement();
105
106
        // a:theme/a:themeElements/a:fmtScheme/a:bgFillStyleLst/
107
        $objWriter->endElement();
108
109
        // a:theme/a:themeElements/a:fmtScheme/
110
        $objWriter->endElement();
111
112
        // a:theme/a:themeElements/
113
        $objWriter->endElement();
114
115
        // a:theme/a:themeElements
116
        $objWriter->writeElement('a:objectDefaults');
117
118
        // a:theme/a:extraClrSchemeLst
119
        $objWriter->writeElement('a:extraClrSchemeLst');
120
121
        // a:theme/
122
        $objWriter->endElement();
123
124
        // Return
125
        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
    public function writeThemeRelationships($masterId = 1)
0 ignored issues
show
Unused Code introduced by
The parameter $masterId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        // Create XML writer
139
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
140
141
        // XML header
142
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
143
144
        // Relationships
145
        $objWriter->startElement('Relationships');
146
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
147
148
        // Other relationships
149
        /*
150
        $otherRelations = $oLayoutPack->getThemeRelations();
151
        foreach ($otherRelations as $otherRelation) {
152
            if ($otherRelation['masterid'] == $masterId) {
153
                $this->writeRelationship($objWriter, $otherRelation['id'], $otherRelation['type'], $otherRelation['target']);
154
            }
155
        }
156
        */
157
        $objWriter->endElement();
158
159
        // Return
160
        return $objWriter->getData();
161
    }
162
}
163