Completed
Push — develop ( f5141e...bd86d7 )
by Franck
18:00
created

PptTheme   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 82.76%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 83
ccs 24
cts 29
cp 0.8276
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 20 4
A writeTheme() 0 10 3
B writeThemeRelationships() 0 28 3
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\PackDefault;
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 94
    public function render()
15
    {
16 94
        $oLayoutPack = new PackDefault();
17
18 94
        $masterSlides = $oLayoutPack->getMasterSlides();
19 94
        foreach ($masterSlides as $masterSlide) {
20
            // Add themes to ZIP file
21 94
            $this->getZip()->addFromString('ppt/theme/_rels/theme' . $masterSlide['masterid'] . '.xml.rels', $this->writeThemeRelationships($masterSlide['masterid']));
22 94
            $this->getZip()->addFromString('ppt/theme/theme' . $masterSlide['masterid'] . '.xml', $this->writeTheme($masterSlide['masterid']));
23
        }
24
25 94
        $otherRelations = $oLayoutPack->getThemeRelations();
26 94
        foreach ($otherRelations as $otherRelation) {
27
            if (strpos($otherRelation['target'], 'http://') !== 0) {
28
                $this->getZip()->addFromString($this->absoluteZipPath('ppt/theme/' . $otherRelation['target']), $otherRelation['contents']);
29
            }
30
        }
31
32 94
        return $this->getZip();
33
    }
34
35
36
    /**
37
     * Write theme to XML format
38
     *
39
     * @param  int $masterId
40
     * @throws \Exception
41
     * @return string XML Output
42
     */
43 94
    public function writeTheme($masterId = 1)
44
    {
45 94
        $oLayoutPack = new PackDefault();
46 94
        foreach ($oLayoutPack->getThemes() as $theme) {
47 94
            if ($theme['masterid'] == $masterId) {
48 94
                return $theme['body'];
49
            }
50
        }
51
        throw new \Exception('No theme has been found!');
52
    }
53
54
55
    /**
56
     * Write theme relationships to XML format
57
     *
58
     * @param  int       $masterId Master slide id
59
     * @return string    XML Output
60
     * @throws \Exception
61
     */
62 94
    public function writeThemeRelationships($masterId = 1)
63
    {
64
        // Create XML writer
65 94
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
66
67
        // Layout pack
68 94
        $oLayoutPack = new PackDefault();
69
70
        // XML header
71 94
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
72
73
        // Relationships
74 94
        $objWriter->startElement('Relationships');
75 94
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
76
77
        // Other relationships
78 94
        $otherRelations = $oLayoutPack->getThemeRelations();
79 94
        foreach ($otherRelations as $otherRelation) {
80
            if ($otherRelation['masterid'] == $masterId) {
81
                $this->writeRelationship($objWriter, $otherRelation['id'], $otherRelation['type'], $otherRelation['target']);
82
            }
83
        }
84
85 94
        $objWriter->endElement();
86
87
        // Return
88 94
        return $objWriter->getData();
89
    }
90
}
91