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 |
||
| 36 | class AllConfig implements \OCP\IConfig { |
||
| 37 | /** @var SystemConfig */ |
||
| 38 | private $systemConfig; |
||
| 39 | |||
| 40 | /** @var IDBConnection */ |
||
| 41 | private $connection; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * 3 dimensional array with the following structure: |
||
| 45 | * [ $userId => |
||
| 46 | * [ $appId => |
||
| 47 | * [ $key => $value ] |
||
| 48 | * ] |
||
| 49 | * ] |
||
| 50 | * |
||
| 51 | * database table: preferences |
||
| 52 | * |
||
| 53 | * methods that use this: |
||
| 54 | * - setUserValue |
||
| 55 | * - getUserValue |
||
| 56 | * - getUserKeys |
||
| 57 | * - deleteUserValue |
||
| 58 | * - deleteAllUserValues |
||
| 59 | * - deleteAppFromAllUsers |
||
| 60 | * |
||
| 61 | * @var array $userCache |
||
| 62 | */ |
||
| 63 | private $userCache = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param SystemConfig $systemConfig |
||
| 67 | */ |
||
| 68 | function __construct(SystemConfig $systemConfig) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * TODO - FIXME This fixes an issue with base.php that cause cyclic |
||
| 74 | * dependencies, especially with autoconfig setup |
||
| 75 | * |
||
| 76 | * Replace this by properly injected database connection. Currently the |
||
| 77 | * base.php triggers the getDatabaseConnection too early which causes in |
||
| 78 | * autoconfig setup case a too early distributed database connection and |
||
| 79 | * the autoconfig then needs to reinit all already initialized dependencies |
||
| 80 | * that use the database connection. |
||
| 81 | * |
||
| 82 | * otherwise a SQLite database is created in the wrong directory |
||
| 83 | * because the database connection was created with an uninitialized config |
||
| 84 | */ |
||
| 85 | private function fixDIInit() { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Sets and deletes system wide values |
||
| 93 | * |
||
| 94 | * @param array $configs Associative array with `key => value` pairs |
||
| 95 | * If value is null, the config key will be deleted |
||
| 96 | */ |
||
| 97 | public function setSystemValues(array $configs) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Sets a new system wide value |
||
| 103 | * |
||
| 104 | * @param string $key the key of the value, under which will be saved |
||
| 105 | * @param mixed $value the value that should be stored |
||
| 106 | */ |
||
| 107 | public function setSystemValue($key, $value) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Looks up a system wide defined value |
||
| 113 | * |
||
| 114 | * @param string $key the key of the value, under which it was saved |
||
| 115 | * @param mixed $default the default value to be returned if the value isn't set |
||
| 116 | * @return mixed the value or $default |
||
| 117 | */ |
||
| 118 | public function getSystemValue($key, $default = '') { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Looks up a system wide defined value and filters out sensitive data |
||
| 124 | * |
||
| 125 | * @param string $key the key of the value, under which it was saved |
||
| 126 | * @param mixed $default the default value to be returned if the value isn't set |
||
| 127 | * @return mixed the value or $default |
||
| 128 | */ |
||
| 129 | public function getFilteredSystemValue($key, $default = '') { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Delete a system wide defined value |
||
| 135 | * |
||
| 136 | * @param string $key the key of the value, under which it was saved |
||
| 137 | */ |
||
| 138 | public function deleteSystemValue($key) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get all keys stored for an app |
||
| 144 | * |
||
| 145 | * @param string $appName the appName that we stored the value under |
||
| 146 | * @return string[] the keys stored for the app |
||
| 147 | */ |
||
| 148 | public function getAppKeys($appName) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Writes a new app wide value |
||
| 154 | * |
||
| 155 | * @param string $appName the appName that we want to store the value under |
||
| 156 | * @param string $key the key of the value, under which will be saved |
||
| 157 | * @param string|float|int $value the value that should be stored |
||
| 158 | */ |
||
| 159 | public function setAppValue($appName, $key, $value) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Looks up an app wide defined value |
||
| 165 | * |
||
| 166 | * @param string $appName the appName that we stored the value under |
||
| 167 | * @param string $key the key of the value, under which it was saved |
||
| 168 | * @param string $default the default value to be returned if the value isn't set |
||
| 169 | * @return string the saved value |
||
| 170 | */ |
||
| 171 | public function getAppValue($appName, $key, $default = '') { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Delete an app wide defined value |
||
| 177 | * |
||
| 178 | * @param string $appName the appName that we stored the value under |
||
| 179 | * @param string $key the key of the value, under which it was saved |
||
| 180 | */ |
||
| 181 | public function deleteAppValue($appName, $key) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Removes all keys in appconfig belonging to the app |
||
| 187 | * |
||
| 188 | * @param string $appName the appName the configs are stored under |
||
| 189 | */ |
||
| 190 | public function deleteAppValues($appName) { |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Set a user defined value |
||
| 197 | * |
||
| 198 | * @param string $userId the userId of the user that we want to store the value under |
||
| 199 | * @param string $appName the appName that we want to store the value under |
||
| 200 | * @param string $key the key under which the value is being stored |
||
| 201 | * @param string|float|int $value the value that you want to store |
||
| 202 | * @param string $preCondition only update if the config value was previously the value passed as $preCondition |
||
| 203 | * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met |
||
| 204 | * @throws \UnexpectedValueException when trying to store an unexpected value |
||
| 205 | */ |
||
| 206 | public function setUserValue($userId, $appName, $key, $value, $preCondition = null) { |
||
| 207 | if (!is_int($value) && !is_float($value) && !is_string($value)) { |
||
| 208 | throw new \UnexpectedValueException('Only integers, floats and strings are allowed as value'); |
||
| 209 | } |
||
| 210 | |||
| 211 | // TODO - FIXME |
||
| 212 | $this->fixDIInit(); |
||
| 213 | |||
| 214 | $preconditionArray = []; |
||
| 215 | if (isset($preCondition)) { |
||
| 216 | $preconditionArray = [ |
||
| 217 | 'configvalue' => $preCondition, |
||
| 218 | ]; |
||
| 219 | } |
||
| 220 | |||
| 221 | $this->connection->setValues('preferences', [ |
||
| 222 | 'userid' => $userId, |
||
| 223 | 'appid' => $appName, |
||
| 224 | 'configkey' => $key, |
||
| 225 | ], [ |
||
| 226 | 'configvalue' => $value, |
||
| 227 | ], $preconditionArray); |
||
| 228 | |||
| 229 | // only add to the cache if we already loaded data for the user |
||
| 230 | if (isset($this->userCache[$userId])) { |
||
| 231 | if (!isset($this->userCache[$userId][$appName])) { |
||
| 232 | $this->userCache[$userId][$appName] = array(); |
||
| 233 | } |
||
| 234 | $this->userCache[$userId][$appName][$key] = $value; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Getting a user defined value |
||
| 240 | * |
||
| 241 | * @param string $userId the userId of the user that we want to store the value under |
||
| 242 | * @param string $appName the appName that we stored the value under |
||
| 243 | * @param string $key the key under which the value is being stored |
||
| 244 | * @param mixed $default the default value to be returned if the value isn't set |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getUserValue($userId, $appName, $key, $default = '') { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get the keys of all stored by an app for the user |
||
| 258 | * |
||
| 259 | * @param string $userId the userId of the user that we want to store the value under |
||
| 260 | * @param string $appName the appName that we stored the value under |
||
| 261 | * @return string[] |
||
| 262 | */ |
||
| 263 | public function getUserKeys($userId, $appName) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Delete a user value |
||
| 274 | * |
||
| 275 | * @param string $userId the userId of the user that we want to store the value under |
||
| 276 | * @param string $appName the appName that we stored the value under |
||
| 277 | * @param string $key the key under which the value is being stored |
||
| 278 | */ |
||
| 279 | public function deleteUserValue($userId, $appName, $key) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Delete all user values |
||
| 294 | * |
||
| 295 | * @param string $userId the userId of the user that we want to remove all values from |
||
| 296 | */ |
||
| 297 | public function deleteAllUserValues($userId) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Delete all user related values of one app |
||
| 310 | * |
||
| 311 | * @param string $appName the appName of the app that we want to remove all values from |
||
| 312 | */ |
||
| 313 | public function deleteAppFromAllUsers($appName) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Returns all user configs sorted by app of one user |
||
| 328 | * |
||
| 329 | * @param string $userId the user ID to get the app configs from |
||
| 330 | * @return array[] - 2 dimensional array with the following structure: |
||
| 331 | * [ $appId => |
||
| 332 | * [ $key => $value ] |
||
| 333 | * ] |
||
| 334 | */ |
||
| 335 | private function getUserValues($userId) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Fetches a mapped list of userId -> value, for a specified app and key and a list of user IDs. |
||
| 358 | * |
||
| 359 | * @param string $appName app to get the value for |
||
| 360 | * @param string $key the key to get the value for |
||
| 361 | * @param array $userIds the user IDs to fetch the values for |
||
| 362 | * @return array Mapped values: userId => value |
||
| 363 | */ |
||
| 364 | public function getUserValueForUsers($appName, $key, $userIds) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Determines the users that have the given value set for a specific app-key-pair |
||
| 400 | * |
||
| 401 | * @param string $appName the app to get the user for |
||
| 402 | * @param string $key the key to get the user for |
||
| 403 | * @param string $value the value to get the user for |
||
| 404 | * @return array of user IDs |
||
| 405 | */ |
||
| 406 | public function getUsersForUserValue($appName, $key, $value) { |
||
| 429 | } |
||
| 430 |
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.