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

ODPresentation::allDrawings()   D

Complexity

Conditions 9
Paths 5

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 4.909
cc 9
eloc 16
nc 5
nop 0
crap 9
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 64
    public function __construct(PhpPresentation $pPhpPresentation = null)
58
    {
59
        // Assign PhpPresentation
60 64
        $this->setPhpPresentation($pPhpPresentation);
61
62
        // Set up disk caching location
63 64
        $this->diskCachingDirectory = './';
64
65
        // Set HashTable variables
66 64
        $this->oDrawingHashTable = new HashTable();
67
68 64
        $this->setZipAdapter(new ZipArchiveAdapter());
69 64
    }
70
71
    /**
72
     * Save PhpPresentation to file
73
     *
74
     * @param  string    $pFilename
75
     * @throws \Exception
76
     */
77 60
    public function save($pFilename)
78
    {
79 60
        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 59
        $originalFilename = $pFilename;
84 59
        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 59
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
93
94
        // Initialize Zip
95 59
        $oZip = $this->getZipAdapter();
96 59
        $oZip->open($pFilename);
97
98
        // Variables
99 59
        $oPresentation = $this->getPhpPresentation();
100 59
        $arrayChart = array();
101
102 59
        $arrayFiles = array();
103 59
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation');
104 59
        foreach ($oDir as $oFile) {
105 59
            if (!$oFile->isFile()) {
106 59
                continue;
107
            }
108
109 59
            $class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php');
110 59
            $o = new \ReflectionClass($class);
111
112 59
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) {
113 59
                continue;
114
            }
115 59
            $arrayFiles[$oFile->getBasename('.php')] = $o;
116
        }
117
118 59
        ksort($arrayFiles);
119
120 59
        foreach ($arrayFiles as $o) {
121 59
            $oService = $o->newInstance();
122 59
            $oService->setZip($oZip);
123 59
            $oService->setPresentation($oPresentation);
124 59
            $oService->setDrawingHashTable($this->getDrawingHashTable());
125 59
            $oService->setArrayChart($arrayChart);
126 59
            $oZip = $oService->render();
127 59
            $arrayChart = $oService->getArrayChart();
128 59
            unset($oService);
129
        }
130
131
        // Close file
132 57
        $oZip->close();
133
134
        // If a temporary file was used, copy it to the correct file stream
135 57
        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 57
    }
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