7
7
use InvalidArgumentException ;
8
8
use PhpList \Core \Domain \Configuration \Model \ConfigOption ;
9
9
use PhpList \Core \Domain \Configuration \Repository \ConfigRepository ;
10
+ use Psr \SimpleCache \CacheInterface ;
10
11
11
12
class ConfigProvider
12
13
{
@@ -15,11 +16,11 @@ class ConfigProvider
15
16
ConfigOption::SendSubscribeMessage,
16
17
];
17
18
18
- private ConfigRepository $ configRepository ;
19
-
20
- public function __construct ( ConfigRepository $ configRepository )
21
- {
22
- $ this -> configRepository = $ configRepository ;
19
+ public function __construct (
20
+ private readonly ConfigRepository $ configRepository ,
21
+ private readonly CacheInterface $ cache ,
22
+ private readonly int $ ttlSeconds = 300
23
+ ) {
23
24
}
24
25
25
26
public function isEnabled (ConfigOption $ key ): bool
@@ -35,11 +36,35 @@ public function isEnabled(ConfigOption $key): bool
35
36
/**
36
37
* Get configuration value by its key
37
38
*/
38
- public function getValue (string $ ikey , ?string $ default = null ): ?string
39
+ public function getValue (ConfigOption $ key , ?string $ default = null ): ?string
39
40
{
40
- $ config = $ this ->configRepository ->findOneBy (['item ' => $ ikey ]);
41
+ if (in_array ($ key , $ this ->booleanValues )) {
42
+ throw new InvalidArgumentException ('Key is a boolean value, use isEnabled instead ' );
43
+ }
44
+ $ cacheKey = 'cfg: ' . $ key ->value ;
45
+ $ value = $ this ->cache ->get ($ cacheKey );
46
+ if ($ value === null ) {
47
+ $ value = $ this ->configRepository ->findValueByItem ($ key ->value );
48
+ $ this ->cache ->set ($ cacheKey , $ value , $ this ->ttlSeconds );
49
+ }
41
50
42
- return $ config ?->getValue() ?? $ default ;
51
+ return $ value ?? $ default ;
43
52
}
44
53
54
+ public function getValueWithNamespace (ConfigOption $ key , ?string $ default = null ): ?string
55
+ {
56
+ $ full = $ this ->getValue ($ key );
57
+ if ($ full !== null && $ full !== '' ) {
58
+ return $ full ;
59
+ }
60
+
61
+ if (str_contains ($ key ->value , ': ' )) {
62
+ [$ parent ] = explode (': ' , $ key ->value , 2 );
63
+ $ parentKey = ConfigOption::from ($ parent );
64
+
65
+ return $ this ->getValue ($ parentKey , $ default );
66
+ }
67
+
68
+ return $ default ;
69
+ }
45
70
}
0 commit comments