Passed
Push — v9 ( 467cbb...1836cb )
by Georges
03:07 queued 24s
created

ConfigurationOption::isItemDetailedDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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