Completed
Pull Request — develop (#322)
by Thomas
08:13 queued 03:00
created

Serialized   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 87
ccs 24
cts 30
cp 0.8
rs 10

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\PhpPresentation\Shape\AbstractDrawing;
21
use PhpOffice\Common\File;
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 AbstractDrawing) {
0 ignored issues
show
Bug introduced by
The class PhpOffice\PhpPresentation\Shape\AbstractDrawing does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
100
                            $file->getSlide($i)->getShapeCollection()->offsetGet($j)->setPath('zip://' . $pFilename . '#media/' . $file->getSlide($i)->getShapeCollection()->offsetGet($j)->getFilename(), false);
101
                        }
102
                    }
103
                }
104
105 1
                $oArchive->close();
106 1
                return $file;
107
            }
108 1
        }
109
110 1
        return null;
111
    }
112
}
113