Completed
Pull Request — develop (#207)
by Franck
06:53
created

PowerPoint2007::allDrawings()   D

Complexity

Conditions 10
Paths 3

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10.017

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 17
cts 18
cp 0.9444
rs 4.8196
cc 10
eloc 18
nc 3
nop 0
crap 10.017

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
     */
63 105
    public function __construct(PhpPresentation $pPhpPresentation = null)
64
    {
65
        // Assign PhpPresentation
66 105
        $this->setPhpPresentation($pPhpPresentation);
67
68
        // Set up disk caching location
69 105
        $this->diskCachingDir = './';
70
71
        // Set layout pack
72 105
        $this->layoutPack = new PackDefault();
73
74
        // Set HashTable variables
75 105
        $this->oDrawingHashTable = new HashTable();
76
77 105
        $this->setZipAdapter(new ZipArchiveAdapter());
78 105
    }
79
80
    /**
81
     * Save PhpPresentation to file
82
     *
83
     * @param  string    $pFilename
84
     * @throws \Exception
85
     */
86 99
    public function save($pFilename)
87
    {
88 99
        if (empty($pFilename)) {
89 1
            throw new \Exception("Filename is empty");
90
        }
91 98
        $oPresentation = $this->getPhpPresentation();
92
93
        // If $pFilename is php://output or php://stdout, make it a temporary file...
94 97
        $originalFilename = $pFilename;
95 97
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
96
            $pFilename = @tempnam('./', 'phppttmp');
97
            if ($pFilename == '') {
98
                $pFilename = $originalFilename;
99
            }
100
        }
101
102
        // Create drawing dictionary
103 97
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
104
105 97
        $oZip = $this->getZipAdapter();
106 97
        $oZip->open($pFilename);
107
108 97
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'PowerPoint2007');
109 97
        foreach ($oDir as $oFile) {
110 97
            if (!$oFile->isFile()) {
111 97
                continue;
112
            }
113
114 97
            $class = __NAMESPACE__ . '\\PowerPoint2007\\' . $oFile->getBasename('.php');
115 97
            $o = new \ReflectionClass($class);
116
117 97
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\PowerPoint2007\AbstractDecoratorWriter')) {
118 97
                continue;
119
            }
120 97
            $arrayFiles[$oFile->getBasename('.php')] = $o;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arrayFiles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arrayFiles = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
121
        }
122
123 97
        ksort($arrayFiles);
124
125 97
        foreach ($arrayFiles as $o) {
126 97
            $oService = $o->newInstance();
127 97
            $oService->setZip($oZip);
128 97
            $oService->setPresentation($oPresentation);
129 97
            $oService->setDrawingHashTable($this->getDrawingHashTable());
130 97
            $oZip = $oService->render();
131 97
            unset($oService);
132
        }
133
134
        // Close file
135 94
        $oZip->close();
136
137
        // If a temporary file was used, copy it to the correct file stream
138 94
        if ($originalFilename != $pFilename) {
139
            if (copy($pFilename, $originalFilename) === false) {
140
                throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
141
            }
142
            if (@unlink($pFilename) === false) {
143
                throw new \Exception('The file '.$pFilename.' could not be removed.');
144
            }
145
        }
146 94
    }
147
148
    /**
149
     * Get use disk caching where possible?
150
     *
151
     * @return boolean
152
     */
153 1
    public function hasDiskCaching()
154
    {
155 1
        return $this->useDiskCaching;
156
    }
157
158
    /**
159
     * Set use disk caching where possible?
160
     *
161
     * @param  boolean $pValue
162
     * @param  string $pDirectory Disk caching directory
163
     * @throws \Exception
164
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
165
     */
166 2
    public function setUseDiskCaching($pValue = false, $pDirectory = null)
167
    {
168 2
        $this->useDiskCaching = $pValue;
169
170 2
        if (!is_null($pDirectory)) {
171 2
            if (!is_dir($pDirectory)) {
172 1
                throw new \Exception("Directory does not exist: $pDirectory");
173
            }
174 1
            $this->diskCachingDir = $pDirectory;
175
        }
176
177 1
        return $this;
178
    }
179
180
    /**
181
     * Get disk caching directory
182
     *
183
     * @return string
184
     */
185 2
    public function getDiskCachingDirectory()
186
    {
187 2
        return $this->diskCachingDir;
188
    }
189
190
    /**
191
     * Get layout pack to use
192
     *
193
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
194
     */
195 2
    public function getLayoutPack()
196
    {
197 2
        return $this->layoutPack;
198
    }
199
200
    /**
201
     * Set layout pack to use
202
     *
203
     * @param \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack $pValue
204
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
205
     */
206 1
    public function setLayoutPack(AbstractLayoutPack $pValue = null)
207
    {
208 1
        $this->layoutPack = $pValue;
209
210 1
        return $this;
211
    }
212
}
213