Passed
Pull Request — master (#856)
by
unknown
10:26
created

ConfigurationOption::setItemDetailedDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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