Completed
Pull Request — master (#413)
by
unknown
07:08
created

Serialized   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 87
ccs 23
cts 26
cp 0.8846
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A canRead() 0 4 1
A fileSupportsUnserializePhpPresentation() 0 10 2
A load() 0 14 3
B loadSerialized() 0 26 6
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\Reader;
19
20
use PhpOffice\Common\File;
21
use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter;
22
23
/**
24
 * Serialized format reader
25
 */
26
class Serialized implements ReaderInterface
27
{
28
    /**
29
     * Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file?
30
     *
31
     * @param  string $pFilename
32
     * @throws \Exception
33
     * @return boolean
34
     */
35 3
    public function canRead($pFilename)
36
    {
37 3
        return $this->fileSupportsUnserializePhpPresentation($pFilename);
38
    }
39
40
    /**
41
     * Does a file support UnserializePhpPresentation ?
42
     *
43
     * @param  string    $pFilename
44
     * @throws \Exception
45
     * @return boolean
46
     */
47 6
    public function fileSupportsUnserializePhpPresentation($pFilename = '')
48
    {
49
        // Check if file exists
50 6
        if (!file_exists($pFilename)) {
51 1
            throw new \Exception("Could not open " . $pFilename . " for reading! File does not exist.");
52
        }
53
54
        // File exists, does it contain PhpPresentation.xml?
55 5
        return File::fileExists("zip://$pFilename#PhpPresentation.xml");
56
    }
57
58
    /**
59
     * Loads PhpPresentation Serialized file
60
     *
61
     * @param  string        $pFilename
62
     * @return \PhpOffice\PhpPresentation\PhpPresentation
63
     * @throws \Exception
64
     */
65 4
    public function load($pFilename)
66
    {
67
        // Check if file exists
68 4
        if (!file_exists($pFilename)) {
69 1
            throw new \Exception("Could not open " . $pFilename . " for reading! File does not exist.");
70
        }
71
72
        // Unserialize... First make sure the file supports it!
73 3
        if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) {
74 1
            throw new \Exception("Invalid file format for PhpOffice\PhpPresentation\Reader\Serialized: " . $pFilename . ".");
75
        }
76
77 2
        return $this->loadSerialized($pFilename);
78
    }
79
80
    /**
81
     * Load PhpPresentation Serialized file
82
     *
83
     * @param  string        $pFilename
84
     * @return \PhpOffice\PhpPresentation\PhpPresentation
85
     */
86 2
    private function loadSerialized($pFilename)
87
    {
88 2
        $oArchive = new \ZipArchive();
89 2
        if ($oArchive->open($pFilename) === true) {
90 2
            $xmlContent = $oArchive->getFromName('PhpPresentation.xml');
91
92 2
            if (!empty($xmlContent)) {
93 1
                $xmlData = simplexml_load_string($xmlContent);
94 1
                $file    = unserialize(base64_decode((string) $xmlData->data));
95
96
                // Update media links
97 1
                for ($i = 0; $i < $file->getSlideCount(); ++$i) {
98
                    for ($j = 0; $j < $file->getSlide($i)->getShapeCollection()->count(); ++$j) {
99
                        if ($file->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawingAdapter) {
100
                            $file->getSlide($i)->getShapeCollection()->offsetGet($j)->setPath('zip://' . $pFilename . '#media/' . $file->getSlide($i)->getShapeCollection()->offsetGet($j)->getIndexedFilename(), false);
101
                        }
102
                    }
103
                }
104
105 1
                $oArchive->close();
106 1
                return $file;
107
            }
108
        }
109
110 1
        return null;
111
    }
112
}
113