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) { |
||
|
|
|||
| 72 | $this->userCache = new CappedMemoryCache(); |
||
| 73 | $this->systemConfig = $systemConfig; |
||
| 74 | } |
||
| 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 $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 $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 | */ |
||
| 209 | public function setUserValue($userId, $appName, $key, $value, $preCondition = null) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Getting a user defined value |
||
| 239 | * |
||
| 240 | * @param string $userId the userId of the user that we want to store the value under |
||
| 241 | * @param string $appName the appName that we stored the value under |
||
| 242 | * @param string $key the key under which the value is being stored |
||
| 243 | * @param mixed $default the default value to be returned if the value isn't set |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getUserValue($userId, $appName, $key, $default = '') { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the keys of all stored by an app for the user |
||
| 257 | * |
||
| 258 | * @param string $userId the userId of the user that we want to store the value under |
||
| 259 | * @param string $appName the appName that we stored the value under |
||
| 260 | * @return string[] |
||
| 261 | */ |
||
| 262 | public function getUserKeys($userId, $appName) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Delete a user value |
||
| 273 | * |
||
| 274 | * @param string $userId the userId of the user that we want to store the value under |
||
| 275 | * @param string $appName the appName that we stored the value under |
||
| 276 | * @param string $key the key under which the value is being stored |
||
| 277 | */ |
||
| 278 | public function deleteUserValue($userId, $appName, $key) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Delete all user values |
||
| 293 | * |
||
| 294 | * @param string $userId the userId of the user that we want to remove all values from |
||
| 295 | */ |
||
| 296 | public function deleteAllUserValues($userId) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Delete all user related values of one app |
||
| 309 | * |
||
| 310 | * @param string $appName the appName of the app that we want to remove all values from |
||
| 311 | */ |
||
| 312 | public function deleteAppFromAllUsers($appName) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns all user configs sorted by app of one user |
||
| 327 | * |
||
| 328 | * @param string $userId the user ID to get the app configs from |
||
| 329 | * @return array[] - 2 dimensional array with the following structure: |
||
| 330 | * [ $appId => |
||
| 331 | * [ $key => $value ] |
||
| 332 | * ] |
||
| 333 | */ |
||
| 334 | private function getUserValues($userId) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs. |
||
| 357 | * |
||
| 358 | * @param string $appName app to get the value for |
||
| 359 | * @param string $key the key to get the value for |
||
| 360 | * @param array $userIds the user IDs to fetch the values for |
||
| 361 | * @return array Mapped values: userId => value |
||
| 362 | */ |
||
| 363 | public function getUserValueForUsers($appName, $key, $userIds) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Determines the users that have the given value set for a specific app-key-pair |
||
| 399 | * |
||
| 400 | * @param string $appName the app to get the user for |
||
| 401 | * @param string $key the key to get the user for |
||
| 402 | * @param string $value the value to get the user for |
||
| 403 | * @return array of user IDs |
||
| 404 | */ |
||
| 405 | public function getUsersForUserValue($appName, $key, $value) { |
||
| 428 | } |
||
| 429 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.