Completed
Push — develop ( 00da3e...def468 )
by Franck
11s
created

ODPresentation::save()   C

Complexity

Conditions 12
Paths 49

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 14.25

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 59
ccs 30
cts 40
cp 0.75
rs 6.4485
cc 12
eloc 35
nc 49
nop 1
crap 14.25

How to fix   Long Method    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 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 57
        }
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 1
            } else {
163 1
                throw new \Exception("Directory does not exist: $pDirectory");
164
            }
165 1
        }
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
    /**
181
     * Get an array of all drawings
182
     *
183
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing[] All drawings in PhpPresentation
184
     * @throws \Exception
185
     */
186 57
    protected function allDrawings()
187
    {
188
        // Get an array of all drawings
189 57
        $aDrawings  = array();
190
191
        // Loop trough PhpPresentation
192 57
        $slideCount = $this->getPhpPresentation()->getSlideCount();
193 57
        for ($i = 0; $i < $slideCount; ++$i) {
194
            // Loop trough images and add to array
195 57
            $iterator = $this->getPhpPresentation()->getSlide($i)->getShapeCollection()->getIterator();
196 57
            while ($iterator->valid()) {
197 50
                if ($iterator->current() instanceof AbstractDrawing && !($iterator->current() instanceof Table)) {
198 28
                    $aDrawings[] = $iterator->current();
199 50
                } elseif ($iterator->current() instanceof Group) {
200 1
                    $iterator2 = $iterator->current()->getShapeCollection()->getIterator();
201 1
                    while ($iterator2->valid()) {
202 1
                        if ($iterator2->current() instanceof AbstractDrawing && !($iterator2->current() instanceof Table)) {
203 1
                            $aDrawings[] = $iterator2->current();
204 1
                        }
205 1
                        $iterator2->next();
206 1
                    }
207 1
                }
208
209 50
                $iterator->next();
210 50
            }
211 57
        }
212
213 57
        return $aDrawings;
214
    }
215
}
216