Completed
Pull Request — develop (#209)
by Franck
07:39
created

ODPresentation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
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\PhpPresentation\HashTable;
22
use PhpOffice\PhpPresentation\PhpPresentation;
23
use PhpOffice\PhpPresentation\Shape\AbstractDrawing;
24
use PhpOffice\PhpPresentation\Shape\Group;
25
use PhpOffice\PhpPresentation\Shape\Table;
26
use DirectoryIterator;
27
28
/**
29
 * ODPresentation writer
30
 */
31
class ODPresentation extends AbstractWriter implements WriterInterface
32
{
33
    /**
34
     * @var \PhpOffice\PhpPresentation\Shape\Chart[]
35
     */
36
    public $chartArray = array();
37
38
    /**
39
    * Use disk caching where possible?
40
    *
41
    * @var boolean
42
    */
43
    private $useDiskCaching = false;
44
45
    /**
46
     * Disk caching directory
47
     *
48
     * @var string
49
     */
50
    private $diskCachingDirectory;
51
52
    /**
53
     * Create a new \PhpOffice\PhpPresentation\Writer\ODPresentation
54
     *
55
     * @param PhpPresentation $pPhpPresentation
56
     */
57 63
    public function __construct(PhpPresentation $pPhpPresentation = null)
58
    {
59
        // Assign PhpPresentation
60 63
        $this->setPhpPresentation($pPhpPresentation);
61
62
        // Set up disk caching location
63 63
        $this->diskCachingDirectory = './';
64
65
        // Set HashTable variables
66 63
        $this->oDrawingHashTable = new HashTable();
67
68 63
        $this->setZipAdapter(new ZipArchiveAdapter());
69 63
    }
70
71
    /**
72
     * Save PhpPresentation to file
73
     *
74
     * @param  string    $pFilename
75
     * @throws \Exception
76
     */
77 59
    public function save($pFilename)
78
    {
79 59
        if (empty($pFilename)) {
80 1
            throw new \Exception("Filename is empty");
81
        }
82
        // If $pFilename is php://output or php://stdout, make it a temporary file...
83 58
        $originalFilename = $pFilename;
84 58
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
85
            $pFilename = @tempnam('./', 'phppttmp');
86
            if ($pFilename == '') {
87
                $pFilename = $originalFilename;
88
            }
89
        }
90
91
        // Initialize HashTable
92 58
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
93
94
        // Initialize Zip
95 58
        $oZip = $this->getZipAdapter();
96 58
        $oZip->open($pFilename);
97
98
        // Variables
99 58
        $oPresentation = $this->getPhpPresentation();
100 58
        $arrayChart = array();
101
102 58
        $arrayFiles = array();
103 58
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation');
104 58
        foreach ($oDir as $oFile) {
105 58
            if (!$oFile->isFile()) {
106 58
                continue;
107
            }
108
109 58
            $class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php');
110 58
            $o = new \ReflectionClass($class);
111
112 58
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) {
113 58
                continue;
114
            }
115 58
            $arrayFiles[$oFile->getBasename('.php')] = $o;
116
        }
117
118 58
        ksort($arrayFiles);
119
120 58
        foreach ($arrayFiles as $o) {
121 58
            $oService = $o->newInstance();
122 58
            $oService->setZip($oZip);
123 58
            $oService->setPresentation($oPresentation);
124 58
            $oService->setDrawingHashTable($this->getDrawingHashTable());
125 58
            $oService->setArrayChart($arrayChart);
126 58
            $oZip = $oService->render();
127 58
            $arrayChart = $oService->getArrayChart();
128 58
            unset($oService);
129
        }
130
131
        // Close file
132 56
        $oZip->close();
133
134
        // If a temporary file was used, copy it to the correct file stream
135 56
        if ($originalFilename != $pFilename) {
136
            if (copy($pFilename, $originalFilename) === false) {
137
                throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
138
            }
139
            if (@unlink($pFilename) === false) {
140
                throw new \Exception('The file ' . $pFilename . ' could not be removed.');
141
            }
142
        }
143 56
    }
144
145
    /**
146
     * Get use disk caching where possible?
147
     *
148
     * @return boolean
149
     */
150 1
    public function hasDiskCaching()
151
    {
152 1
        return $this->useDiskCaching;
153
    }
154
155
    /**
156
     * Set use disk caching where possible?
157
     *
158
     * @param  boolean $pValue
159
     * @param  string $pDirectory Disk caching directory
160
     * @throws \Exception
161
     * @return \PhpOffice\PhpPresentation\Writer\ODPresentation
162
     */
163 2
    public function setUseDiskCaching($pValue = false, $pDirectory = null)
164
    {
165 2
        $this->useDiskCaching = $pValue;
166
167 2
        if (!is_null($pDirectory)) {
168 2
            if (is_dir($pDirectory)) {
169 1
                $this->diskCachingDirectory = $pDirectory;
170
            } else {
171 1
                throw new \Exception("Directory does not exist: $pDirectory");
172
            }
173
        }
174
175 1
        return $this;
176
    }
177
178
    /**
179
     * Get disk caching directory
180
     *
181
     * @return string
182
     */
183 2
    public function getDiskCachingDirectory()
184
    {
185 2
        return $this->diskCachingDirectory;
186
    }
187
188
    /**
189
     * Get an array of all drawings
190
     *
191
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing[] All drawings in PhpPresentation
192
     * @throws \Exception
193
     */
194 58
    protected function allDrawings()
195
    {
196
        // Get an array of all drawings
197 58
        $aDrawings  = array();
198
199
        // Loop trough PhpPresentation
200 58
        $slideCount = $this->getPhpPresentation()->getSlideCount();
201 58
        for ($i = 0; $i < $slideCount; ++$i) {
202
            // Loop trough images and add to array
203 58
            $iterator = $this->getPhpPresentation()->getSlide($i)->getShapeCollection()->getIterator();
204 58
            while ($iterator->valid()) {
205 51
                if ($iterator->current() instanceof AbstractDrawing && !($iterator->current() instanceof Table)) {
206 29
                    $aDrawings[] = $iterator->current();
207 23
                } elseif ($iterator->current() instanceof Group) {
208 1
                    $iterator2 = $iterator->current()->getShapeCollection()->getIterator();
209 1
                    while ($iterator2->valid()) {
210 1
                        if ($iterator2->current() instanceof AbstractDrawing && !($iterator2->current() instanceof Table)) {
211 1
                            $aDrawings[] = $iterator2->current();
212
                        }
213 1
                        $iterator2->next();
214
                    }
215
                }
216
217 51
                $iterator->next();
218
            }
219
        }
220
221 58
        return $aDrawings;
222
    }
223
}
224