Completed
Push — develop ( 47d726...7b90bb )
by Adrien
17:12
created

XMLWriter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 58.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 86
ccs 14
cts 24
cp 0.5833
rs 10
c 1
b 0
f 0
wmc 11
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 5
A __destruct() 0 7 2
A getData() 0 9 2
A writeRawData() 0 8 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5 57
if (!defined('DATE_W3C')) {
6
    define('DATE_W3C', 'Y-m-d\TH:i:sP');
7
}
8
9 57
if (!defined('DEBUGMODE_ENABLED')) {
10 57
    define('DEBUGMODE_ENABLED', false);
11
}
12
13
/**
14
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
15
 *
16
 * This library is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU Lesser General Public
18
 * License as published by the Free Software Foundation; either
19
 * version 2.1 of the License, or (at your option) any later version.
20
 *
21
 * This library is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24
 * Lesser General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Lesser General Public
27
 * License along with this library; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29
 *
30
 * @category   PhpSpreadsheet
31
 *
32
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
33
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
34
 */
35
class XMLWriter extends \XMLWriter
36
{
37
    /** Temporary storage method */
38
    const STORAGE_MEMORY = 1;
39
    const STORAGE_DISK = 2;
40
41
    /**
42
     * Temporary filename.
43
     *
44
     * @var string
45
     */
46
    private $tempFileName = '';
47
48
    /**
49
     * Create a new XMLWriter instance.
50
     *
51
     * @param int $pTemporaryStorage Temporary storage location
52
     * @param string $pTemporaryStorageFolder Temporary storage folder
53
     */
54 56
    public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = null)
55
    {
56
        // Open temporary storage
57 56
        if ($pTemporaryStorage == self::STORAGE_MEMORY) {
58 56
            $this->openMemory();
59
        } else {
60
            // Create temporary filename
61
            if ($pTemporaryStorageFolder === null) {
62
                $pTemporaryStorageFolder = File::sysGetTempDir();
63
            }
64
            $this->tempFileName = @tempnam($pTemporaryStorageFolder, 'xml');
65
66
            // Open storage
67
            if ($this->openUri($this->tempFileName) === false) {
68
                // Fallback to memory...
69
                $this->openMemory();
70
            }
71
        }
72
73
        // Set default values
74 56
        if (DEBUGMODE_ENABLED) {
75
            $this->setIndent(true);
76
        }
77 56
    }
78
79
    /**
80
     * Destructor.
81
     */
82 56
    public function __destruct()
83
    {
84
        // Unlink temporary files
85 56
        if ($this->tempFileName != '') {
86
            @unlink($this->tempFileName);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
87
        }
88 56
    }
89
90
    /**
91
     * Get written data.
92
     *
93
     * @return $data
0 ignored issues
show
Documentation introduced by
The doc-type $data could not be parsed: Unknown type name "$data" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
94
     */
95 56
    public function getData()
96
    {
97 56
        if ($this->tempFileName == '') {
98 56
            return $this->outputMemory(true);
99
        }
100
        $this->flush();
101
102
        return file_get_contents($this->tempFileName);
103
    }
104
105
    /**
106
     * Wrapper method for writeRaw.
107
     *
108
     * @param string|string[] $text
109
     *
110
     * @return bool
111
     */
112 56
    public function writeRawData($text)
113
    {
114 56
        if (is_array($text)) {
115
            $text = implode("\n", $text);
116
        }
117
118 56
        return $this->writeRaw(htmlspecialchars($text));
119
    }
120
}
121