Completed
Pull Request — develop (#207)
by Franck
08:43 queued 01:46
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 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
            if (!$oFile->isFile()) {
105 57
                continue;
106
            }
107 57
            $class = __NAMESPACE__.'\\ODPresentation\\'.$oFile->getBasename('.php');
108 57
            $o = new \ReflectionClass($class);
109
110 57
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) {
111 57
                continue;
112
            }
113 57
            $oService = $o->newInstance();
114 57
            $oService->setZip($oZip);
115 57
            $oService->setPresentation($oPresentation);
116 57
            $oService->setDrawingHashTable($this->getDrawingHashTable());
117 57
            $oService->setArrayChart($arrayChart);
118 57
            $oZip = $oService->render();
119 57
            $arrayChart = $oService->getArrayChart();
120 57
            unset($oService);
121
        }
122
123
        // Close file
124 55
        $oZip->close();
125
126
        // If a temporary file was used, copy it to the correct file stream
127 55
        if ($originalFilename != $pFilename) {
128
            if (copy($pFilename, $originalFilename) === false) {
129
                throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
130
            }
131
            if (@unlink($pFilename) === false) {
132
                throw new \Exception('The file ' . $pFilename . ' could not be removed.');
133
            }
134
        }
135 55
    }
136
137
    /**
138
     * Get use disk caching where possible?
139
     *
140
     * @return boolean
141
     */
142 1
    public function hasDiskCaching()
143
    {
144 1
        return $this->useDiskCaching;
145
    }
146
147
    /**
148
     * Set use disk caching where possible?
149
     *
150
     * @param  boolean $pValue
151
     * @param  string $pDirectory Disk caching directory
152
     * @throws \Exception
153
     * @return \PhpOffice\PhpPresentation\Writer\ODPresentation
154
     */
155 2
    public function setUseDiskCaching($pValue = false, $pDirectory = null)
156
    {
157 2
        $this->useDiskCaching = $pValue;
158
159 2
        if (!is_null($pDirectory)) {
160 2
            if (is_dir($pDirectory)) {
161 1
                $this->diskCachingDirectory = $pDirectory;
162
            } else {
163 1
                throw new \Exception("Directory does not exist: $pDirectory");
164
            }
165
        }
166
167 1
        return $this;
168
    }
169
170
    /**
171
     * Get disk caching directory
172
     *
173
     * @return string
174
     */
175 2
    public function getDiskCachingDirectory()
176
    {
177 2
        return $this->diskCachingDirectory;
178
    }
179
}
180