Completed
Pull Request — develop (#435)
by
unknown
11:46
created

PowerPoint2007   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 83.61%

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 5
dl 0
loc 181
ccs 51
cts 61
cp 0.8361
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
C save() 0 62 13
A hasDiskCaching() 0 4 1
A setUseDiskCaching() 0 13 3
A getDiskCachingDirectory() 0 4 1
A getLayoutPack() 0 4 1
A setLayoutPack() 0 6 1
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 DirectoryIterator;
21
use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter;
22
use PhpOffice\PhpPresentation\HashTable;
23
use PhpOffice\PhpPresentation\PhpPresentation;
24
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack;
25
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\PackDefault;
26
27
/**
28
 * \PhpOffice\PhpPresentation\Writer\PowerPoint2007
29
 */
30
class PowerPoint2007 extends AbstractWriter implements WriterInterface
31
{
32
    /**
33
     * Use disk caching where possible?
34
     *
35
     * @var boolean
36
     */
37
    protected $useDiskCaching = false;
38
39
    /**
40
     * Disk caching directory
41
     *
42
     * @var string
43
     */
44
    protected $diskCachingDir;
45
46
    /**
47
     * Layout pack to use
48
     * @deprecated 0.7
49
     * @var \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
50
     */
51
    protected $layoutPack;
52
53
    /**
54
     * Create a new PowerPoint2007 file
55
     *
56
     * @param PhpPresentation $pPhpPresentation
57
     */
58 124
    public function __construct(PhpPresentation $pPhpPresentation = null)
59
    {
60
        // Assign PhpPresentation
61 124
        $this->setPhpPresentation($pPhpPresentation);
62
63
        // Set up disk caching location
64 124
        $this->diskCachingDir = './';
65
66
        // Set layout pack
67 124
        $this->layoutPack = new PackDefault();
0 ignored issues
show
Deprecated Code introduced by
The property PhpOffice\PhpPresentatio...rPoint2007::$layoutPack has been deprecated with message: 0.7

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Deprecated Code introduced by
The class PhpOffice\PhpPresentatio...\LayoutPack\PackDefault has been deprecated with message: 0.7

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
68
69
        // Set HashTable variables
70 124
        $this->oDrawingHashTable = new HashTable();
71
72 124
        $this->setZipAdapter(new ZipArchiveAdapter());
73 124
    }
74
75
    /**
76
     * Save PhpPresentation to file
77
     *
78
     * @param  string    $pFilename
79
     * @throws \Exception
80
     */
81 118
    public function save($pFilename)
82
    {
83 118
        if (empty($pFilename)) {
84 1
            throw new \Exception("Filename is empty");
85
        }
86 117
        $oPresentation = $this->getPhpPresentation();
87
88
        // If $pFilename is php://output or php://stdout, make it a temporary file...
89 116
        $originalFilename = $pFilename;
90 116
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
91
            $pFilename = @tempnam('./', 'phppttmp');
92
            if ($pFilename == '') {
93
                $pFilename = $originalFilename;
94
            }
95
        }
96
97
        // Create drawing dictionary
98 116
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
99
100 116
        $oZip = $this->getZipAdapter();
101 116
        $oZip->open($pFilename);
102
103 116
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'PowerPoint2007');
104 116
        $arrayFiles = array();
105 116
        foreach ($oDir as $oFile) {
106 116
            if (!$oFile->isFile()) {
107 116
                continue;
108
            }
109
110 116
            $class = __NAMESPACE__ . '\\PowerPoint2007\\' . $oFile->getBasename('.php');
111 116
            $o = new \ReflectionClass($class);
112
113 116
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\PowerPoint2007\AbstractDecoratorWriter')) {
114 116
                continue;
115
            }
116 116
            $arrayFiles[$oFile->getBasename('.php')] = $o;
117 116
        }
118
119 116
        ksort($arrayFiles);
120
121 116
        foreach ($arrayFiles as $o) {
122 116
            $oService = $o->newInstance();
123 116
            $oService->setZip($oZip);
124 116
            $oService->setPresentation($oPresentation);
125 116
            $oService->setDrawingHashTable($this->getDrawingHashTable());
126 116
            $oZip = $oService->render();
127 116
            unset($oService);
128 116
        }
129
130
        // Close file
131 113
        $oZip->close();
132
133
        // If a temporary file was used, copy it to the correct file stream
134 113
        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 113
    }
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\PowerPoint2007
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->diskCachingDir = $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->diskCachingDir;
184
    }
185
186
    /**
187
     * Get layout pack to use
188
     *
189
     * @deprecated 0.7
190
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
191
     */
192
    public function getLayoutPack()
193
    {
194
        return $this->layoutPack;
0 ignored issues
show
Deprecated Code introduced by
The property PhpOffice\PhpPresentatio...rPoint2007::$layoutPack has been deprecated with message: 0.7

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
195
    }
196
197
    /**
198
     * Set layout pack to use
199
     *
200
     * @deprecated 0.7
201
     * @param \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack $pValue
202
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
203
     */
204
    public function setLayoutPack(AbstractLayoutPack $pValue = null)
205
    {
206
        $this->layoutPack = $pValue;
0 ignored issues
show
Deprecated Code introduced by
The property PhpOffice\PhpPresentatio...rPoint2007::$layoutPack has been deprecated with message: 0.7

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
207
208
        return $this;
209
    }
210
}
211