|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
6
|
|
|
|
|
7
|
|
|
class CacheConfiguration implements CacheConfigurationInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var int|null */ |
|
10
|
|
|
private $ttl; |
|
11
|
|
|
/** @var bool */ |
|
12
|
|
|
private $enabled = false; |
|
13
|
|
|
/** @var KeyStrategyInterface */ |
|
14
|
|
|
private $strategy; |
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
private $extra; |
|
17
|
|
|
|
|
18
|
19 |
|
private function __construct() |
|
19
|
|
|
{ |
|
20
|
19 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** {@inheritdoc} */ |
|
23
|
1 |
|
public function getStrategy() |
|
24
|
|
|
{ |
|
25
|
1 |
|
return $this->enabled ? $this->strategy : null; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array $data |
|
30
|
|
|
* |
|
31
|
|
|
* @return static |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public static function create(array $data) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$resolver = new OptionsResolver(); |
|
36
|
1 |
|
self::configureResolver($resolver); |
|
37
|
1 |
|
$data = $resolver->resolve($data); |
|
38
|
|
|
|
|
39
|
1 |
|
$configuration = new static(); |
|
40
|
|
|
|
|
41
|
1 |
|
$configuration->enabled = $data['enabled']; |
|
42
|
1 |
|
$configuration->strategy = $data['strategy']; |
|
43
|
1 |
|
$configuration->ttl = $data['ttl']; |
|
44
|
|
|
|
|
45
|
1 |
|
$configuration->extra = $data['extra'] ?: []; |
|
46
|
|
|
|
|
47
|
1 |
|
return $configuration; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Constructor for disabled cache entities configuration |
|
52
|
|
|
* |
|
53
|
|
|
* @return static |
|
54
|
|
|
*/ |
|
55
|
18 |
|
public static function disabled() |
|
56
|
|
|
{ |
|
57
|
18 |
|
$configuration = new static(); |
|
58
|
18 |
|
$configuration->enabled = false; |
|
59
|
|
|
|
|
60
|
18 |
|
return $configuration; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** {@inheritdoc} */ |
|
64
|
1 |
|
public function getTtl() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return $this->enabled ? $this->ttl : null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** {@inheritdoc} */ |
|
70
|
19 |
|
public function isEnabled() |
|
71
|
|
|
{ |
|
72
|
19 |
|
return $this->enabled; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** {@inheritdoc} */ |
|
76
|
1 |
|
public function extra($key) |
|
77
|
|
|
{ |
|
78
|
1 |
|
if (!$this->enabled) { |
|
79
|
|
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
if (!array_key_exists($key, $this->extra)) { |
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return $this->extra[$key]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
private static function configureResolver(OptionsResolver $resolver) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$resolver->setDefault('enabled', false); |
|
92
|
1 |
|
$resolver->setAllowedValues('enabled', [false, true]); |
|
93
|
1 |
|
$resolver->setDefault('ttl', null); |
|
94
|
1 |
|
$resolver->setAllowedTypes('ttl', ['int', 'null']); |
|
95
|
1 |
|
$resolver->setDefault('strategy', new ScalarKeyStrategy()); |
|
96
|
1 |
|
$resolver->setAllowedTypes('strategy', KeyStrategyInterface::class); |
|
97
|
1 |
|
$resolver->setDefault('extra', []); |
|
98
|
1 |
|
$resolver->setAllowedTypes('extra', ['array', null]); |
|
99
|
1 |
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|