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

ODPresentation::setUseDiskCaching()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 3
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 62
    public function __construct(PhpPresentation $pPhpPresentation = null)
58
    {
59
        // Assign PhpPresentation
60 62
        $this->setPhpPresentation($pPhpPresentation);
61
62
        // Set up disk caching location
63 62
        $this->diskCachingDirectory = './';
64
65
        // Set HashTable variables
66 62
        $this->oDrawingHashTable = new HashTable();
67
68 62
        $this->setZipAdapter(new ZipArchiveAdapter());
69 62
    }
70
71
    /**
72
     * Save PhpPresentation to file
73
     *
74
     * @param  string    $pFilename
75
     * @throws \Exception
76
     */
77 58
    public function save($pFilename)
78
    {
79 58
        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 57
        $originalFilename = $pFilename;
84 57
        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 57
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
93
94
        // Initialize Zip
95 57
        $oZip = $this->getZipAdapter();
96 57
        $oZip->open($pFilename);
97
98
        // Variables
99 57
        $oPresentation = $this->getPhpPresentation();
100 57
        $arrayChart = array();
101
102 57
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation');
103 57
        foreach ($oDir as $oFile) {
104 57
            echo "\n".$this->path."/".$oFile->getBasename('.php')."\n";
0 ignored issues
show
Bug introduced by
The property path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
105
            if (!$oFile->isFile()) {
106
                continue;
107
            }
108
            $class = __NAMESPACE__.'\\ODPresentation\\'.$oFile->getBasename('.php');
109
            $o = new \ReflectionClass($class);
110
111
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) {
112
                continue;
113
            }
114
            $oService = $o->newInstance();
115
            $oService->setZip($oZip);
116
            $oService->setPresentation($oPresentation);
117
            $oService->setDrawingHashTable($this->getDrawingHashTable());
118
            $oService->setArrayChart($arrayChart);
119
            $oZip = $oService->render();
120
            $arrayChart = $oService->getArrayChart();
121
            unset($oService);
122
        }
123
124
        // Close file
125
        $oZip->close();
126
127
        // If a temporary file was used, copy it to the correct file stream
128
        if ($originalFilename != $pFilename) {
129
            if (copy($pFilename, $originalFilename) === false) {
130
                throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
131
            }
132
            if (@unlink($pFilename) === false) {
133
                throw new \Exception('The file ' . $pFilename . ' could not be removed.');
134
            }
135
        }
136
    }
137
138
    /**
139
     * Get use disk caching where possible?
140
     *
141
     * @return boolean
142
     */
143 1
    public function hasDiskCaching()
144
    {
145 1
        return $this->useDiskCaching;
146
    }
147
148
    /**
149
     * Set use disk caching where possible?
150
     *
151
     * @param  boolean $pValue
152
     * @param  string $pDirectory Disk caching directory
153
     * @throws \Exception
154
     * @return \PhpOffice\PhpPresentation\Writer\ODPresentation
155
     */
156 2
    public function setUseDiskCaching($pValue = false, $pDirectory = null)
157
    {
158 2
        $this->useDiskCaching = $pValue;
159
160 2
        if (!is_null($pDirectory)) {
161 2
            if (is_dir($pDirectory)) {
162 1
                $this->diskCachingDirectory = $pDirectory;
163
            } else {
164 1
                throw new \Exception("Directory does not exist: $pDirectory");
165
            }
166
        }
167
168 1
        return $this;
169
    }
170
171
    /**
172
     * Get disk caching directory
173
     *
174
     * @return string
175
     */
176 2
    public function getDiskCachingDirectory()
177
    {
178 2
        return $this->diskCachingDirectory;
179
    }
180
181
    /**
182
     * Get an array of all drawings
183
     *
184
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing[] All drawings in PhpPresentation
185
     * @throws \Exception
186
     */
187 57
    protected function allDrawings()
188
    {
189
        // Get an array of all drawings
190 57
        $aDrawings  = array();
191
192
        // Loop trough PhpPresentation
193 57
        $slideCount = $this->getPhpPresentation()->getSlideCount();
194 57
        for ($i = 0; $i < $slideCount; ++$i) {
195
            // Loop trough images and add to array
196 57
            $iterator = $this->getPhpPresentation()->getSlide($i)->getShapeCollection()->getIterator();
197 57
            while ($iterator->valid()) {
198 50
                if ($iterator->current() instanceof AbstractDrawing && !($iterator->current() instanceof Table)) {
199 28
                    $aDrawings[] = $iterator->current();
200 23
                } elseif ($iterator->current() instanceof Group) {
201 1
                    $iterator2 = $iterator->current()->getShapeCollection()->getIterator();
202 1
                    while ($iterator2->valid()) {
203 1
                        if ($iterator2->current() instanceof AbstractDrawing && !($iterator2->current() instanceof Table)) {
204 1
                            $aDrawings[] = $iterator2->current();
205
                        }
206 1
                        $iterator2->next();
207
                    }
208
                }
209
210 50
                $iterator->next();
211
            }
212
        }
213
214 57
        return $aDrawings;
215
    }
216
}
217