Complex classes like SyncService 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 SyncService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class SyncService { |
||
| 47 | |||
| 48 | /** @var IConfig */ |
||
| 49 | private $config; |
||
| 50 | /** @var ILogger */ |
||
| 51 | private $logger; |
||
| 52 | /** @var AccountMapper */ |
||
| 53 | private $mapper; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * SyncService constructor. |
||
| 57 | * |
||
| 58 | * @param IConfig $config |
||
| 59 | * @param ILogger $logger |
||
| 60 | * @param AccountMapper $mapper |
||
| 61 | */ |
||
| 62 | public function __construct(IConfig $config, |
||
| 69 | |||
| 70 | /** |
||
| 71 | * For unit tests |
||
| 72 | * @param AccountMapper $mapper |
||
| 73 | */ |
||
| 74 | public function setAccountMapper(AccountMapper $mapper) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param UserInterface $backend the backend to check |
||
| 80 | * @param \Closure $callback is called for every user to allow progress display |
||
| 81 | * @return array[] the first array contains a uid => account map of users that were removed in the external backend |
||
| 82 | * the second array contains a uid => account map of users that are not enabled in oc, but are available in the external backend |
||
| 83 | */ |
||
| 84 | public function analyzeExistingUsers(UserInterface $backend, \Closure $callback) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Checks a backend to see if a user reappeared relative to the accounts table |
||
| 100 | * @param Account $a |
||
| 101 | * @param UserInterface $backend |
||
| 102 | * @param $backendClass |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | private function checkIfAccountReappeared(Account $a, UserInterface $backend, $backendClass) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param UserInterface $backend to sync |
||
| 125 | * @param \Traversable $userIds of users |
||
| 126 | * @param \Closure $callback is called for every user to progress display |
||
| 127 | */ |
||
| 128 | public function run(UserInterface $backend, \Traversable $userIds, \Closure $callback) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param Account $a |
||
| 150 | */ |
||
| 151 | private function syncState(Account $a) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param Account $a |
||
| 176 | */ |
||
| 177 | private function syncLastLogin(Account $a) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param Account $a |
||
| 192 | * @param UserInterface $backend |
||
| 193 | */ |
||
| 194 | private function syncEmail(Account $a, UserInterface $backend) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param Account $a |
||
| 215 | * @param UserInterface $backend |
||
| 216 | */ |
||
| 217 | private function syncQuota(Account $a, UserInterface $backend) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param Account $a |
||
| 241 | * @param UserInterface $backend |
||
| 242 | */ |
||
| 243 | private function syncHome(Account $a, UserInterface $backend) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param Account $a |
||
| 282 | * @param UserInterface $backend |
||
| 283 | */ |
||
| 284 | private function syncDisplayName(Account $a, UserInterface $backend) { |
||
| 285 | $uid = $a->getUserId(); |
||
| 286 | if ($backend instanceof IProvidesDisplayNameBackend || $backend->implementsActions(\OC\User\Backend::GET_DISPLAYNAME)) { |
||
| 287 | $displayName = $backend->getDisplayName($uid); |
||
| 288 | $a->setDisplayName($displayName); |
||
| 289 | if (\array_key_exists('displayName', $a->getUpdatedFields())) { |
||
| 290 | $this->logger->debug( |
||
| 291 | "Setting displayName for <$uid> to <$displayName>", ['app' => self::class] |
||
| 292 | ); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * TODO store username in account table instead of user preferences |
||
| 299 | * |
||
| 300 | * @param Account $a |
||
| 301 | * @param UserInterface $backend |
||
| 302 | */ |
||
| 303 | private function syncUserName(Account $a, UserInterface $backend) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param Account $a |
||
| 323 | * @param UserInterface $backend |
||
| 324 | */ |
||
| 325 | private function syncSearchTerms(Account $a, UserInterface $backend) { |
||
| 326 | $uid = $a->getUserId(); |
||
| 327 | if ($backend instanceof IProvidesExtendedSearchBackend) { |
||
| 328 | $searchTerms = $backend->getSearchTerms($uid); |
||
| 329 | $a->setSearchTerms($searchTerms); |
||
| 330 | if ($a->haveTermsChanged()) { |
||
| 331 | $logTerms = \implode('|', $searchTerms); |
||
| 332 | $this->logger->debug( |
||
| 333 | "Setting searchTerms for <$uid> to <$logTerms>", ['app' => self::class] |
||
| 334 | ); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param Account $a |
||
| 341 | * @param UserInterface $backend of the user |
||
| 342 | * @return Account |
||
| 343 | */ |
||
| 344 | public function syncAccount(Account $a, UserInterface $backend) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param $uid |
||
| 358 | * @param UserInterface $backend |
||
| 359 | * @return Account |
||
| 360 | * @throws \Exception |
||
| 361 | * @throws \InvalidArgumentException if you try to sync with a backend |
||
| 362 | * that doesnt match an existing account |
||
| 363 | */ |
||
| 364 | public function createOrSyncAccount($uid, UserInterface $backend) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $backend of the user |
||
| 397 | * @param string $uid of the user |
||
| 398 | * @return Account |
||
| 399 | */ |
||
| 400 | public function createNewAccount($backend, $uid) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param string $uid |
||
| 411 | * @param string $app |
||
| 412 | * @param string $key |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | private function readUserConfig($uid, $app, $key) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $uid |
||
| 426 | */ |
||
| 427 | private function cleanPreferences($uid) { |
||
| 433 | } |
||
| 434 |