Passed
Pull Request — master (#4341)
by Owen
14:52
created

Settings::setHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
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
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
    public static function setLocale(string $locale): bool
43
    {
44
        return Calculation::getInstance()->setLocale($locale);
45
    }
46
47 745
    public static function getLocale(): string
48
    {
49 745
        return Calculation::getInstance()->getLocale();
50
    }
51
52 745
    /**
53
     * Identify to PhpSpreadsheet the external library to use for rendering charts.
54 745
     *
55
     * @param class-string<IRenderer> $rendererClassName Class name of the chart renderer
56
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
57
     */
58
    public static function setChartRenderer(string $rendererClassName): void
59
    {
60
        if (!is_a($rendererClassName, IRenderer::class, true)) {
61
            throw new Exception('Chart renderer must implement ' . IRenderer::class);
62
        }
63 7
64
        self::$chartRenderer = $rendererClassName;
65 7
    }
66 1
67
    public static function unsetChartRenderer(): void
68
    {
69 6
        self::$chartRenderer = null;
70
    }
71
72 1
    /**
73
     * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
74 1
     *
75
     * @return null|class-string<IRenderer> Class name of the chart renderer
76
     *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
77
     */
78
    public static function getChartRenderer(): ?string
79
    {
80
        return self::$chartRenderer;
81
    }
82
83 6
    public static function htmlEntityFlags(): int
84
    {
85 6
        return ENT_COMPAT;
86
    }
87
88 533
    /**
89
     * Sets the implementation of cache that should be used for cell collection.
90 533
     */
91
    public static function setCache(?CacheInterface $cache): void
92
    {
93
        self::$cache = $cache;
94
    }
95
96
    /**
97
     * Gets the implementation of cache that is being used for cell collection.
98
     */
99
    public static function getCache(): CacheInterface
100 1
    {
101
        if (!self::$cache) {
102 1
            self::$cache = self::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1();
103
        }
104
105 1
        return self::$cache;
106
    }
107 1
108
    public static function useSimpleCacheVersion3(): bool
109
    {
110
        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
    public static function setHttpClient(ClientInterface $httpClient, RequestFactoryInterface $requestFactory): void
117
    {
118 3
        self::$httpClient = $httpClient;
119
        self::$requestFactory = $requestFactory;
120 3
    }
121
122
    /**
123
     * Unset the HTTP client configuration.
124
     */
125
    public static function unsetHttpClient(): void
126 5
    {
127
        self::$httpClient = null;
128 5
        self::$requestFactory = null;
129
    }
130
131
    /**
132
     * Get the HTTP client implementation to be used for network request.
133
     */
134 10450
    public static function getHttpClient(): ClientInterface
135
    {
136 10450
        if (!self::$httpClient || !self::$requestFactory) {
137 280
            throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
138
        }
139
140 10450
        return self::$httpClient;
141
    }
142
143 282
    /**
144
     * Get the HTTP request factory.
145 282
     */
146
    public static function getRequestFactory(): RequestFactoryInterface
147
    {
148
        if (!self::$httpClient || !self::$requestFactory) {
149
            throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
150
        }
151 4
152
        return self::$requestFactory;
153 4
    }
154
}
155