Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 35 | class User { |
||
| 36 | /** |
||
| 37 | * @var IUserTools |
||
| 38 | */ |
||
| 39 | protected $access; |
||
| 40 | /** |
||
| 41 | * @var Connection |
||
| 42 | */ |
||
| 43 | protected $connection; |
||
| 44 | /** |
||
| 45 | * @var \OCP\IConfig |
||
| 46 | */ |
||
| 47 | protected $config; |
||
| 48 | /** |
||
| 49 | * @var FilesystemHelper |
||
| 50 | */ |
||
| 51 | protected $fs; |
||
| 52 | /** |
||
| 53 | * @var \OCP\Image |
||
| 54 | */ |
||
| 55 | protected $image; |
||
| 56 | /** |
||
| 57 | * @var LogWrapper |
||
| 58 | */ |
||
| 59 | protected $log; |
||
| 60 | /** |
||
| 61 | * @var \OCP\IAvatarManager |
||
| 62 | */ |
||
| 63 | protected $avatarManager; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $dn; |
||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $uid; |
||
| 73 | /** |
||
| 74 | * @var string[] |
||
| 75 | */ |
||
| 76 | protected $refreshedFeatures = array(); |
||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $avatarImage; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * DB config keys for user preferences |
||
| 84 | */ |
||
| 85 | const USER_PREFKEY_FIRSTLOGIN = 'firstLoginAccomplished'; |
||
| 86 | const USER_PREFKEY_LASTREFRESH = 'lastFeatureRefresh'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @brief constructor, make sure the subclasses call this one! |
||
| 90 | * @param string the internal username |
||
| 91 | * @param string the LDAP DN |
||
| 92 | * @param IUserTools $access an instance that implements IUserTools for |
||
| 93 | * LDAP interaction |
||
| 94 | * @param \OCP\IConfig |
||
| 95 | * @param FilesystemHelper |
||
| 96 | * @param \OCP\Image any empty instance |
||
| 97 | * @param LogWrapper |
||
| 98 | * @param \OCP\IAvatarManager |
||
| 99 | */ |
||
| 100 | 43 | public function __construct($username, $dn, IUserTools $access, |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @brief updates properties like email, quota or avatar provided by LDAP |
||
| 117 | * @return null |
||
| 118 | */ |
||
| 119 | 9 | public function update() { |
|
| 140 | |||
| 141 | /** |
||
| 142 | * processes results from LDAP for attributes as returned by getAttributesToRead() |
||
| 143 | * @param array $ldapEntry the user entry as retrieved from LDAP |
||
| 144 | */ |
||
| 145 | 3 | public function processAttributes($ldapEntry) { |
|
| 146 | 3 | $this->markRefreshTime(); |
|
| 147 | //Quota |
||
| 148 | 3 | $attr = strtolower($this->connection->ldapQuotaAttribute); |
|
| 149 | 3 | if(isset($ldapEntry[$attr])) { |
|
| 150 | 1 | $this->updateQuota($ldapEntry[$attr][0]); |
|
| 151 | 1 | } |
|
| 152 | 3 | unset($attr); |
|
| 153 | |||
| 154 | |||
| 155 | 3 | $attr = strtolower($this->connection->ldapEmailAttribute); |
|
| 156 | 3 | if(isset($ldapEntry[$attr])) { |
|
| 157 | 1 | $this->updateEmail($ldapEntry[$attr][0]); |
|
| 158 | 1 | } |
|
| 159 | 3 | unset($attr); |
|
| 160 | |||
| 161 | //displayName |
||
| 162 | 3 | $displayName = $displayName2 = ''; |
|
| 163 | 3 | $attr = strtolower($this->connection->ldapUserDisplayName); |
|
| 164 | 3 | if(isset($ldapEntry[$attr])) { |
|
| 165 | 1 | $displayName = $ldapEntry[$attr][0]; |
|
| 166 | 1 | } |
|
| 167 | 3 | $attr = strtolower($this->connection->ldapUserDisplayName2); |
|
| 168 | 3 | if(isset($ldapEntry[$attr])) { |
|
| 169 | $displayName2 = $ldapEntry[$attr][0]; |
||
| 170 | } |
||
| 171 | 3 | if(!empty($displayName)) { |
|
| 172 | 1 | $this->composeAndStoreDisplayName($displayName); |
|
| 173 | 1 | $this->access->cacheUserDisplayName( |
|
| 174 | 1 | $this->getUsername(), |
|
| 175 | 1 | $displayName, |
|
| 176 | $displayName2 |
||
| 177 | 1 | ); |
|
| 178 | 1 | } |
|
| 179 | 3 | unset($attr); |
|
| 180 | |||
| 181 | // LDAP Username, needed for s2s sharing |
||
| 182 | 3 | if(isset($ldapEntry['uid'])) { |
|
| 183 | 1 | $this->storeLDAPUserName($ldapEntry['uid'][0]); |
|
| 184 | 3 | } else if(isset($ldapEntry['samaccountname'])) { |
|
| 185 | $this->storeLDAPUserName($ldapEntry['samaccountname'][0]); |
||
| 186 | } |
||
| 187 | |||
| 188 | //homePath |
||
| 189 | 3 | if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) { |
|
| 190 | 1 | $attr = strtolower(substr($this->connection->homeFolderNamingRule, strlen('attr:'))); |
|
| 191 | 1 | if(isset($ldapEntry[$attr])) { |
|
| 192 | 1 | $this->access->cacheUserHome( |
|
| 193 | 1 | $this->getUsername(), $this->getHomePath($ldapEntry[$attr][0])); |
|
| 194 | 1 | } |
|
| 195 | 1 | } |
|
| 196 | |||
| 197 | //memberOf groups |
||
| 198 | 3 | $cacheKey = 'getMemberOf'.$this->getUsername(); |
|
| 199 | 3 | $groups = false; |
|
| 200 | 3 | if(isset($ldapEntry['memberof'])) { |
|
| 201 | 1 | $groups = $ldapEntry['memberof']; |
|
| 202 | 1 | } |
|
| 203 | 3 | $this->connection->writeToCache($cacheKey, $groups); |
|
| 204 | |||
| 205 | //Avatar |
||
| 206 | 3 | $attrs = array('jpegphoto', 'thumbnailphoto'); |
|
| 207 | 3 | foreach ($attrs as $attr) { |
|
| 208 | 3 | if(isset($ldapEntry[$attr])) { |
|
| 209 | 1 | $this->avatarImage = $ldapEntry[$attr][0]; |
|
| 210 | 1 | $this->updateAvatar(); |
|
| 211 | 1 | break; |
|
| 212 | } |
||
| 213 | 3 | } |
|
| 214 | 3 | } |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @brief returns the LDAP DN of the user |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | 10 | public function getDN() { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @brief returns the ownCloud internal username of the user |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | 12 | public function getUsername() { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * returns the home directory of the user if specified by LDAP settings |
||
| 234 | * @param string $valueFromLDAP |
||
| 235 | * @return bool|string |
||
| 236 | * @throws \Exception |
||
| 237 | */ |
||
| 238 | 6 | public function getHomePath($valueFromLDAP = null) { |
|
| 283 | |||
| 284 | public function getMemberOfGroups() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @brief reads the image from LDAP that shall be used as Avatar |
||
| 296 | * @return string data (provided by LDAP) | false |
||
| 297 | */ |
||
| 298 | 11 | public function getAvatarImage() { |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @brief marks the user as having logged in at least once |
||
| 318 | * @return null |
||
| 319 | */ |
||
| 320 | 3 | public function markLogin() { |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @brief marks the time when user features like email have been updated |
||
| 327 | * @return null |
||
| 328 | */ |
||
| 329 | 9 | public function markRefreshTime() { |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @brief checks whether user features needs to be updated again by |
||
| 336 | * comparing the difference of time of the last refresh to now with the |
||
| 337 | * desired interval |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | 9 | private function needsRefresh() { |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Stores a key-value pair in relation to this user |
||
| 353 | * |
||
| 354 | * @param string $key |
||
| 355 | * @param string $value |
||
| 356 | */ |
||
| 357 | 5 | private function store($key, $value) { |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Composes the display name and stores it in the database. The final |
||
| 363 | * display name is returned. |
||
| 364 | * |
||
| 365 | * @param string $displayName |
||
| 366 | * @param string $displayName2 |
||
| 367 | * @returns string the effective display name |
||
| 368 | */ |
||
| 369 | 5 | public function composeAndStoreDisplayName($displayName, $displayName2 = '') { |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Stores the LDAP Username in the Database |
||
| 379 | * @param string $userName |
||
| 380 | */ |
||
| 381 | public function storeLDAPUserName($userName) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @brief checks whether an update method specified by feature was run |
||
| 387 | * already. If not, it will marked like this, because it is expected that |
||
| 388 | * the method will be run, when false is returned. |
||
| 389 | * @param string email | quota | avatar (can be extended) |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | 20 | private function wasRefreshed($feature) { |
|
| 399 | |||
| 400 | /** |
||
| 401 | * fetches the email from LDAP and stores it as ownCloud user value |
||
| 402 | * @param string $valueFromLDAP if known, to save an LDAP read request |
||
| 403 | * @return null |
||
| 404 | */ |
||
| 405 | 11 | public function updateEmail($valueFromLDAP = null) { |
|
| 424 | |||
| 425 | /** |
||
| 426 | * fetches the quota from LDAP and stores it as ownCloud user value |
||
| 427 | * @param string $valueFromLDAP the quota attribute's value can be passed, |
||
| 428 | * to save the readAttribute request |
||
| 429 | * @return null |
||
| 430 | */ |
||
| 431 | 14 | public function updateQuota($valueFromLDAP = null) { |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @brief attempts to get an image from LDAP and sets it as ownCloud avatar |
||
| 456 | * @return null |
||
| 457 | */ |
||
| 458 | 10 | public function updateAvatar() { |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @brief sets an image as ownCloud avatar |
||
| 473 | * @return null |
||
| 474 | */ |
||
| 475 | 2 | private function setOwnCloudAvatar() { |
|
| 503 | |||
| 504 | } |
||
| 505 |