XMLWriter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of PHPProject - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPProject 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/PHPWord/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPProject
14
 * @copyright   2009-2014 PHPProject contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpProject\Shared;
19
20
/**
21
 * XMLWriter
22
 *
23
 * @method bool endElement()
24
 * @method mixed flush(bool $empty = null)
25
 * @method bool openMemory()
26
 * @method string outputMemory(bool $flush = null)
27
 * @method bool setIndent(bool $indent)
28
 * @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
29
 * @method bool startElement(string $name)
30
 * @method bool text(string $content)
31
 * @method bool writeAttribute(string $name, mixed $value)
32
 * @method bool writeCData(string $content)
33
 * @method bool writeComment(string $content)
34
 * @method bool writeElement(string $name, string $content = null)
35
 * @method bool writeRaw(string $content)
36
 */
37
class XMLWriter
38
{
39
    /** Temporary storage method */
40
    const STORAGE_MEMORY = 1;
41
    const STORAGE_DISK = 2;
42
43
    /**
44
     * Internal XMLWriter
45
     *
46
     * @var \XMLWriter
47
     */
48
    private $xmlWriter;
49
50
    /**
51
     * Temporary filename
52
     *
53
     * @var string
54
     */
55
    private $tempFileName = '';
56
57
    /**
58
     * Create a new \PhpOffice\PhpPowerpoint\Shared\XMLWriter instance
59
     *
60
     * @param int $pTemporaryStorage Temporary storage location
61
     * @param string $pTemporaryStorageDir Temporary storage folder
62
     */
63 3
    public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = './')
64
    {
65
        // Create internal XMLWriter
66 3
        $this->xmlWriter = new \XMLWriter();
67
68
        // Open temporary storage
69 3
        if ($pTemporaryStorage == self::STORAGE_MEMORY) {
70 1
            $this->xmlWriter->openMemory();
71 1
        } else {
72
            // Create temporary filename
73 3
            $this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml');
74
75
            // Open storage
76 3
            $this->xmlWriter->openUri($this->tempFileName);
77
        }
78
79
        // Set default values
80 3
        $this->xmlWriter->setIndent(true);
81 3
    }
82
83
    /**
84
     * Destructor
85
     */
86 3
    public function __destruct()
87
    {
88
        // Desctruct XMLWriter
89 3
        unset($this->xmlWriter);
90
91
        // Unlink temporary files
92 3
        if ($this->tempFileName != '') {
93 3
            @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...
94 3
        }
95 3
    }
96
97
    /**
98
     * Catch function calls (and pass them to internal XMLWriter)
99
     *
100
     * @param mixed $function
101
     * @param mixed $args
102
     */
103 3
    public function __call($function, $args)
104
    {
105
        try {
106 3
            @call_user_func_array(array(
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...
107 3
                $this->xmlWriter,
108
                $function
109 3
            ), $args);
110 3
        } catch (\Exception $ex) {
111
            // Do nothing!
112
        }
113 3
    }
114
115
    /**
116
     * Get written data
117
     *
118
     * @return string
119
     */
120 2
    public function getData()
121
    {
122 2
        if ($this->tempFileName == '') {
123 1
            return $this->xmlWriter->outputMemory(true);
124
        } else {
125 2
            $this->xmlWriter->flush();
126 2
            return file_get_contents($this->tempFileName);
127
        }
128
    }
129
}
130