2
2
3
3
namespace Hengebytes \SettingBundle \Handler ;
4
4
5
+ use Doctrine \ORM \EntityManagerInterface ;
5
6
use Hengebytes \SettingBundle \Entity \Setting ;
6
7
use Hengebytes \SettingBundle \Interfaces \SettingHandlerInterface ;
7
8
use Hengebytes \SettingBundle \Service \CryptoService ;
8
- use Doctrine \Persistence \{ManagerRegistry , ObjectManager , ObjectRepository };
9
9
10
10
class SettingHandler implements SettingHandlerInterface
11
11
{
12
12
protected const string SETTING_NOT_DEFINED_INDICATOR = "\n! \t" ;
13
13
/** setting with this prefix will be first priority */
14
- protected string $ overridePrefix = '' ;
14
+ protected ? string $ overridePrefix = null ;
15
15
16
- protected ObjectManager $ em ;
17
- protected ObjectRepository $ repository ;
18
- protected CryptoService $ cryptoService ;
19
16
private array $ runTimeStorage = [];
20
17
21
- public function __construct (ManagerRegistry $ objectManager , protected string $ entityClass , CryptoService $ cryptoService )
22
- {
23
- $ this ->em = $ objectManager ->getManager ();
24
- $ this ->repository = $ this ->em ->getRepository ($ entityClass );
25
- $ this ->cryptoService = $ cryptoService ;
18
+ public function __construct (
19
+ protected EntityManagerInterface $ em , protected string $ entityClass , protected CryptoService $ cryptoService
20
+ ) {
26
21
}
27
22
28
23
public function set (string $ name , string $ value , bool $ isSensitive = false ): void
@@ -33,9 +28,9 @@ public function set(string $name, string $value, bool $isSensitive = false): voi
33
28
}
34
29
35
30
/** @var Setting $setting */
36
- $ setting = $ this ->repository ->findOneBy (['name ' => $ name ]);
31
+ $ setting = $ this ->em -> getRepository ( $ this -> entityClass ) ->findOneBy (['name ' => $ name ]);
37
32
38
- if ($ setting !== null ) {
33
+ if ($ setting ) {
39
34
$ setting ->value = $ value ;
40
35
$ setting ->isSensitive = $ isSensitive ;
41
36
} else {
@@ -69,9 +64,11 @@ public function isProductionEnvironment(): bool
69
64
70
65
public function get (string $ name , $ default = null ): ?string
71
66
{
72
- $ settingValue = $ this ->innerGet ($ this ->overridePrefix . $ name ) ?? $ this ->innerGet ($ name );
67
+ if ($ this ->overridePrefix !== null ) {
68
+ return $ this ->innerGet ($ this ->overridePrefix . $ name ) ?? $ this ->innerGet ($ name ) ?? $ default ;
69
+ }
73
70
74
- return $ settingValue ?? $ default ;
71
+ return $ this -> innerGet ( $ name ) ?? $ default ;
75
72
}
76
73
77
74
protected function innerGet (string $ name ): ?string
@@ -82,9 +79,10 @@ protected function innerGet(string $name): ?string
82
79
}
83
80
84
81
/** @var Setting $setting */
85
- $ setting = $ this ->repository ->findOneBy (['name ' => $ name ]);
82
+ $ setting = $ this ->em -> getRepository ( $ this -> entityClass ) ->findOneBy (['name ' => $ name ]);
86
83
if ($ setting === null ) {
87
84
$ this ->setRunTime ($ name , self ::SETTING_NOT_DEFINED_INDICATOR );
85
+
88
86
return null ;
89
87
}
90
88
@@ -97,15 +95,46 @@ protected function innerGet(string $name): ?string
97
95
return $ value ;
98
96
}
99
97
100
- public function getRunTime (string $ name ): ?string
98
+ public function getMultiple (array $ names ): array
99
+ {
100
+ $ result = [];
101
+ foreach ($ names as $ key => $ name ) {
102
+ $ runTimeValue = $ this ->getRunTime ($ name );
103
+ if ($ runTimeValue === null ) {
104
+ continue ;
105
+ }
106
+ $ result [$ name ] = $ runTimeValue !== self ::SETTING_NOT_DEFINED_INDICATOR ? $ runTimeValue : null ;
107
+ unset($ names [$ key ]);
108
+ }
109
+
110
+ if (!$ names ) {
111
+ return $ result ;
112
+ }
113
+ $ settings = $ this ->em ->getRepository ($ this ->entityClass )->findBy (['name ' => $ names ]);
114
+
115
+ foreach ($ settings as $ setting ) {
116
+ $ value = $ setting ->value ;
117
+ if ($ setting ->isSensitive ) {
118
+ $ value = $ this ->cryptoService ->decrypt ($ value );
119
+ }
120
+ $ result [$ setting ->name ] = $ value ;
121
+
122
+ $ this ->setRunTime ($ setting ->name , $ value );
123
+ }
124
+
125
+ return $ result ;
126
+ }
127
+
128
+
129
+ private function getRunTime (string $ name ): ?string
101
130
{
102
131
return $ this ->runTimeStorage [$ name ] ?? null ;
103
132
}
104
133
105
134
public function getGrouped (): array
106
135
{
107
136
return $ this ->group (
108
- $ this ->repository ->findAll ()
137
+ $ this ->em -> getRepository ( $ this -> entityClass ) ->findAll ()
109
138
);
110
139
}
111
140
@@ -134,27 +163,28 @@ private function group(array $settings): array
134
163
public function remove (string $ name ): void
135
164
{
136
165
/** @var Setting $setting */
137
- $ setting = $ this ->repository ->findOneBy (['name ' => $ name ]);
166
+ $ setting = $ this ->em -> getRepository ( $ this -> entityClass ) ->findOneBy (['name ' => $ name ]);
138
167
if ($ setting !== null ) {
139
168
$ this ->em ->remove ($ setting );
140
169
$ this ->em ->flush ();
141
170
$ this ->setRunTime ($ name , self ::SETTING_NOT_DEFINED_INDICATOR );
142
171
}
143
172
}
144
173
145
- public function setOverridePrefix (string $ prefix ): void
174
+ public function setOverridePrefix (? string $ prefix ): void
146
175
{
147
176
$ this ->overridePrefix = $ prefix ;
148
177
}
149
178
150
179
private function maskSensitiveString (string $ input ): string
151
180
{
152
181
$ input = $ this ->cryptoService ->decrypt ($ input );
182
+ if (strlen ($ input ) < 5 ) {
183
+ return "**** " ;
184
+ }
153
185
154
- $ start = substr ($ input , 0 , 2 );
155
- $ end = substr ($ input , -2 );
156
- $ masked = str_repeat ('* ' , 8 );
157
-
158
- return strlen ($ input ) < 5 ? "**** " : $ start . $ masked . $ end ;
186
+ return substr ($ input , 0 , 2 )
187
+ . str_repeat ('* ' , 8 )
188
+ . substr ($ input , -2 );
159
189
}
160
190
}
0 commit comments