TemplateBased::__construct()   F
last analyzed

Complexity

Conditions 22
Paths 4

Size

Total Lines 127

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 39.9204

Importance

Changes 0
Metric Value
dl 0
loc 127
ccs 48
cts 72
cp 0.6667
rs 3.3333
c 0
b 0
f 0
cc 22
nc 4
nop 1
crap 39.9204

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\LayoutPack;
19
20
/**
21
 * \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\TemplateBased
22
 * @deprecated 0.7
23
 */
24
class TemplateBased extends AbstractLayoutPack
0 ignored issues
show
Deprecated Code introduced by
The class PhpOffice\PhpPresentatio...Pack\AbstractLayoutPack has been deprecated with message: 0.7

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
25
{
26
    /**
27
     * \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\TemplateBased
28
     *
29
     * @param string $fileName
30
     * @throws \Exception
31
     */
32 4
    public function __construct($fileName = '')
33
    {
34
        // Check if file exists
35 4
        if (!file_exists($fileName)) {
36
            throw new \Exception("Could not open " . $fileName . " for reading! File does not exist.");
37
        }
38
39
        // Master slide relations
40 4
        $this->masterSlideRels = array();
41
42
        // Theme relations
43 4
        $this->themeRelations = array();
44
45
        // Layout relations
46 4
        $this->layoutRelations = array();
47
48
        // Open package
49 4
        $package = new \ZipArchive;
50 4
        $package->open($fileName);
51
52
        // Read relations and search for officeDocument
53 4
        $relations = simplexml_load_string($package->getFromName("_rels/.rels"));
54 4
        foreach ($relations->Relationship as $rel) {
55 4
            if ($rel["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument") {
56
                // Found office document! Search for master slide...
57 4
                $presentationRels = simplexml_load_string($package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/_rels/" . basename($rel["Target"]) . ".rels")));
58 4
                foreach ($presentationRels->Relationship as $presRel) {
59 4
                    if ($presRel["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster") {
60
                        // Found slide master!
61 4
                        $slideMasterId         = str_replace('slideMaster', '', basename($presRel["Target"], '.xml'));
62 4
                        $this->masterSlides[] = array(
63 4
                            'masterid' => $slideMasterId,
64 4
                            'body' => $package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($presRel["Target"]) . "/" . basename($presRel["Target"])))
65
                        );
66
67
                        // Search for theme & slide layouts
68 4
                        $masterRelations = simplexml_load_string($package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($presRel["Target"]) . "/_rels/" . basename($presRel["Target"]) . ".rels")));
69 4
                        foreach ($masterRelations->Relationship as $masterRel) {
70 4
                            if ($masterRel["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme") {
71
                                // Found theme!
72 4
                                $themeId                     = str_replace('theme', '', basename($masterRel["Target"], '.xml'));
73 4
                                $this->themes[$themeId - 1] = array(
74 4
                                    'masterid' => $slideMasterId,
75 4
                                    'body' => $package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($presRel["Target"]) . "/" . dirname($masterRel["Target"]) . "/" . basename($masterRel["Target"])))
76
                                );
77
78
                                // Search for theme relations
79 4
                                $themeRelations = @simplexml_load_string($package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($presRel["Target"]) . "/" . dirname($masterRel["Target"]) . "/_rels/" . basename($masterRel["Target"]) . ".rels")));
80 4
                                if ($themeRelations && $themeRelations->Relationship) {
81 4
                                    foreach ($themeRelations->Relationship as $themeRel) {
82
                                        if ($themeRel["Type"] != "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster" && $themeRel["Type"] != "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" && $themeRel["Type"] != "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme") {
83
                                            // Theme relation
84
                                            $this->themeRelations[] = array(
85
                                                'masterid' => $slideMasterId,
86
                                                'id' => $themeRel["Id"],
87
                                                'type' => $themeRel["Type"],
88
                                                'contentType' => '',
89
                                                'target' => $themeRel["Target"],
90
                                                'contents' => $package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($presRel["Target"]) . "/" . dirname($masterRel["Target"]) . "/" . dirname($themeRel["Target"]) . "/" . basename($themeRel["Target"])))
91
                                            );
92
                                        }
93
                                    }
94
                                }
95 4
                            } elseif ($masterRel["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout") {
96
                                // Found slide layout!
97 4
                                $layoutId  = str_replace('slideLayout', '', basename($masterRel["Target"], '.xml'));
98
                                $layout    = array(
99 4
                                    'id'        => $layoutId,
100 4
                                    'masterid'  => $slideMasterId,
101 4
                                    'name'      => '-unknown-',
102 4
                                    'body'      => $package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($presRel["Target"]) . "/" . dirname($masterRel["Target"]) . "/" . basename($masterRel["Target"])))
103
                                );
104 4
                                if (utf8_encode(utf8_decode($layout['body'])) == $layout['body']) {
105 4
                                    $layoutXml = simplexml_load_string($layout['body']);
106
                                } else {
107
                                    $layoutXml = simplexml_load_string(utf8_encode($layout['body']));
108
                                }
109 4
                                $layoutXml->registerXPathNamespace("p", "http://schemas.openxmlformats.org/presentationml/2006/main");
110 4
                                $slide                     = $layoutXml->xpath('/p:sldLayout/p:cSld');
111 4
                                $layout['name']            = (string) $slide[0]['name'];
112 4
                                $this->layouts[$layoutId] = $layout;
113
114
                                // Search for slide layout relations
115 4
                                $layoutRelations = @simplexml_load_string($package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($presRel["Target"]) . "/" . dirname($masterRel["Target"]) . "/_rels/" . basename($masterRel["Target"]) . ".rels")));
116 4
                                if ($layoutRelations && $layoutRelations->Relationship) {
117 4
                                    foreach ($layoutRelations->Relationship as $layoutRel) {
118 4
                                        if ($layoutRel["Type"] != "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster" && $layoutRel["Type"] != "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" && $layoutRel["Type"] != "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme") {
119
                                            // Layout relation
120
                                            $this->layoutRelations[] = array(
121
                                                'layoutId' => $layoutId,
122
                                                'id' => $layoutRel["Id"],
123
                                                'type' => $layoutRel["Type"],
124
                                                'contentType' => '',
125
                                                'target' => $layoutRel["Target"],
126
                                                'contents' => $package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($presRel["Target"]) . "/" . dirname($masterRel["Target"]) . "/" . dirname($layoutRel["Target"]) . "/" . basename($layoutRel["Target"])))
127
                                            );
128
                                        }
129
                                    }
130
                                }
131
                            } else {
132
                                // Master slide relation
133
                                $this->masterSlideRels[] = array(
134
                                    'masterid' => $slideMasterId,
135
                                    'id' => $masterRel["Id"],
136
                                    'type' => $masterRel["Type"],
137
                                    'contentType' => '',
138
                                    'target' => $masterRel["Target"],
139
                                    'contents' => $package->getFromName($this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($presRel["Target"]) . "/" . dirname($masterRel["Target"]) . "/" . basename($masterRel["Target"])))
140
                                );
141
                            }
142
                        }
143
                    }
144
                }
145
146 4
                break;
147
            }
148
        }
149
150
        // Sort master slides
151 4
        usort($this->masterSlides, array(
152 4
            "\PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\TemplateBased",
153
            "cmpMaster"
154
        ));
155
156
        // Close package
157 4
        $package->close();
158 4
    }
159
160
    /**
161
     * Compare master slides
162
     *
163
     * @param array $firstSlide
164
     * @param array $secondSlide
165
     * @return int
166
     */
167
    public static function cmpMaster($firstSlide, $secondSlide)
168
    {
169
        if ($firstSlide['masterid'] == $secondSlide['masterid']) {
170
            return 0;
171
        }
172
173
        return ($firstSlide['masterid'] < $secondSlide['masterid']) ? -1 : 1;
174
    }
175
176
    /**
177
     * Determine absolute zip path
178
     *
179
     * @param  string $path
180
     * @return string
181
     */
182 4
    protected function absoluteZipPath($path)
183
    {
184 4
        $path      = str_replace(array(
185 4
            '/',
186
            '\\'
187 4
        ), DIRECTORY_SEPARATOR, $path);
188 4
        $parts     = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
189 4
        $absolutes = array();
190 4
        foreach ($parts as $part) {
191 4
            if ('.' == $part) {
192
                continue;
193
            }
194 4
            if ('..' == $part) {
195 4
                array_pop($absolutes);
196
            } else {
197 4
                $absolutes[] = $part;
198
            }
199
        }
200
201 4
        return implode('/', $absolutes);
202
    }
203
}
204