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