Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class User extends BaseObject |
||
| 28 | { |
||
| 29 | const CACHE_CONFIG = 'config'; |
||
| 30 | const CACHE_DATABASES = 'databases'; |
||
| 31 | const CACHE_USAGE = 'usage'; |
||
| 32 | |||
| 33 | /** @var Domain[] * */ |
||
| 34 | private $domains; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Construct the object. |
||
| 38 | * |
||
| 39 | * @param string $name Username of the account |
||
| 40 | * @param UserContext $context The context managing this object |
||
| 41 | * @param mixed|null $config An optional preloaded configuration |
||
| 42 | */ |
||
| 43 | public function __construct($name, UserContext $context, $config = null) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Clear the object's internal cache. |
||
| 53 | */ |
||
| 54 | public function clearCache() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Creates a new database under this user. |
||
| 62 | * |
||
| 63 | * @param string $name Database name, without <user>_ prefix |
||
| 64 | * @param string $username Username to access the database with, without <user>_ prefix |
||
| 65 | * @param string|null $password Password, or null if database user already exists |
||
| 66 | * @return Database Newly created database |
||
| 67 | */ |
||
| 68 | public function createDatabase($name, $username, $password = null) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Creates a new domain under this user. |
||
| 77 | * |
||
| 78 | * @param string $domainName Domain name to create |
||
| 79 | * @param float|null $bandwidthLimit Bandwidth limit in MB, or NULL to share with account |
||
| 80 | * @param float|null $diskLimit Disk limit in MB, or NULL to share with account |
||
| 81 | * @param bool|null $ssl Whether SSL is to be enabled, or NULL to fallback to account default |
||
| 82 | * @param bool|null $php Whether PHP is to be enabled, or NULL to fallback to account default |
||
| 83 | * @param bool|null $cgi Whether CGI is to be enabled, or NULL to fallback to account default |
||
| 84 | * @return Domain Newly created domain |
||
| 85 | */ |
||
| 86 | public function createDomain($domainName, $bandwidthLimit = null, $diskLimit = null, $ssl = null, $php = null, $cgi = null) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string The username |
||
| 95 | */ |
||
| 96 | public function getUsername() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns the bandwidth limit of the user. |
||
| 103 | * |
||
| 104 | * @return float|null Limit in megabytes, or null for unlimited |
||
| 105 | */ |
||
| 106 | public function getBandwidthLimit() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the current period's bandwidth usage in megabytes. |
||
| 113 | * |
||
| 114 | * @return float |
||
| 115 | */ |
||
| 116 | public function getBandwidthUsage() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns the database quota of the user. |
||
| 123 | * |
||
| 124 | * @return int|null Limit, or null for unlimited |
||
| 125 | */ |
||
| 126 | public function getDatabaseLimit() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the current number databases in use. |
||
| 133 | * |
||
| 134 | * @return int |
||
| 135 | */ |
||
| 136 | public function getDatabaseUsage() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Returns the disk quota of the user. |
||
| 143 | * |
||
| 144 | * @return float|null Limit in megabytes, or null for unlimited |
||
| 145 | */ |
||
| 146 | public function getDiskLimit() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the current disk usage in megabytes. |
||
| 153 | * |
||
| 154 | * @return float |
||
| 155 | */ |
||
| 156 | public function getDiskUsage() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return Domain|null The default domain for the user, if any |
||
| 163 | */ |
||
| 164 | public function getDefaultDomain() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns maximum number of domains allowed to this user, or NULL for unlimited. |
||
| 174 | * |
||
| 175 | * @return int|null |
||
| 176 | */ |
||
| 177 | public function getDomainLimit() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns number of domains owned by this user. |
||
| 184 | * |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | public function getDomainUsage() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns whether the user is currently suspended. |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | public function isSuspended() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return Domain[] |
||
| 204 | */ |
||
| 205 | public function getDatabases() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $domainName |
||
| 222 | * @return null|Domain |
||
| 223 | */ |
||
| 224 | public function getDomain($domainName) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return Domain[] |
||
| 234 | */ |
||
| 235 | public function getDomains() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return string The user type, as one of the ACCOUNT_TYPE_ constants in the DirectAdmin class |
||
| 249 | */ |
||
| 250 | public function getType() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return bool Whether the user can use CGI |
||
| 257 | */ |
||
| 258 | public function hasCGI() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return bool Whether the user can use PHP |
||
| 265 | */ |
||
| 266 | public function hasPHP() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return bool Whether the user can use SSL |
||
| 273 | */ |
||
| 274 | public function hasSSL() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return UserContext |
||
| 281 | */ |
||
| 282 | public function impersonate() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Modifies the configuration of the user. For available keys in the array check the documentation on |
||
| 293 | * CMD_API_MODIFY_USER in the linked document. |
||
| 294 | * |
||
| 295 | * @param array $newConfig Associative array of values to be modified |
||
| 296 | * @url http://www.directadmin.com/api.html#modify |
||
| 297 | */ |
||
| 298 | public function modifyConfig(array $newConfig) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param bool $newValue Whether catch-all email is enabled for this user |
||
| 310 | */ |
||
| 311 | public function setAllowCatchall($newValue) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param float|null $newValue New value, or NULL for unlimited |
||
| 318 | */ |
||
| 319 | public function setBandwidthLimit($newValue) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param float|null $newValue New value, or NULL for unlimited |
||
| 326 | */ |
||
| 327 | public function setDiskLimit($newValue) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param int|null $newValue New value, or NULL for unlimited |
||
| 334 | */ |
||
| 335 | public function setDomainLimit($newValue) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Constructs the correct object from the given user config. |
||
| 342 | * |
||
| 343 | * @param array $config The raw config from DirectAdmin |
||
| 344 | * @param UserContext $context The context within which the config was retrieved |
||
| 345 | * @return Admin|Reseller|User The correct object |
||
| 346 | * @throws DirectAdminException If the user type could not be determined |
||
| 347 | */ |
||
| 348 | public static function fromConfig($config, UserContext $context) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Internal function to safe guard config changes and cache them. |
||
| 365 | * |
||
| 366 | * @param string $item Config item to retrieve |
||
| 367 | * @return mixed The value of the config item, or NULL |
||
| 368 | */ |
||
| 369 | private function getConfig($item) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Internal function to safe guard usage changes and cache them. |
||
| 378 | * |
||
| 379 | * @param string $item Usage item to retrieve |
||
| 380 | * @return mixed The value of the stats item, or NULL |
||
| 381 | */ |
||
| 382 | private function getUsage($item) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return UserContext The local user context |
||
| 391 | */ |
||
| 392 | protected function getSelfManagedContext() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @return User The user acting as himself |
||
| 399 | */ |
||
| 400 | protected function getSelfManagedUser() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return bool Whether the account is managing itself |
||
| 407 | */ |
||
| 408 | protected function isSelfManaged() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Loads the current user configuration from the server. |
||
| 415 | * |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | private function loadConfig() |
||
| 422 | } |
||
| 423 |