Completed
Pull Request — develop (#207)
by Franck
14:46 queued 07:07
created

PowerPoint2007::allDrawings()   D

Complexity

Conditions 10
Paths 3

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10.0658

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 21
cts 23
cp 0.913
rs 4.8196
cc 10
eloc 18
nc 3
nop 0
crap 10.0658

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Shape\AbstractDrawing;
25
use PhpOffice\PhpPresentation\Shape\Chart as ChartShape;
26
use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter;
27
use PhpOffice\PhpPresentation\Shape\Group;
28
use PhpOffice\PhpPresentation\Shape\Table;
29
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack;
30
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\PackDefault;
31
32
/**
33
 * \PhpOffice\PhpPresentation\Writer\PowerPoint2007
34
 */
35
class PowerPoint2007 extends AbstractWriter implements WriterInterface
36
{
37
    /**
38
     * Use disk caching where possible?
39
     *
40
     * @var boolean
41
     */
42
    protected $useDiskCaching = false;
43
44
    /**
45
     * Disk caching directory
46
     *
47
     * @var string
48
     */
49
    protected $diskCachingDir;
50
51
    /**
52
     * Layout pack to use
53
     *
54
     * @var \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
55
     */
56
    protected $layoutPack;
57
58
    /**
59
     * Create a new PowerPoint2007 file
60
     *
61
     * @param PhpPresentation $pPhpPresentation
62 105
     */
63
    public function __construct(PhpPresentation $pPhpPresentation = null)
64
    {
65 105
        // Assign PhpPresentation
66
        $this->setPhpPresentation($pPhpPresentation);
67
68 105
        // Set up disk caching location
69
        $this->diskCachingDir = './';
70
71 105
        // Set layout pack
72
        $this->layoutPack = new PackDefault();
73
74 105
        // Set HashTable variables
75
        $this->oDrawingHashTable = new HashTable();
76 105
77 105
        $this->setZipAdapter(new ZipArchiveAdapter());
78
    }
79
80
    /**
81
     * Save PhpPresentation to file
82
     *
83
     * @param  string    $pFilename
84
     * @throws \Exception
85 99
     */
86
    public function save($pFilename)
87 99
    {
88 1
        if (empty($pFilename)) {
89
            throw new \Exception("Filename is empty");
90 98
        }
91
        $oPresentation = $this->getPhpPresentation();
92
93 97
        // If $pFilename is php://output or php://stdout, make it a temporary file...
94 97
        $originalFilename = $pFilename;
95
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
96
            $pFilename = @tempnam('./', 'phppttmp');
97
            if ($pFilename == '') {
98
                $pFilename = $originalFilename;
99
            }
100
        }
101
102 97
        // Create drawing dictionary
103
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
104 97
105 97
        $oZip = $this->getZipAdapter();
106
        $oZip->open($pFilename);
107 97
108 97
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'PowerPoint2007');
109 97
        foreach ($oDir as $oFile) {
110 97
            if (!$oFile->isFile()) {
111
                continue;
112 97
            }
113 97
            $class = __NAMESPACE__.'\\PowerPoint2007\\'.$oFile->getBasename('.php');
114
            $o = new \ReflectionClass($class);
115 97
116 95
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\PowerPoint2007\AbstractDecoratorWriter')) {
117
                continue;
118 97
            }
119 97
            $oService = $o->newInstance();
120 97
            $oService->setZip($oZip);
121 97
            $oService->setPresentation($oPresentation);
122 97
            $oService->setDrawingHashTable($this->getDrawingHashTable());
123 97
            $oZip = $oService->render();
124 97
            unset($oService);
125
        }
126
127 94
        // Close file
128
        $oZip->close();
129
130 94
        // If a temporary file was used, copy it to the correct file stream
131
        if ($originalFilename != $pFilename) {
132
            if (copy($pFilename, $originalFilename) === false) {
133
                throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
134
            }
135
            if (@unlink($pFilename) === false) {
136
                throw new \Exception('The file '.$pFilename.' could not be removed.');
137
            }
138 94
        }
139
    }
140
141
    /**
142
     * Get use disk caching where possible?
143
     *
144
     * @return boolean
145 1
     */
146
    public function hasDiskCaching()
147 1
    {
148
        return $this->useDiskCaching;
149
    }
150
151
    /**
152
     * Set use disk caching where possible?
153
     *
154
     * @param  boolean $pValue
155
     * @param  string $pDirectory Disk caching directory
156
     * @throws \Exception
157
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
158 2
     */
159
    public function setUseDiskCaching($pValue = false, $pDirectory = null)
160 2
    {
161
        $this->useDiskCaching = $pValue;
162 2
163 2
        if (!is_null($pDirectory)) {
164 1
            if (is_dir($pDirectory)) {
165 1
                $this->diskCachingDir = $pDirectory;
166 1
            } else {
167
                throw new \Exception("Directory does not exist: $pDirectory");
168 1
            }
169
        }
170 1
171
        return $this;
172
    }
173
174
    /**
175
     * Get disk caching directory
176
     *
177
     * @return string
178 2
     */
179
    public function getDiskCachingDirectory()
180 2
    {
181
        return $this->diskCachingDir;
182
    }
183
184
    /**
185
     * Get layout pack to use
186
     *
187
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
188 2
     */
189
    public function getLayoutPack()
190 2
    {
191
        return $this->layoutPack;
192
    }
193
194
    /**
195
     * Set layout pack to use
196
     *
197
     * @param \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack $pValue
198
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
199 1
     */
200
    public function setLayoutPack(AbstractLayoutPack $pValue = null)
201 1
    {
202
        $this->layoutPack = $pValue;
203 1
204
        return $this;
205
    }
206
}
207