Completed
Push — develop ( c5339b...ea5663 )
by Adrien
19:12
created

Settings::getLibXmlLoaderOptions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 0
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
ccs 6
cts 7
cp 0.8571
crap 4.0466
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * @category   PhpSpreadsheet
23
 *
24
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
25
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
26
 */
27
class Settings
28
{
29
    /**    Optional Chart Rendering libraries */
30
    const CHART_RENDERER_JPGRAPH = 'JpGraph';
31
32
    /**    Optional PDF Rendering libraries */
33
    const PDF_RENDERER_TCPDF = 'TcPDF';
34
    const PDF_RENDERER_DOMPDF = 'DomPDF';
35
    const PDF_RENDERER_MPDF = 'MPDF';
36
37
    private static $chartRenderers = [
38
        self::CHART_RENDERER_JPGRAPH,
39
    ];
40
41
    private static $pdfRenderers = [
42
        self::PDF_RENDERER_TCPDF,
43
        self::PDF_RENDERER_DOMPDF,
44
        self::PDF_RENDERER_MPDF,
45
    ];
46
47
    /**
48
     * Name of the external Library used for rendering charts
49
     * e.g.
50
     *        jpgraph.
51
     *
52
     * @var string
53
     */
54
    private static $chartRendererName;
55
56
    /**
57
     * Directory Path to the external Library used for rendering charts.
58
     *
59
     * @var string
60
     */
61
    private static $chartRendererPath;
62
63
    /**
64
     * Name of the external Library used for rendering PDF files
65
     * e.g.
66
     *         mPDF.
67
     *
68
     * @var string
69
     */
70
    private static $pdfRendererName;
71
72
    /**
73
     * Default options for libxml loader.
74
     *
75
     * @var int
76
     */
77
    private static $libXmlLoaderOptions = null;
78
79
    /**
80
     * Return the name of the method that is currently configured for cell cacheing.
81
     *
82
     * @return string Name of the cacheing method
83
     */
84
    public static function getCacheStorageMethod()
85
    {
86
        return CachedObjectStorageFactory::getCacheStorageMethod();
87
    }
88
89
    /**
90
     * Return the name of the class that is currently being used for cell cacheing.
91
     *
92
     * @return string Name of the class currently being used for cacheing
93
     */
94
    public static function getCacheStorageClass()
95
    {
96
        return CachedObjectStorageFactory::getCacheStorageClass();
97
    }
98
99
    /**
100
     * Set the method that should be used for cell caching.
101
     *
102
     * @param string $method Name of the caching method
103
     * @param array $arguments Optional configuration arguments for the caching method
104
     *
105
     * @return bool Success or failure
106
     */
107
    public static function setCacheStorageMethod($method = CachedObjectStorageFactory::CACHE_IN_MEMORY, $arguments = [])
108
    {
109
        return CachedObjectStorageFactory::initialize($method, $arguments);
110
    }
111
112
    /**
113
     * Set the locale code to use for formula translations and any special formatting.
114
     *
115
     * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
116
     *
117
     * @return bool Success or failure
118
     */
119
    public static function setLocale($locale = 'en_us')
120
    {
121
        return Calculation::getInstance()->setLocale($locale);
122
    }
123
124
    /**
125
     * Set details of the external library that PhpSpreadsheet should use for rendering charts.
126
     *
127
     * @param string $libraryName Internal reference name of the library
128
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH
129
     * @param string $libraryBaseDir Directory path to the library's base folder
130
     *
131
     * @return bool Success or failure
132
     */
133 3
    public static function setChartRenderer($libraryName, $libraryBaseDir)
134
    {
135 3
        if (!self::setChartRendererName($libraryName)) {
136
            return false;
137
        }
138
139 3
        return self::setChartRendererPath($libraryBaseDir);
140
    }
141
142
    /**
143
     * Identify to PhpSpreadsheet the external library to use for rendering charts.
144
     *
145
     * @param string $libraryName Internal reference name of the library
146
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH
147
     *
148
     * @return bool Success or failure
149
     */
150 3
    public static function setChartRendererName($libraryName)
151
    {
152 3
        if (!in_array($libraryName, self::$chartRenderers)) {
153
            return false;
154
        }
155 3
        self::$chartRendererName = $libraryName;
156
157 3
        return true;
158
    }
159
160
    /**
161
     * Tell PhpSpreadsheet where to find the external library to use for rendering charts.
162
     *
163
     * @param string $libraryBaseDir Directory path to the library's base folder
164
     *
165
     * @return bool Success or failure
166
     */
167 3
    public static function setChartRendererPath($libraryBaseDir)
168
    {
169 3
        if ((file_exists($libraryBaseDir) === false) || (is_readable($libraryBaseDir) === false)) {
170 3
            return false;
171
        }
172
        self::$chartRendererPath = $libraryBaseDir;
173
174
        return true;
175
    }
176
177
    /**
178
     * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use (e.g. jpgraph).
179
     *
180
     * @return string|null Internal reference name of the Chart Rendering Library that PhpSpreadsheet is
181
     *    currently configured to use
182
     *    e.g. \PhpOffice\PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH
183
     */
184
    public static function getChartRendererName()
185
    {
186
        return self::$chartRendererName;
187
    }
188
189
    /**
190
     * Return the directory path to the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
191
     *
192
     * @return string|null Directory Path to the Chart Rendering Library that PhpSpreadsheet is
193
     *     currently configured to use
194
     */
195
    public static function getChartRendererPath()
196
    {
197
        return self::$chartRendererPath;
198
    }
199
200
    /**
201
     * Identify to PhpSpreadsheet the external library to use for rendering PDF files.
202
     *
203
     * @param string $libraryName Internal reference name of the library
204
     *     e.g. \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_TCPDF,
205
     *          \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_DOMPDF
206
     *       or \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_MPDF
207
     */
208 5
    public static function setPdfRendererName($libraryName)
209
    {
210 5
        if (!in_array($libraryName, self::$pdfRenderers)) {
211
            throw new Exception('"' . $libraryName . '" is not a valid PDF library name');
212
        }
213 5
        self::$pdfRendererName = $libraryName;
214 5
    }
215
216
    /**
217
     * Return the PDF Rendering Library that PhpSpreadsheet is currently configured to use (e.g. dompdf).
218
     *
219
     * @return string|null Internal reference name of the PDF Rendering Library that PhpSpreadsheet is
220
     *     currently configured to use
221
     * e.g. \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_TCPDF,
222
     *       \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_DOMPDF
223
     *    or \PhpOffice\PhpSpreadsheet\Settings::PDF_RENDERER_MPDF
224
     */
225 4
    public static function getPdfRendererName()
226
    {
227 4
        return self::$pdfRendererName;
228
    }
229
230
    /**
231
     * Set default options for libxml loader.
232
     *
233
     * @param int $options Default options for libxml loader
234
     */
235 14
    public static function setLibXmlLoaderOptions($options = null)
236
    {
237 14
        if (is_null($options) && defined('LIBXML_DTDLOAD')) {
238
            $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
239
        }
240 14
        @libxml_disable_entity_loader((bool) $options);
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...
241 14
        self::$libXmlLoaderOptions = $options;
242 14
    }
243
244
    /**
245
     * Get default options for libxml loader.
246
     * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
247
     *
248
     * @return int Default options for libxml loader
249
     */
250 22
    public static function getLibXmlLoaderOptions()
251
    {
252 22
        if (is_null(self::$libXmlLoaderOptions) && defined('LIBXML_DTDLOAD')) {
253 13
            self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
254 19
        } elseif (is_null(self::$libXmlLoaderOptions)) {
255
            self::$libXmlLoaderOptions = true;
0 ignored issues
show
Documentation Bug introduced by
The property $libXmlLoaderOptions was declared of type integer, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
256
        }
257 22
        @libxml_disable_entity_loader((bool) self::$libXmlLoaderOptions);
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...
258
259 22
        return self::$libXmlLoaderOptions;
260
    }
261
}
262