Failed Conditions
Pull Request — master (#4328)
by Owen
15:26 queued 04:43
created

Settings   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
eloc 28
dl 0
loc 140
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getChartRenderer() 0 3 1
A getHttpClient() 0 7 3
A getLocale() 0 3 1
A htmlEntityFlags() 0 3 1
A unsetHttpClient() 0 4 1
A setCache() 0 3 1
A setChartRenderer() 0 7 2
A unsetChartRenderer() 0 3 1
A getCache() 0 7 3
A getRequestFactory() 0 7 3
A useSimpleCacheVersion3() 0 3 1
A setHttpClient() 0 4 1
A setLocale() 0 3 1
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
use ReflectionClass;
12
13
class Settings
14
{
15
    /**
16
     * Class name of the chart renderer used for rendering charts
17
     * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph.
18
     *
19
     * @var null|class-string<IRenderer>
20
     */
21
    private static ?string $chartRenderer = null;
22
23
    /**
24
     * The cache implementation to be used for cell collection.
25
     */
26
    private static ?CacheInterface $cache = null;
27
28
    /**
29
     * The HTTP client implementation to be used for network request.
30
     */
31
    private static ?ClientInterface $httpClient = null;
32
33
    private static ?RequestFactoryInterface $requestFactory = null;
34
35
    /**
36
     * Set the locale code to use for formula translations and any special formatting.
37
     *
38
     * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
39
     *
40
     * @return bool Success or failure
41
     */
42 745
    public static function setLocale(string $locale): bool
43
    {
44 745
        return Calculation::getInstance()->setLocale($locale);
45
    }
46
47 745
    public static function getLocale(): string
48
    {
49 745
        return Calculation::getInstance()->getLocale();
50
    }
51
52
    /**
53
     * Identify to PhpSpreadsheet the external library to use for rendering charts.
54
     *
55
     * @param class-string<IRenderer> $rendererClassName Class name of the chart renderer
56
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
57
     */
58 7
    public static function setChartRenderer(string $rendererClassName): void
59
    {
60 7
        if (!is_a($rendererClassName, IRenderer::class, true)) {
61 1
            throw new Exception('Chart renderer must implement ' . IRenderer::class);
62
        }
63
64 6
        self::$chartRenderer = $rendererClassName;
65
    }
66
67 1
    public static function unsetChartRenderer(): void
68
    {
69 1
        self::$chartRenderer = null;
70
    }
71
72
    /**
73
     * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
74
     *
75
     * @return null|class-string<IRenderer> Class name of the chart renderer
76
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
77
     */
78 6
    public static function getChartRenderer(): ?string
79
    {
80 6
        return self::$chartRenderer;
81
    }
82
83 533
    public static function htmlEntityFlags(): int
84
    {
85 533
        return ENT_COMPAT;
86
    }
87
88
    /**
89
     * Sets the implementation of cache that should be used for cell collection.
90
     */
91 3
    public static function setCache(?CacheInterface $cache): void
92
    {
93 3
        self::$cache = $cache;
94
    }
95
96
    /**
97
     * Gets the implementation of cache that is being used for cell collection.
98
     */
99 10451
    public static function getCache(): CacheInterface
100
    {
101 10451
        if (!self::$cache) {
102 280
            self::$cache = self::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1();
103
        }
104
105 10451
        return self::$cache;
106
    }
107
108 282
    public static function useSimpleCacheVersion3(): bool
109
    {
110 282
        return (new ReflectionClass(CacheInterface::class))->getMethod('get')->getReturnType() !== null;
111
    }
112
113
    /**
114
     * Set the HTTP client implementation to be used for network request.
115
     */
116 4
    public static function setHttpClient(ClientInterface $httpClient, RequestFactoryInterface $requestFactory): void
117
    {
118 4
        self::$httpClient = $httpClient;
119 4
        self::$requestFactory = $requestFactory;
120
    }
121
122
    /**
123
     * Unset the HTTP client configuration.
124
     */
125 7
    public static function unsetHttpClient(): void
126
    {
127 7
        self::$httpClient = null;
128 7
        self::$requestFactory = null;
129
    }
130
131
    /**
132
     * Get the HTTP client implementation to be used for network request.
133
     */
134 5
    public static function getHttpClient(): ClientInterface
135
    {
136 5
        if (!self::$httpClient || !self::$requestFactory) {
137 1
            throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
138
        }
139
140 4
        return self::$httpClient;
141
    }
142
143
    /**
144
     * Get the HTTP request factory.
145
     */
146 5
    public static function getRequestFactory(): RequestFactoryInterface
147
    {
148 5
        if (!self::$httpClient || !self::$requestFactory) {
149 1
            throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
150
        }
151
152 4
        return self::$requestFactory;
153
    }
154
}
155