@@ -20,7 +20,6 @@ class ConfigManager {
2020 public function __construct (
2121 private readonly AppConfig $ appConfig ,
2222 private readonly UserConfig $ userConfig ,
23- private readonly IAppManager $ appManager ,
2423 private readonly LoggerInterface $ logger ,
2524 ) {
2625 }
@@ -41,7 +40,8 @@ public function __construct(
4140 public function migrateConfigLexiconKeys (?string $ appId = null ): void {
4241 if ($ appId === null ) {
4342 $ this ->migrateConfigLexiconKeys ('core ' );
44- foreach ($ this ->appManager ->getEnabledApps () as $ app ) {
43+ $ appManager = \OCP \Server::get (IAppManager::class);
44+ foreach ($ appManager ->getEnabledApps () as $ app ) {
4545 $ this ->migrateConfigLexiconKeys ($ app );
4646 }
4747
@@ -124,7 +124,7 @@ private function migrateUserConfigKeys(string $appId): void {
124124 continue ;
125125 }
126126
127- foreach ($ this ->userConfig ->getValuesByUsers ($ appId , $ entry ->getRename ()) as $ userId => $ value ) {
127+ foreach ($ this ->userConfig ->getValuesByUsers ($ appId , $ entry ->getRename ()) as $ userId => $ value ) {
128128 if ($ this ->userConfig ->hasKey ($ userId , $ appId , $ entry ->getKey ())) {
129129 continue ;
130130 }
@@ -155,38 +155,19 @@ private function migrateAppConfigValue(string $appId, ConfigLexiconEntry $entry)
155155 return ;
156156
157157 case ValueType::INT :
158- if ($ value !== ((string )((int )$ value ))) {
159- throw new TypeConflictException ('Value is not an integer ' );
160- }
161- $ this ->appConfig ->setValueInt ($ appId , $ entry ->getKey (), (int )$ value );
158+ $ this ->appConfig ->setValueInt ($ appId , $ entry ->getKey (), $ this ->convertToInt ($ value ));
162159 return ;
163160
164161 case ValueType::FLOAT :
165- if ($ value !== ((string )((float )$ value ))) {
166- throw new TypeConflictException ('Value is not a float ' );
167- }
168- $ this ->appConfig ->setValueFloat ($ appId , $ entry ->getKey (), (float )$ value );
162+ $ this ->appConfig ->setValueFloat ($ appId , $ entry ->getKey (), $ this ->convertToFloat ($ value ));
169163 return ;
170164
171165 case ValueType::BOOL :
172- if (in_array (strtolower ($ value ), ['true ' , '1 ' , 'on ' , 'yes ' ])) {
173- $ valueBool = true ;
174- } elseif (in_array (strtolower ($ value ), ['false ' , '0 ' , 'off ' , 'no ' ])) {
175- $ valueBool = false ;
176- } else {
177- throw new TypeConflictException ('Value cannot be converted to boolean ' );
178- }
179- $ this ->appConfig ->setValueBool ($ appId , $ entry ->getKey (), $ valueBool );
166+ $ this ->appConfig ->setValueBool ($ appId , $ entry ->getKey (), $ this ->convertToBool ($ value , $ entry ));
180167 return ;
181168
182169 case ValueType::ARRAY :
183- try {
184- $ valueArray = json_decode ($ value , true , flags: JSON_THROW_ON_ERROR );
185- } catch (JsonException ) {
186- throw new TypeConflictException ('Value is not a valid json ' );
187- }
188- $ valueArray = (is_array ($ valueArray )) ? $ valueArray : throw new TypeConflictException ('Value is not an array ' );
189- $ this ->appConfig ->setValueArray ($ appId , $ entry ->getKey (), $ valueArray );
170+ $ this ->appConfig ->setValueArray ($ appId , $ entry ->getKey (), $ this ->convertToArray ($ value ));
190171 return ;
191172 }
192173 }
@@ -204,39 +185,64 @@ private function migrateUserConfigValue(string $userId, string $appId, ConfigLex
204185 return ;
205186
206187 case ValueType::INT :
207- if ($ value !== ((string )((int )$ value ))) {
208- throw new TypeConflictException ('Value is not an integer ' );
209- }
210- $ this ->userConfig ->setValueInt ($ userId , $ appId , $ entry ->getKey (), (int )$ value );
188+ $ this ->userConfig ->setValueInt ($ userId , $ appId , $ entry ->getKey (), $ this ->convertToInt ($ value ));
211189 return ;
212190
213191 case ValueType::FLOAT :
214- if ($ value !== ((string )((float )$ value ))) {
215- throw new TypeConflictException ('Value is not a float ' );
216- }
217- $ this ->userConfig ->setValueFloat ($ userId , $ appId , $ entry ->getKey (), (float )$ value );
192+ $ this ->userConfig ->setValueFloat ($ userId , $ appId , $ entry ->getKey (), $ this ->convertToFloat ($ value ));
218193 return ;
219194
220195 case ValueType::BOOL :
221- if (in_array (strtolower ($ value ), ['true ' , '1 ' , 'on ' , 'yes ' ])) {
222- $ valueBool = true ;
223- } elseif (in_array (strtolower ($ value ), ['false ' , '0 ' , 'off ' , 'no ' ])) {
224- $ valueBool = false ;
225- } else {
226- throw new TypeConflictException ('Value cannot be converted to boolean ' );
227- }
228- $ this ->userConfig ->setValueBool ($ userId , $ appId , $ entry ->getKey (), $ valueBool );
196+ $ this ->userConfig ->setValueBool ($ userId , $ appId , $ entry ->getKey (), $ this ->convertToBool ($ value , $ entry ));
229197 return ;
230198
231199 case ValueType::ARRAY :
232- try {
233- $ valueArray = json_decode ($ value , true , flags: JSON_THROW_ON_ERROR );
234- } catch (JsonException ) {
235- throw new TypeConflictException ('Value is not a valid json ' );
236- }
237- $ valueArray = (is_array ($ valueArray )) ? $ valueArray : throw new TypeConflictException ('Value is not an array ' );
238- $ this ->userConfig ->setValueArray ($ userId , $ appId , $ entry ->getKey (), $ valueArray );
200+ $ this ->userConfig ->setValueArray ($ userId , $ appId , $ entry ->getKey (), $ this ->convertToArray ($ value ));
239201 return ;
240202 }
241203 }
204+
205+ public function convertToInt (string $ value ): int {
206+ if ($ value !== ((string )((int )$ value ))) {
207+ throw new TypeConflictException ('Value is not an integer ' );
208+ }
209+
210+ return (int )$ value ;
211+ }
212+
213+ public function convertToFloat (string $ value ): float {
214+ if ($ value !== ((string )((float )$ value ))) {
215+ throw new TypeConflictException ('Value is not a float ' );
216+ }
217+
218+ return (float )$ value ;
219+ }
220+
221+ public function convertToBool (string $ value , ?ConfigLexiconEntry $ entry = null ): bool {
222+ if (in_array (strtolower ($ value ), ['true ' , '1 ' , 'on ' , 'yes ' ])) {
223+ $ valueBool = true ;
224+ } elseif (in_array (strtolower ($ value ), ['false ' , '0 ' , 'off ' , 'no ' ])) {
225+ $ valueBool = false ;
226+ } else {
227+ throw new TypeConflictException ('Value cannot be converted to boolean ' );
228+ }
229+ if ($ entry ?->hasOption(ConfigLexiconEntry::RENAME_INVERT_BOOLEAN ) === true ) {
230+ $ valueBool = !$ valueBool ;
231+ }
232+
233+ return $ valueBool ;
234+ }
235+
236+ public function convertToArray (string $ value ): array {
237+ try {
238+ $ valueArray = json_decode ($ value , true , flags: JSON_THROW_ON_ERROR );
239+ } catch (JsonException ) {
240+ throw new TypeConflictException ('Value is not a valid json ' );
241+ }
242+ if (!is_array ($ valueArray )) {
243+ throw new TypeConflictException ('Value is not an array ' );
244+ }
245+
246+ return $ valueArray ;
247+ }
242248}
0 commit comments