ODPresentation::setUseDiskCaching()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
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\Table;
25
use DirectoryIterator;
26
27
/**
28
 * ODPresentation writer
29
 */
30
class ODPresentation extends AbstractWriter implements WriterInterface
31
{
32
    /**
33
     * @var \PhpOffice\PhpPresentation\Shape\Chart[]
34
     */
35
    public $chartArray = array();
36
37
    /**
38
    * Use disk caching where possible?
39
    *
40
    * @var boolean
41
    */
42
    private $useDiskCaching = false;
43
44
    /**
45
     * Disk caching directory
46
     *
47
     * @var string
48
     */
49
    private $diskCachingDirectory;
50
51
    /**
52
     * Create a new \PhpOffice\PhpPresentation\Writer\ODPresentation
53
     *
54
     * @param PhpPresentation $pPhpPresentation
55
     * @throws \Exception
56
     */
57 69
    public function __construct(PhpPresentation $pPhpPresentation = null)
58
    {
59
        // Assign PhpPresentation
60 69
        $this->setPhpPresentation($pPhpPresentation);
61
62
        // Set up disk caching location
63 69
        $this->diskCachingDirectory = './';
64
65
        // Set HashTable variables
66 69
        $this->oDrawingHashTable = new HashTable();
67
68 69
        $this->setZipAdapter(new ZipArchiveAdapter());
69 69
    }
70
71
    /**
72
     * Save PhpPresentation to file
73
     *
74
     * @param  string    $pFilename
75
     * @throws \Exception
76
     */
77 65
    public function save($pFilename)
78
    {
79 65
        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 64
        $originalFilename = $pFilename;
84 64
        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 64
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
93
94
        // Initialize Zip
95 64
        $oZip = $this->getZipAdapter();
96 64
        $oZip->open($pFilename);
97
98
        // Variables
99 64
        $oPresentation = $this->getPhpPresentation();
100 64
        $arrayChart = array();
101
102 64
        $arrayFiles = array();
103 64
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation');
104 64
        foreach ($oDir as $oFile) {
105 64
            if (!$oFile->isFile()) {
106 64
                continue;
107
            }
108
109 64
            $class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php');
110 64
            $class = new \ReflectionClass($class);
111
112 64
            if ($class->isAbstract() || !$class->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) {
113 64
                continue;
114
            }
115 64
            $arrayFiles[$oFile->getBasename('.php')] = $class;
116
        }
117
118 64
        ksort($arrayFiles);
119
120 64
        foreach ($arrayFiles as $o) {
121 64
            $oService = $o->newInstance();
122 64
            $oService->setZip($oZip);
123 64
            $oService->setPresentation($oPresentation);
124 64
            $oService->setDrawingHashTable($this->getDrawingHashTable());
125 64
            $oService->setArrayChart($arrayChart);
126 64
            $oZip = $oService->render();
127 64
            $arrayChart = $oService->getArrayChart();
128 64
            unset($oService);
129
        }
130
131
        // Close file
132 62
        $oZip->close();
133
134
        // If a temporary file was used, copy it to the correct file stream
135 62
        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 62
    }
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
                throw new \Exception("Directory does not exist: $pDirectory");
170
            }
171 1
            $this->diskCachingDirectory = $pDirectory;
172
        }
173
174 1
        return $this;
175
    }
176
177
    /**
178
     * Get disk caching directory
179
     *
180
     * @return string
181
     */
182 2
    public function getDiskCachingDirectory()
183
    {
184 2
        return $this->diskCachingDirectory;
185
    }
186
}
187