Complex classes like AllConfig often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AllConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class AllConfig implements \OCP\IConfig { |
||
| 40 | /** @var SystemConfig */ |
||
| 41 | private $systemConfig; |
||
| 42 | |||
| 43 | /** @var IDBConnection */ |
||
| 44 | private $connection; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * 3 dimensional array with the following structure: |
||
| 48 | * [ $userId => |
||
| 49 | * [ $appId => |
||
| 50 | * [ $key => $value ] |
||
| 51 | * ] |
||
| 52 | * ] |
||
| 53 | * |
||
| 54 | * database table: preferences |
||
| 55 | * |
||
| 56 | * methods that use this: |
||
| 57 | * - setUserValue |
||
| 58 | * - getUserValue |
||
| 59 | * - getUserKeys |
||
| 60 | * - deleteUserValue |
||
| 61 | * - deleteAllUserValues |
||
| 62 | * - deleteAppFromAllUsers |
||
| 63 | * |
||
| 64 | * @var CappedMemoryCache $userCache |
||
| 65 | */ |
||
| 66 | private $userCache; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param SystemConfig $systemConfig |
||
| 70 | */ |
||
| 71 | function __construct(SystemConfig $systemConfig) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * TODO - FIXME This fixes an issue with base.php that cause cyclic |
||
| 78 | * dependencies, especially with autoconfig setup |
||
| 79 | * |
||
| 80 | * Replace this by properly injected database connection. Currently the |
||
| 81 | * base.php triggers the getDatabaseConnection too early which causes in |
||
| 82 | * autoconfig setup case a too early distributed database connection and |
||
| 83 | * the autoconfig then needs to reinit all already initialized dependencies |
||
| 84 | * that use the database connection. |
||
| 85 | * |
||
| 86 | * otherwise a SQLite database is created in the wrong directory |
||
| 87 | * because the database connection was created with an uninitialized config |
||
| 88 | */ |
||
| 89 | private function fixDIInit() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Sets and deletes system wide values |
||
| 97 | * |
||
| 98 | * @param array $configs Associative array with `key => value` pairs |
||
| 99 | * If value is null, the config key will be deleted |
||
| 100 | */ |
||
| 101 | public function setSystemValues(array $configs) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Sets a new system wide value |
||
| 107 | * |
||
| 108 | * @param string $key the key of the value, under which will be saved |
||
| 109 | * @param mixed $value the value that should be stored |
||
| 110 | */ |
||
| 111 | public function setSystemValue($key, $value) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Looks up a system wide defined value |
||
| 117 | * |
||
| 118 | * @param string $key the key of the value, under which it was saved |
||
| 119 | * @param mixed $default the default value to be returned if the value isn't set |
||
| 120 | * @return mixed the value or $default |
||
| 121 | */ |
||
| 122 | public function getSystemValue($key, $default = '') { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Looks up a system wide defined value and filters out sensitive data |
||
| 128 | * |
||
| 129 | * @param string $key the key of the value, under which it was saved |
||
| 130 | * @param mixed $default the default value to be returned if the value isn't set |
||
| 131 | * @return mixed the value or $default |
||
| 132 | */ |
||
| 133 | public function getFilteredSystemValue($key, $default = '') { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Delete a system wide defined value |
||
| 139 | * |
||
| 140 | * @param string $key the key of the value, under which it was saved |
||
| 141 | */ |
||
| 142 | public function deleteSystemValue($key) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get all keys stored for an app |
||
| 148 | * |
||
| 149 | * @param string $appName the appName that we stored the value under |
||
| 150 | * @return string[] the keys stored for the app |
||
| 151 | */ |
||
| 152 | public function getAppKeys($appName) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Writes a new app wide value |
||
| 158 | * |
||
| 159 | * @param string $appName the appName that we want to store the value under |
||
| 160 | * @param string $key the key of the value, under which will be saved |
||
| 161 | * @param string|float|int $value the value that should be stored |
||
| 162 | */ |
||
| 163 | public function setAppValue($appName, $key, $value) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Looks up an app wide defined value |
||
| 169 | * |
||
| 170 | * @param string $appName the appName that we stored the value under |
||
| 171 | * @param string $key the key of the value, under which it was saved |
||
| 172 | * @param string $default the default value to be returned if the value isn't set |
||
| 173 | * @return string the saved value |
||
| 174 | */ |
||
| 175 | public function getAppValue($appName, $key, $default = '') { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Delete an app wide defined value |
||
| 181 | * |
||
| 182 | * @param string $appName the appName that we stored the value under |
||
| 183 | * @param string $key the key of the value, under which it was saved |
||
| 184 | */ |
||
| 185 | public function deleteAppValue($appName, $key) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Removes all keys in appconfig belonging to the app |
||
| 191 | * |
||
| 192 | * @param string $appName the appName the configs are stored under |
||
| 193 | */ |
||
| 194 | public function deleteAppValues($appName) { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Set a user defined value |
||
| 201 | * |
||
| 202 | * @param string $userId the userId of the user that we want to store the value under |
||
| 203 | * @param string $appName the appName that we want to store the value under |
||
| 204 | * @param string $key the key under which the value is being stored |
||
| 205 | * @param string|float|int $value the value that you want to store |
||
| 206 | * @param string $preCondition only update if the config value was previously the value passed as $preCondition |
||
| 207 | * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met |
||
| 208 | * @throws \UnexpectedValueException when trying to store an unexpected value |
||
| 209 | */ |
||
| 210 | public function setUserValue($userId, $appName, $key, $value, $preCondition = null) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Getting a user defined value |
||
| 244 | * |
||
| 245 | * @param string $userId the userId of the user that we want to store the value under |
||
| 246 | * @param string $appName the appName that we stored the value under |
||
| 247 | * @param string $key the key under which the value is being stored |
||
| 248 | * @param mixed $default the default value to be returned if the value isn't set |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getUserValue($userId, $appName, $key, $default = '') { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get the keys of all stored by an app for the user |
||
| 262 | * |
||
| 263 | * @param string $userId the userId of the user that we want to store the value under |
||
| 264 | * @param string $appName the appName that we stored the value under |
||
| 265 | * @return string[] |
||
| 266 | */ |
||
| 267 | public function getUserKeys($userId, $appName) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Delete a user value |
||
| 278 | * |
||
| 279 | * @param string $userId the userId of the user that we want to store the value under |
||
| 280 | * @param string $appName the appName that we stored the value under |
||
| 281 | * @param string $key the key under which the value is being stored |
||
| 282 | */ |
||
| 283 | public function deleteUserValue($userId, $appName, $key) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Delete all user values |
||
| 298 | * |
||
| 299 | * @param string $userId the userId of the user that we want to remove all values from |
||
| 300 | */ |
||
| 301 | public function deleteAllUserValues($userId) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Delete all user related values of one app |
||
| 314 | * |
||
| 315 | * @param string $appName the appName of the app that we want to remove all values from |
||
| 316 | */ |
||
| 317 | public function deleteAppFromAllUsers($appName) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns all user configs sorted by app of one user |
||
| 332 | * |
||
| 333 | * @param string $userId the user ID to get the app configs from |
||
| 334 | * @return array[] - 2 dimensional array with the following structure: |
||
| 335 | * [ $appId => |
||
| 336 | * [ $key => $value ] |
||
| 337 | * ] |
||
| 338 | */ |
||
| 339 | private function getUserValues($userId) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs. |
||
| 362 | * |
||
| 363 | * @param string $appName app to get the value for |
||
| 364 | * @param string $key the key to get the value for |
||
| 365 | * @param array $userIds the user IDs to fetch the values for |
||
| 366 | * @return array Mapped values: userId => value |
||
| 367 | */ |
||
| 368 | public function getUserValueForUsers($appName, $key, $userIds) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Determines the users that have the given value set for a specific app-key-pair |
||
| 404 | * |
||
| 405 | * @param string $appName the app to get the user for |
||
| 406 | * @param string $key the key to get the user for |
||
| 407 | * @param string $value the value to get the user for |
||
| 408 | * @return array of user IDs |
||
| 409 | */ |
||
| 410 | public function getUsersForUserValue($appName, $key, $value) { |
||
| 448 | } |
||
| 449 |