Completed
Push — develop ( 312b7a...53bbae )
by Franck
16s
created

ODPresentation   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 84.13%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 4
dl 0
loc 156
ccs 53
cts 63
cp 0.8413
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A hasDiskCaching() 0 4 1
A getDiskCachingDirectory() 0 4 1
C save() 0 67 13
A setUseDiskCaching() 0 13 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
     */
56 63
    public function __construct(PhpPresentation $pPhpPresentation = null)
57
    {
58
        // Assign PhpPresentation
59 63
        $this->setPhpPresentation($pPhpPresentation);
60
61
        // Set up disk caching location
62 63
        $this->diskCachingDirectory = './';
63
64
        // Set HashTable variables
65 63
        $this->oDrawingHashTable = new HashTable();
66
67 63
        $this->setZipAdapter(new ZipArchiveAdapter());
68 63
    }
69
70
    /**
71
     * Save PhpPresentation to file
72
     *
73
     * @param  string    $pFilename
74
     * @throws \Exception
75
     */
76 59
    public function save($pFilename)
77
    {
78 59
        if (empty($pFilename)) {
79 1
            throw new \Exception("Filename is empty");
80
        }
81
        // If $pFilename is php://output or php://stdout, make it a temporary file...
82 58
        $originalFilename = $pFilename;
83 58
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
84
            $pFilename = @tempnam('./', 'phppttmp');
85
            if ($pFilename == '') {
86
                $pFilename = $originalFilename;
87
            }
88
        }
89
90
        // Initialize HashTable
91 58
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
92
93
        // Initialize Zip
94 58
        $oZip = $this->getZipAdapter();
95 58
        $oZip->open($pFilename);
96
97
        // Variables
98 58
        $oPresentation = $this->getPhpPresentation();
99 58
        $arrayChart = array();
100
101 58
        $arrayFiles = array();
102 58
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation');
103 58
        foreach ($oDir as $oFile) {
104 58
            if (!$oFile->isFile()) {
105 58
                continue;
106
            }
107
108 58
            $class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php');
109 58
            $o = new \ReflectionClass($class);
110
111 58
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) {
112 58
                continue;
113
            }
114 58
            $arrayFiles[$oFile->getBasename('.php')] = $o;
115 58
        }
116
117 58
        ksort($arrayFiles);
118
119 58
        foreach ($arrayFiles as $o) {
120 58
            $oService = $o->newInstance();
121 58
            $oService->setZip($oZip);
122 58
            $oService->setPresentation($oPresentation);
123 58
            $oService->setDrawingHashTable($this->getDrawingHashTable());
124 58
            $oService->setArrayChart($arrayChart);
125 58
            $oZip = $oService->render();
126 58
            $arrayChart = $oService->getArrayChart();
127 58
            unset($oService);
128 58
        }
129
130
        // Close file
131 57
        $oZip->close();
132
133
        // If a temporary file was used, copy it to the correct file stream
134 57
        if ($originalFilename != $pFilename) {
135
            if (copy($pFilename, $originalFilename) === false) {
136
                throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
137
            }
138
            if (@unlink($pFilename) === false) {
139
                throw new \Exception('The file ' . $pFilename . ' could not be removed.');
140
            }
141
        }
142 57
    }
143
144
    /**
145
     * Get use disk caching where possible?
146
     *
147
     * @return boolean
148
     */
149 1
    public function hasDiskCaching()
150
    {
151 1
        return $this->useDiskCaching;
152
    }
153
154
    /**
155
     * Set use disk caching where possible?
156
     *
157
     * @param  boolean $pValue
158
     * @param  string $pDirectory Disk caching directory
159
     * @throws \Exception
160
     * @return \PhpOffice\PhpPresentation\Writer\ODPresentation
161
     */
162 2
    public function setUseDiskCaching($pValue = false, $pDirectory = null)
163
    {
164 2
        $this->useDiskCaching = $pValue;
165
166 2
        if (!is_null($pDirectory)) {
167 2
            if (!is_dir($pDirectory)) {
168 1
                throw new \Exception("Directory does not exist: $pDirectory");
169
            }
170 1
            $this->diskCachingDirectory = $pDirectory;
171 1
        }
172
173 1
        return $this;
174
    }
175
176
    /**
177
     * Get disk caching directory
178
     *
179
     * @return string
180
     */
181 2
    public function getDiskCachingDirectory()
182
    {
183 2
        return $this->diskCachingDirectory;
184
    }
185
}
186