Failed Conditions
Push — master ( 165034...7cb488 )
by Adrien
09:17
created

Settings::getLibXmlDisableEntityLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Chart\Renderer\IRenderer;
7
use PhpOffice\PhpSpreadsheet\Collection\Memory;
8
use Psr\Http\Client\ClientInterface;
9
use Psr\Http\Message\RequestFactoryInterface;
10
use Psr\SimpleCache\CacheInterface;
11
12
class Settings
13
{
14
    /**
15
     * Class name of the chart renderer used for rendering charts
16
     * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph.
17
     *
18
     * @var string
19
     */
20
    private static $chartRenderer;
21
22
    /**
23
     * Default options for libxml loader.
24
     *
25
     * @var int
26
     */
27
    private static $libXmlLoaderOptions = null;
28
29
    /**
30
     * Allow/disallow libxml_disable_entity_loader() call when not thread safe.
31
     * Default behaviour is to do the check, but if you're running PHP versions
32
     *      7.2 < 7.2.1
33
     * then you may need to disable this check to prevent unwanted behaviour in other threads
34
     * SECURITY WARNING: Changing this flag is not recommended.
35
     *
36
     * @var bool
37
     */
38
    private static $libXmlDisableEntityLoader = true;
39
40
    /**
41
     * The cache implementation to be used for cell collection.
42
     *
43
     * @var CacheInterface
44
     */
45
    private static $cache;
46
47
    /**
48
     * The HTTP client implementation to be used for network request.
49
     *
50
     * @var null|ClientInterface
51
     */
52
    private static $httpClient;
53
54
    /**
55
     * @var null|RequestFactoryInterface
56
     */
57
    private static $requestFactory;
58
59
    /**
60
     * Set the locale code to use for formula translations and any special formatting.
61
     *
62
     * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
63 1
     *
64
     * @return bool Success or failure
65 1
     */
66
    public static function setLocale($locale)
67
    {
68
        return Calculation::getInstance()->setLocale($locale);
69 1
    }
70 1
71
    /**
72
     * Identify to PhpSpreadsheet the external library to use for rendering charts.
73
     *
74
     * @param string $rendererClass Class name of the chart renderer
75
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
76
     */
77
    public static function setChartRenderer($rendererClass): void
78 1
    {
79
        if (!is_a($rendererClass, IRenderer::class, true)) {
80 1
            throw new Exception('Chart renderer must implement ' . IRenderer::class);
81
        }
82
83
        self::$chartRenderer = $rendererClass;
84
    }
85
86
    /**
87
     * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
88 33
     *
89
     * @return null|string Class name of the chart renderer
90 33
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
91 15
     */
92
    public static function getChartRenderer()
93 33
    {
94 33
        return self::$chartRenderer;
95
    }
96
97
    /**
98
     * Set default options for libxml loader.
99
     *
100
     * @param int $options Default options for libxml loader
101
     */
102 96
    public static function setLibXmlLoaderOptions($options): void
103
    {
104 96
        if ($options === null && defined('LIBXML_DTDLOAD')) {
0 ignored issues
show
introduced by
The condition $options === null is always false.
Loading history...
105 17
            $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
106 94
        }
107
        self::$libXmlLoaderOptions = $options;
108
    }
109
110 96
    /**
111
     * Get default options for libxml loader.
112
     * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
113
     *
114
     * @return int Default options for libxml loader
115
     */
116
    public static function getLibXmlLoaderOptions()
117
    {
118
        if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) {
0 ignored issues
show
introduced by
The condition self::libXmlLoaderOptions === null is always false.
Loading history...
119
            self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
120
        } elseif (self::$libXmlLoaderOptions === null) {
0 ignored issues
show
introduced by
The condition self::libXmlLoaderOptions === null is always false.
Loading history...
121
            self::$libXmlLoaderOptions = true;
122
        }
123
124
        return self::$libXmlLoaderOptions;
125
    }
126
127
    /**
128
     * Enable/Disable the entity loader for libxml loader.
129
     * Allow/disallow libxml_disable_entity_loader() call when not thread safe.
130
     * Default behaviour is to do the check, but if you're running PHP versions
131
     *      7.2 < 7.2.1
132
     * then you may need to disable this check to prevent unwanted behaviour in other threads
133 160
     * SECURITY WARNING: Changing this flag to false is not recommended.
134
     *
135 160
     * @param bool $state
136
     */
137
    public static function setLibXmlDisableEntityLoader($state): void
138
    {
139
        self::$libXmlDisableEntityLoader = (bool) $state;
140
    }
141
142
    /**
143
     * Return the state of the entity loader (disabled/enabled) for libxml loader.
144
     *
145
     * @return bool $state
146
     */
147
    public static function getLibXmlDisableEntityLoader()
148
    {
149
        return self::$libXmlDisableEntityLoader;
150
    }
151
152
    /**
153 359
     * Sets the implementation of cache that should be used for cell collection.
154
     */
155 359
    public static function setCache(CacheInterface $cache): void
156 101
    {
157
        self::$cache = $cache;
158
    }
159 359
160
    /**
161
     * Gets the implementation of cache that should be used for cell collection.
162
     *
163
     * @return CacheInterface
164
     */
165
    public static function getCache()
166
    {
167
        if (!self::$cache) {
168
            self::$cache = new Memory();
169
        }
170
171
        return self::$cache;
172
    }
173
174
    /**
175
     * Set the HTTP client implementation to be used for network request.
176
     */
177
    public static function setHttpClient(ClientInterface $httpClient, RequestFactoryInterface $requestFactory): void
178
    {
179
        self::$httpClient = $httpClient;
180
        self::$requestFactory = $requestFactory;
181
    }
182
183
    /**
184
     * Unset the HTTP client configuration.
185
     */
186
    public static function unsetHttpClient(): void
187
    {
188
        self::$httpClient = null;
189
        self::$requestFactory = null;
190
    }
191
192
    /**
193
     * Get the HTTP client implementation to be used for network request.
194
     */
195
    public static function getHttpClient(): ClientInterface
196
    {
197
        self::assertHttpClient();
198
199
        return self::$httpClient;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::httpClient could return the type null which is incompatible with the type-hinted return Psr\Http\Client\ClientInterface. Consider adding an additional type-check to rule them out.
Loading history...
200
    }
201
202
    /**
203
     * Get the HTTP request factory.
204
     */
205
    public static function getRequestFactory(): RequestFactoryInterface
206
    {
207
        self::assertHttpClient();
208
209
        return self::$requestFactory;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::requestFactory could return the type null which is incompatible with the type-hinted return Psr\Http\Message\RequestFactoryInterface. Consider adding an additional type-check to rule them out.
Loading history...
210
    }
211
212
    private static function assertHttpClient(): void
213
    {
214
        if (!self::$httpClient || !self::$requestFactory) {
215
            throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
216
        }
217
    }
218
}
219