77use InvalidArgumentException ;
88use PhpList \Core \Domain \Configuration \Model \ConfigOption ;
99use PhpList \Core \Domain \Configuration \Repository \ConfigRepository ;
10+ use Psr \SimpleCache \CacheInterface ;
1011
1112class ConfigProvider
1213{
@@ -15,11 +16,11 @@ class ConfigProvider
1516 ConfigOption::SendSubscribeMessage,
1617 ];
1718
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+ ) {
2324 }
2425
2526 public function isEnabled (ConfigOption $ key ): bool
@@ -35,11 +36,35 @@ public function isEnabled(ConfigOption $key): bool
3536 /**
3637 * Get configuration value by its key
3738 */
38- public function getValue (string $ ikey , ?string $ default = null ): ?string
39+ public function getValue (ConfigOption $ key , ?string $ default = null ): ?string
3940 {
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+ }
4150
42- return $ config ?->getValue() ?? $ default ;
51+ return $ value ?? $ default ;
4352 }
4453
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+ }
4570}
0 commit comments