Completed
Pull Request — develop (#188)
by Franck
06:55
created

Serialized::save()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 32
ccs 16
cts 16
cp 1
rs 8.439
cc 6
eloc 15
nc 6
nop 1
crap 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\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
     * Private PhpPresentation
32
     *
33
     * @var \PhpOffice\PhpPresentation\PhpPresentation
34
     */
35
    private $presentation;
36
37
    /**
38
     * Create a new \PhpOffice\PhpPresentation\Writer\Serialized
39
     *
40
     * @param \PhpOffice\PhpPresentation\PhpPresentation $pPhpPresentation
41
     */
42 7
    public function __construct(PhpPresentation $pPhpPresentation = null)
43
    {
44
        // Set PhpPresentation
45 7
        $this->setPhpPresentation($pPhpPresentation);
46
47
        // Set ZIP Adapter
48 7
        $this->setZipAdapter(new ZipArchiveAdapter());
49 7
    }
50
51
    /**
52
     * Save PhpPresentation to file
53
     *
54
     * @param  string    $pFilename
55
     * @throws \Exception
56
     */
57 5
    public function save($pFilename)
58
    {
59 5
        if (empty($pFilename)) {
60 1
            throw new \Exception("Filename is empty.");
61
        }
62 4
        if (is_null($this->presentation)) {
63 1
            throw new \Exception("PhpPresentation object unassigned.");
64
        }
65
66
        // Create new ZIP file and open it for writing
67 3
        $objZip = $this->getZipAdapter();
68
69
        // Try opening the ZIP file
70 3
        $objZip->open($pFilename);
71
72
        // Add media
73 2
        $slideCount = $this->presentation->getSlideCount();
74 2
        for ($i = 0; $i < $slideCount; ++$i) {
75 2
            for ($j = 0; $j < $this->presentation->getSlide($i)->getShapeCollection()->count(); ++$j) {
76 2
                if ($this->presentation->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawing) {
77 2
                    $imgTemp = $this->presentation->getSlide($i)->getShapeCollection()->offsetGet($j);
78 2
                    $objZip->addFromString('media/' . $imgTemp->getFilename(), file_get_contents($imgTemp->getPath()));
79
                }
80
            }
81
        }
82
83
        // Add PhpPresentation.xml to the document, which represents a PHP serialized PhpPresentation object
84 2
        $objZip->addFromString('PhpPresentation.xml', $this->writeSerialized($this->presentation, $pFilename));
85
86
        // Close file
87 2
        $objZip->close();
88 2
    }
89
90
    /**
91
     * Get PhpPresentation object
92
     *
93
     * @return PhpPresentation
94
     * @throws \Exception
95
     */
96 2
    public function getPhpPresentation()
97
    {
98 2
        if (!is_null($this->presentation)) {
99 1
            return $this->presentation;
100
        } else {
101 1
            throw new \Exception("No PhpPresentation assigned.");
102
        }
103
    }
104
105
    /**
106
     * Get PhpPresentation object
107
     *
108
     * @param  PhpPresentation                   $pPhpPresentation PhpPresentation object
109
     * @throws \Exception
110
     * @return \PhpOffice\PhpPresentation\Writer\Serialized
111
     */
112 7
    public function setPhpPresentation(PhpPresentation $pPhpPresentation = null)
113
    {
114 7
        $this->presentation = $pPhpPresentation;
115
116 7
        return $this;
117
    }
118
119
    /**
120
     * Serialize PhpPresentation object to XML
121
     *
122
     * @param  PhpPresentation $pPhpPresentation
123
     * @param  string        $pFilename
124
     * @return string        XML Output
125
     * @throws \Exception
126
     */
127 2
    private function writeSerialized(PhpPresentation $pPhpPresentation = null, $pFilename = '')
128
    {
129
        // Clone $pPhpPresentation
130 2
        $pPhpPresentation = clone $pPhpPresentation;
131
132
        // Update media links
133 2
        $slideCount = $pPhpPresentation->getSlideCount();
134 2
        for ($i = 0; $i < $slideCount; ++$i) {
135 2
            for ($j = 0; $j < $pPhpPresentation->getSlide($i)->getShapeCollection()->count(); ++$j) {
136 2
                if ($pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j) instanceof AbstractDrawing) {
137 2
                    $pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j)->setPath('zip://' . $pFilename . '#media/' . $pPhpPresentation->getSlide($i)->getShapeCollection()->offsetGet($j)->getFilename(), false);
138
                }
139
            }
140
        }
141
142
        // Create XML writer
143 2
        $objWriter = new XMLWriter();
144 2
        $objWriter->openMemory();
145 2
        $objWriter->setIndent(true);
146
147
        // XML header
148 2
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
149
150
        // PhpPresentation
151 2
        $objWriter->startElement('PhpPresentation');
152 2
        $objWriter->writeAttribute('version', '##VERSION##');
153
154
        // Comment
155 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.');
156
157
        // Data
158 2
        $objWriter->startElement('data');
159 2
        $objWriter->writeCData(base64_encode(serialize($pPhpPresentation)));
160 2
        $objWriter->endElement();
161
162 2
        $objWriter->endElement();
163
164
        // Return
165 2
        return $objWriter->getData();
166
    }
167
}
168