Completed
Pull Request — develop (#207)
by Franck
06:53
created

Serialized::save()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 30
ccs 13
cts 15
cp 0.8667
rs 8.439
cc 5
eloc 14
nc 5
nop 1
crap 5.0592
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;
19
20
use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter;
21
use PhpOffice\Common\XMLWriter;
22
use PhpOffice\PhpPresentation\PhpPresentation;
23
use PhpOffice\PhpPresentation\Shape\AbstractDrawing;
24
25
/**
26
 * \PhpOffice\PhpPresentation\Writer\Serialized
27
 */
28
class Serialized extends AbstractWriter implements WriterInterface
29
{
30
    /**
31
     * Create a new \PhpOffice\PhpPresentation\Writer\Serialized
32
     *
33
     * @param \PhpOffice\PhpPresentation\PhpPresentation $pPhpPresentation
34
     */
35 7
    public function __construct(PhpPresentation $pPhpPresentation = null)
36
    {
37
        // Set PhpPresentation
38 7
        $this->setPhpPresentation($pPhpPresentation);
39
40
        // Set ZIP Adapter
41 7
        $this->setZipAdapter(new ZipArchiveAdapter());
42 7
    }
43
44
    /**
45
     * Save PhpPresentation to file
46
     *
47
     * @param  string    $pFilename
48
     * @throws \Exception
49
     */
50 5
    public function save($pFilename)
51
    {
52 5
        if (empty($pFilename)) {
53 1
            throw new \Exception("Filename is empty.");
54
        }
55 4
        $oPresentation = $this->getPhpPresentation();
56
57
        // Create new ZIP file and open it for writing
58 3
        $objZip = $this->getZipAdapter();
59
60
        // Try opening the ZIP file
61 3
        $objZip->open($pFilename);
62
63
        // Add media
64 2
        $slideCount = $oPresentation->getSlideCount();
65 2
        for ($i = 0; $i < $slideCount; ++$i) {
66 2
            for ($j = 0; $j < $oPresentation->getSlide($i)->getShapeCollection()->count(); ++$j) {
67 2
                if ($oPresentation->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...
68
                    $imgTemp = $oPresentation->getSlide($i)->getShapeCollection()->offsetGet($j);
69
                    $objZip->addFromString('media/' . $imgTemp->getFilename(), file_get_contents($imgTemp->getPath()));
70
                }
71
            }
72
        }
73
74
        // Add PhpPresentation.xml to the document, which represents a PHP serialized PhpPresentation object
75 2
        $objZip->addFromString('PhpPresentation.xml', $this->writeSerialized($oPresentation, $pFilename));
76
77
        // Close file
78 2
        $objZip->close();
79 2
    }
80
81
    /**
82
     * Serialize PhpPresentation object to XML
83
     *
84
     * @param  PhpPresentation $pPhpPresentation
85
     * @param  string        $pFilename
86
     * @return string        XML Output
87
     * @throws \Exception
88
     */
89 2
    private function writeSerialized(PhpPresentation $pPhpPresentation = null, $pFilename = '')
90
    {
91
        // Clone $pPhpPresentation
92 2
        $pPhpPresentation = clone $pPhpPresentation;
93
94
        // Update media links
95 2
        $slideCount = $pPhpPresentation->getSlideCount();
96 2
        for ($i = 0; $i < $slideCount; ++$i) {
97 2
            for ($j = 0; $j < $pPhpPresentation->getSlide($i)->getShapeCollection()->count(); ++$j) {
98 2
                if ($pPhpPresentation->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...
99
                    $pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j)->setPath('zip://' . $pFilename . '#media/' . $pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j)->getFilename(), false);
100
                }
101
            }
102
        }
103
104
        // Create XML writer
105 2
        $objWriter = new XMLWriter();
106 2
        $objWriter->openMemory();
107 2
        $objWriter->setIndent(true);
108
109
        // XML header
110 2
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
111
112
        // PhpPresentation
113 2
        $objWriter->startElement('PhpPresentation');
114 2
        $objWriter->writeAttribute('version', '##VERSION##');
115
116
        // Comment
117 2
        $objWriter->writeComment('This file has been generated using PhpPresentation v##VERSION## (http://github.com/PHPOffice/PhpPresentation). It contains a base64 encoded serialized version of the PhpPresentation internal object.');
118
119
        // Data
120 2
        $objWriter->startElement('data');
121 2
        $objWriter->writeCData(base64_encode(serialize($pPhpPresentation)));
122 2
        $objWriter->endElement();
123
124 2
        $objWriter->endElement();
125
126
        // Return
127 2
        return $objWriter->getData();
128
    }
129
}
130