Completed
Push — master ( 2b5368...128e80 )
by Georges
11s
created

ConfigurationOption::setCacheFileExtension()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 24
rs 9.8666
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 file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> https://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
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\Util\ArrayObject;
22
23
class ConfigurationOption extends ArrayObject implements ConfigurationOptionInterface
24
{
25
    /**
26
     * @var bool
27
     */
28
    protected $itemDetailedDate = false;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $autoTmpFallback = false;
34
35
    /**
36
     * @var bool
37
     * @deprecated Do not use this option anymore
38
     */
39
    protected $ignoreSymfonyNotice = false;
40
41
    /**
42
     * @var int
43
     */
44
    protected $defaultTtl = 900;
45
46
    /**
47
     * @var string|Callable
48
     */
49
    protected $defaultKeyHashFunction = 'md5';
50
51
    /**
52
     * @var string|Callable
53
     */
54
    protected $defaultFileNameHashFunction = 'md5';
55
56
    /**
57
     * @var int
58
     */
59
    protected $defaultChmod = 0777;
60
61
    /**
62
     * @var string
63
     */
64
    protected $path = '';
65
66
    /**
67
     * @var string
68
     */
69
    protected $fallback = '';
70
71
    /**
72
     * @var \Phpfastcache\Config\ConfigurationOption
73
     */
74
    protected $fallbackConfig;
75
76
    /**
77
     * @var int
78
     */
79
    protected $limitedMemoryByObject = 4096;
80
81
    /**
82
     * @var bool
83
     */
84
    protected $compressData = false;
85
86
    /**
87
     * @var bool
88
     */
89
    protected $preventCacheSlams = false;
90
91
    /**
92
     * @var int
93
     */
94
    protected $cacheSlamsTimeout = 15;
95
96
97
    /**
98
     * @param $args
99
     * ArrayObject constructor.
100
     * @throws PhpfastcacheInvalidConfigurationException
101
     * @throws \ReflectionException
102
     */
103
    public function __construct(...$args)
104
    {
105
        parent::__construct(...$args);
106
        $array =& $this->getArray();
107
108
        /**
109
         * Detect unwanted keys and throw an exception.
110
         * No more kidding now, it's 21th century.
111
         */
112
        if (\array_diff_key($array, \get_object_vars($this))) {
113
            throw new PhpfastcacheInvalidConfigurationException(\sprintf(
114
                'Invalid option(s) for the config %s: %s',
115
                static::class,
116
                \implode(', ', \array_keys(\array_diff_key($array, \get_object_vars($this))))
117
            ));
118
        }
119
120
        foreach (\get_object_vars($this) as $property => $value) {
121
122
            if (\array_key_exists($property, $array)) {
123
                $this->$property = &$array[$property];
124
            } else {
125
                $array[$property] = &$this->$property;
126
            }
127
        }
128
129
        foreach (\get_class_methods($this) as $method) {
130
            if (\strpos($method, 'set') === 0) {
131
                $value = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
132
                try {
133
                    /**
134
                     * We use property instead of getter
135
                     * because of is/get conditions and
136
                     * to allow us to retrieve the value
137
                     * in catch statement bloc
138
                     */
139
                    $value = $this->{\lcfirst(\substr($method, 3))};
140
                    $this->{$method}($value);
141
                } catch (\TypeError $e) {
142
                    $typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value);
143
                    $reflectionMethod = new \ReflectionMethod($this, $method);
144
                    $parameter = $reflectionMethod->getParameters()[0] ?? null;
145
                    $typeHintExpected = ($parameter instanceof \ReflectionParameter ? ($parameter->getType() === 'object' ? $parameter->getClass() : $parameter->getType()) : 'Unknown type');
146
147
                    throw new PhpfastcacheInvalidConfigurationException(\sprintf(
148
                        'Invalid type hint found for "%s", expected "%s" got "%s"',
149
                        \lcfirst(\substr($method, 3)),
150
                        $typeHintExpected,
151
                        $typeHintGot
152
                    ));
153
                }
154
            }
155
        }
156
    }
157
158
    /**
159
     * @param string $optionName
160
     * @return mixed|null
161
     * @deprecated Use ->getOptionName() instead
162
     */
163
    public function getOption(string $optionName)
164
    {
165
        \trigger_error(\sprintf('Method "%s" is deprecated, use "getOptionName()" instead', __METHOD__), \E_USER_DEPRECATED);
166
        return $this->$optionName ?? null;
167
    }
168
169
    /**
170
     * @param string $optionName
171
     * @return mixed|null
172
     */
173
    public function isValidOption(string $optionName)
174
    {
175
        return \property_exists($this, $optionName);
176
    }
177
178
    /**
179
     * @return bool
180
     */
181
    public function isItemDetailedDate(): bool
182
    {
183
        return $this->itemDetailedDate;
184
    }
185
186
    /**
187
     * @param bool $itemDetailedDate
188
     * @return ConfigurationOption
189
     */
190
    public function setItemDetailedDate(bool $itemDetailedDate): self
191
    {
192
        $this->itemDetailedDate = $itemDetailedDate;
193
        return $this;
194
    }
195
196
    /**
197
     * @return bool
198
     */
199
    public function isAutoTmpFallback(): bool
200
    {
201
        return $this->autoTmpFallback;
202
    }
203
204
    /**
205
     * @param bool $autoTmpFallback
206
     * @return ConfigurationOption
207
     */
208
    public function setAutoTmpFallback(bool $autoTmpFallback): self
209
    {
210
        $this->autoTmpFallback = $autoTmpFallback;
211
        return $this;
212
    }
213
214
    /**
215
     * @return bool
216
     * @deprecated As of V7
217
     */
218
    public function isIgnoreSymfonyNotice(): bool
219
    {
220
        return $this->ignoreSymfonyNotice;
0 ignored issues
show
Deprecated Code introduced by
The property Phpfastcache\Config\Conf...n::$ignoreSymfonyNotice has been deprecated: Do not use this option anymore ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

220
        return /** @scrutinizer ignore-deprecated */ $this->ignoreSymfonyNotice;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
221
    }
222
223
    /**
224
     * @param bool $ignoreSymfonyNotice
225
     * @return ConfigurationOption
226
     * @deprecated As of V7
227
     */
228
    public function setIgnoreSymfonyNotice(bool $ignoreSymfonyNotice): self
229
    {
230
        if ($ignoreSymfonyNotice) {
231
            \trigger_error('Configuration option "ignoreSymfonyNotice" is deprecated as of the V7', \E_USER_DEPRECATED);
232
        }
233
        $this->ignoreSymfonyNotice = $ignoreSymfonyNotice;
0 ignored issues
show
Deprecated Code introduced by
The property Phpfastcache\Config\Conf...n::$ignoreSymfonyNotice has been deprecated: Do not use this option anymore ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

233
        /** @scrutinizer ignore-deprecated */ $this->ignoreSymfonyNotice = $ignoreSymfonyNotice;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
234
        return $this;
235
    }
236
237
    /**
238
     * @return int
239
     */
240
    public function getDefaultTtl(): int
241
    {
242
        return $this->defaultTtl;
243
    }
244
245
    /**
246
     * @param int $defaultTtl
247
     * @return ConfigurationOption
248
     */
249
    public function setDefaultTtl(int $defaultTtl): self
250
    {
251
        $this->defaultTtl = $defaultTtl;
252
        return $this;
253
    }
254
255
    /**
256
     * @return Callable|string
257
     */
258
    public function getDefaultKeyHashFunction()
259
    {
260
        return $this->defaultKeyHashFunction;
261
    }
262
263
    /**
264
     * @param Callable|string $defaultKeyHashFunction
265
     * @return ConfigurationOption
266
     * @throws  PhpfastcacheInvalidConfigurationException
267
     */
268
    public function setDefaultKeyHashFunction($defaultKeyHashFunction): self
269
    {
270
        if (!\is_callable($defaultKeyHashFunction) && (\is_string($defaultKeyHashFunction) && !\function_exists($defaultKeyHashFunction))) {
271
            throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string');
272
        }
273
        $this->defaultKeyHashFunction = $defaultKeyHashFunction;
274
        return $this;
275
    }
276
277
    /**
278
     * @return Callable|string
279
     */
280
    public function getDefaultFileNameHashFunction()
281
    {
282
        return $this->defaultFileNameHashFunction;
283
    }
284
285
    /**
286
     * @param Callable|string $defaultFileNameHashFunction
287
     * @return ConfigurationOption
288
     * @throws  PhpfastcacheInvalidConfigurationException
289
     */
290
    public function setDefaultFileNameHashFunction($defaultFileNameHashFunction): self
291
    {
292
        if (!\is_callable($defaultFileNameHashFunction) && (\is_string($defaultFileNameHashFunction) && !\function_exists($defaultFileNameHashFunction))) {
293
            throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string');
294
        }
295
        $this->defaultFileNameHashFunction = $defaultFileNameHashFunction;
296
        return $this;
297
    }
298
299
    /**
300
     * @return int
301
     */
302
    public function getDefaultChmod(): int
303
    {
304
        return $this->defaultChmod;
305
    }
306
307
    /**
308
     * @param int $defaultChmod
309
     * @return ConfigurationOption
310
     */
311
    public function setDefaultChmod(int $defaultChmod): self
312
    {
313
        $this->defaultChmod = $defaultChmod;
314
        return $this;
315
    }
316
317
    /**
318
     * @return string
319
     */
320
    public function getPath(): string
321
    {
322
        return $this->path;
323
    }
324
325
    /**
326
     * @param string $path
327
     * @return ConfigurationOption
328
     */
329
    public function setPath(string $path): self
330
    {
331
        $this->path = $path;
332
        return $this;
333
    }
334
335
    /**
336
     * @return bool|string
337
     */
338
    public function getFallback()
339
    {
340
        return $this->fallback;
341
    }
342
343
    /**
344
     * @param string $fallback
345
     * @return ConfigurationOption
346
     */
347
    public function setFallback(string $fallback): self
348
    {
349
        $this->fallback = $fallback;
350
        return $this;
351
    }
352
353
    /**
354
     * @return \Phpfastcache\Config\ConfigurationOption|null
355
     */
356
    public function getFallbackConfig()
357
    {
358
        return $this->fallbackConfig;
359
    }
360
361
    /**
362
     * @param \Phpfastcache\Config\ConfigurationOption|null $fallbackConfig
363
     * @return ConfigurationOption
364
     * @throws PhpfastcacheInvalidArgumentException
365
     */
366
    public function setFallbackConfig($fallbackConfig): self
367
    {
368
        if ($fallbackConfig !== null && !($fallbackConfig instanceof self)) {
0 ignored issues
show
introduced by
$fallbackConfig is always a sub-type of self.
Loading history...
369
            throw new PhpfastcacheInvalidArgumentException(\sprintf(
370
                'Invalid argument "%s" for %s',
371
                \is_object($fallbackConfig) ? \get_class($fallbackConfig) : \gettype($fallbackConfig),
372
                __METHOD__
373
            ));
374
        }
375
        $this->fallbackConfig = $fallbackConfig;
376
        return $this;
377
    }
378
379
    /**
380
     * @return int
381
     */
382
    public function getLimitedMemoryByObject(): int
383
    {
384
        return $this->limitedMemoryByObject;
385
    }
386
387
    /**
388
     * @param int $limitedMemoryByObject
389
     * @return ConfigurationOption
390
     */
391
    public function setLimitedMemoryByObject(int $limitedMemoryByObject): self
392
    {
393
        $this->limitedMemoryByObject = $limitedMemoryByObject;
394
        return $this;
395
    }
396
397
    /**
398
     * @return bool
399
     */
400
    public function isCompressData(): bool
401
    {
402
        return $this->compressData;
403
    }
404
405
    /**
406
     * @param bool $compressData
407
     * @return ConfigurationOption
408
     */
409
    public function setCompressData(bool $compressData): self
410
    {
411
        $this->compressData = $compressData;
412
        return $this;
413
    }
414
415
    /**
416
     * @return bool
417
     */
418
    public function isPreventCacheSlams(): bool
419
    {
420
        return $this->preventCacheSlams;
421
    }
422
423
    /**
424
     * @param bool $preventCacheSlams
425
     * @return ConfigurationOption
426
     */
427
    public function setPreventCacheSlams(bool $preventCacheSlams): self
428
    {
429
        $this->preventCacheSlams = $preventCacheSlams;
430
        return $this;
431
    }
432
433
    /**
434
     * @return int
435
     */
436
    public function getCacheSlamsTimeout(): int
437
    {
438
        return $this->cacheSlamsTimeout;
439
    }
440
441
    /**
442
     * @param int $cacheSlamsTimeout
443
     * @return ConfigurationOption
444
     */
445
    public function setCacheSlamsTimeout(int $cacheSlamsTimeout): self
446
    {
447
        $this->cacheSlamsTimeout = $cacheSlamsTimeout;
448
        return $this;
449
    }
450
}