Passed
Push — v9 ( cdb3b5...611ee9 )
by Georges
02:15
created

ConfigurationOption::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * This file is part of Phpfastcache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
9
 *
10
 * @author Georges.L (Geolim4)  <[email protected]>
11
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
12
 */
13
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Config;
17
18
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
19
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
20
21
class ConfigurationOption implements ConfigurationOptionInterface
22
{
23
    protected bool $itemDetailedDate = false;
24
25
    protected bool $autoTmpFallback = false;
26
27
    protected int $defaultTtl = 900;
28
29
    /**
30
     * @var string|callable
31
     */
32
    protected mixed $defaultKeyHashFunction = 'md5';
33
34
    /**
35
     * @var string|callable
36
     */
37
    protected mixed $defaultFileNameHashFunction = 'md5';
38
39
    protected string $path = '';
40
41
    protected bool $preventCacheSlams = false;
42
43
    protected int $cacheSlamsTimeout = 15;
44
45
    protected bool $useStaticItemCaching = true;
46
47
    protected ?object $superGlobalAccessor = null;
48
49
    /**
50
     * @throws PhpfastcacheInvalidConfigurationException
51
     */
52
    public function __construct(array $parameters = [])
53
    {
54
        foreach ($parameters as $configKey => $configVal) {
55
            try {
56
                if (\property_exists($this, $configKey)) {
57
                    $this->{'set' . \ucfirst($configKey)}($configVal);
58
                } else {
59
                    throw new PhpfastcacheInvalidConfigurationException(
60
                        sprintf(
61
                            'Invalid option for the config %s: %s',
62
                            $this::class,
63
                            $configKey
64
                        )
65
                    );
66
                }
67
            } catch (\TypeError $e) {
68
                throw new PhpfastcacheInvalidConfigurationException(
69
                    \sprintf(
70
                        'TypeError exception thrown while trying to set your configuration: %s',
71
                        $e->getMessage()
72
                    )
73
                );
74
            }
75
        }
76
    }
77
78
    public function toArray(): array
79
    {
80
        return get_object_vars($this);
81
    }
82
83
    /**
84
     * @param string $optionName
85
     * @return bool
86
     */
87
    public function isValidOption(string $optionName): bool
88
    {
89
        return property_exists($this, $optionName);
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function isItemDetailedDate(): bool
96
    {
97
        return $this->itemDetailedDate;
98
    }
99
100
    /**
101
     * @param bool $itemDetailedDate
102
     * @return ConfigurationOption
103
     */
104
    public function setItemDetailedDate(bool $itemDetailedDate): static
105
    {
106
        $this->itemDetailedDate = $itemDetailedDate;
107
        return $this;
108
    }
109
110
    /**
111
     * @return bool
112
     */
113
    public function isAutoTmpFallback(): bool
114
    {
115
        return $this->autoTmpFallback;
116
    }
117
118
    /**
119
     * @param bool $autoTmpFallback
120
     * @return ConfigurationOption
121
     */
122
    public function setAutoTmpFallback(bool $autoTmpFallback): static
123
    {
124
        $this->autoTmpFallback = $autoTmpFallback;
125
        return $this;
126
    }
127
128
    /**
129
     * @return int
130
     */
131
    public function getDefaultTtl(): int
132
    {
133
        return $this->defaultTtl;
134
    }
135
136
    /**
137
     * @param int $defaultTtl
138
     * @return ConfigurationOption
139
     */
140
    public function setDefaultTtl(int $defaultTtl): static
141
    {
142
        $this->defaultTtl = $defaultTtl;
143
        return $this;
144
    }
145
146
    /**
147
     * @return callable|string
148
     */
149
    public function getDefaultKeyHashFunction(): callable|string
150
    {
151
        return $this->defaultKeyHashFunction;
152
    }
153
154
    /**
155
     * @param callable|string $defaultKeyHashFunction
156
     * @return ConfigurationOption
157
     * @throws  PhpfastcacheInvalidConfigurationException
158
     */
159
    public function setDefaultKeyHashFunction(callable|string $defaultKeyHashFunction): static
160
    {
161
        if ($defaultKeyHashFunction && !\is_callable($defaultKeyHashFunction) && (\is_string($defaultKeyHashFunction) && !\function_exists($defaultKeyHashFunction))) {
162
            throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string');
163
        }
164
        $this->defaultKeyHashFunction = $defaultKeyHashFunction;
165
        return $this;
166
    }
167
168
    /**
169
     * @return callable|string
170
     */
171
    public function getDefaultFileNameHashFunction(): callable|string
172
    {
173
        return $this->defaultFileNameHashFunction;
174
    }
175
176
    /**
177
     * @param callable|string $defaultFileNameHashFunction
178
     * @return ConfigurationOption
179
     * @throws  PhpfastcacheInvalidConfigurationException
180
     */
181
    public function setDefaultFileNameHashFunction(callable|string $defaultFileNameHashFunction): static
182
    {
183
        if (!\is_callable($defaultFileNameHashFunction) && (\is_string($defaultFileNameHashFunction) && !\function_exists($defaultFileNameHashFunction))) {
184
            throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string');
185
        }
186
        $this->defaultFileNameHashFunction = $defaultFileNameHashFunction;
187
        return $this;
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function getPath(): string
194
    {
195
        return $this->path;
196
    }
197
198
    /**
199
     * @param string $path
200
     * @return ConfigurationOption
201
     */
202
    public function setPath(string $path): static
203
    {
204
        $this->path = $path;
205
        return $this;
206
    }
207
208
    /**
209
     * @return bool
210
     */
211
    public function isPreventCacheSlams(): bool
212
    {
213
        return $this->preventCacheSlams;
214
    }
215
216
    /**
217
     * @param bool $preventCacheSlams
218
     * @return ConfigurationOption
219
     */
220
    public function setPreventCacheSlams(bool $preventCacheSlams): static
221
    {
222
        $this->preventCacheSlams = $preventCacheSlams;
223
        return $this;
224
    }
225
226
    /**
227
     * @return int
228
     */
229
    public function getCacheSlamsTimeout(): int
230
    {
231
        return $this->cacheSlamsTimeout;
232
    }
233
234
    /**
235
     * @param int $cacheSlamsTimeout
236
     * @return ConfigurationOption
237
     */
238
    public function setCacheSlamsTimeout(int $cacheSlamsTimeout): static
239
    {
240
        $this->cacheSlamsTimeout = $cacheSlamsTimeout;
241
        return $this;
242
    }
243
244
    /**
245
     * @return bool
246
     */
247
    public function isUseStaticItemCaching(): bool
248
    {
249
        return $this->useStaticItemCaching;
250
    }
251
252
    /**
253
     * @param bool $useStaticItemCaching
254
     * @return ConfigurationOption
255
     */
256
    public function setUseStaticItemCaching(bool $useStaticItemCaching): static
257
    {
258
        $this->useStaticItemCaching = $useStaticItemCaching;
259
        return $this;
260
    }
261
262
263
    /**
264
     * @return object
265
     * @throws PhpfastcacheInvalidArgumentException
266
     */
267
    public function getSuperGlobalAccessor(): object
268
    {
269
        if (!isset($this->superGlobalAccessor)) {
270
            $this->setSuperGlobalAccessor($this->getDefaultSuperGlobalAccessor());
271
        }
272
        return $this->superGlobalAccessor;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->superGlobalAccessor could return the type null which is incompatible with the type-hinted return object. Consider adding an additional type-check to rule them out.
Loading history...
273
    }
274
275
    /**
276
     * @param ?object $superGlobalAccessor
277
     * @return static
278
     * @throws PhpfastcacheInvalidArgumentException
279
     */
280
    public function setSuperGlobalAccessor(?object $superGlobalAccessor): static
281
    {
282
        /**
283
         *  Symfony's implementation for users that want a good control of their code:
284
         *
285
         *  $config['superGlobalAccessor'] = \Closure::fromCallable(static function(string $superGlobalName, string $keyName) use ($request) {
286
         *      return match ($superGlobalName) {
287
         *          'SERVER' => $request->server->get($keyName),
288
         *          'REQUEST' => $request->request->get($keyName),
289
         *      };
290
         *  });
291
         */
292
293
        if ($superGlobalAccessor === null) {
294
            $this->superGlobalAccessor = $this->getDefaultSuperGlobalAccessor();
295
        } elseif (!\is_callable($superGlobalAccessor)) {
296
            throw new PhpfastcacheInvalidArgumentException('The "superGlobalAccessor" callback must be callable using "__invoke" or \Closure implementation');
297
        } else {
298
            $this->superGlobalAccessor = $superGlobalAccessor;
299
        }
300
301
        return $this;
302
    }
303
304
    /**
305
     * @return \Closure
306
     * @SuppressWarnings(PHPMD.Superglobals)
307
     */
308
    protected function getDefaultSuperGlobalAccessor(): \Closure
309
    {
310
        return \Closure::fromCallable(static function (string $superGlobalName, ?string $keyName = null) {
311
            return match ($superGlobalName) {
312
                'SERVER' => $keyName !== null ? $_SERVER[$keyName] ?? null : $_SERVER,
313
                'REQUEST' => $keyName !== null ? $_REQUEST[$keyName] ?? null : $_REQUEST,
314
                'COOKIE' => $keyName !== null ? $_COOKIE[$keyName] ?? null : $_COOKIE,
315
            };
316
        });
317
    }
318
}
319