| Total Complexity | 92 |
| Total Lines | 587 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AccountManager 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.
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 AccountManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 68 | class AccountManager implements IAccountManager { |
||
| 69 | use TAccountsHelper; |
||
| 70 | |||
| 71 | /** @var IDBConnection database connection */ |
||
| 72 | private $connection; |
||
| 73 | |||
| 74 | /** @var IConfig */ |
||
| 75 | private $config; |
||
| 76 | |||
| 77 | /** @var string table name */ |
||
| 78 | private $table = 'accounts'; |
||
| 79 | |||
| 80 | /** @var string table name */ |
||
| 81 | private $dataTable = 'accounts_data'; |
||
| 82 | |||
| 83 | /** @var EventDispatcherInterface */ |
||
| 84 | private $eventDispatcher; |
||
| 85 | |||
| 86 | /** @var IJobList */ |
||
| 87 | private $jobList; |
||
| 88 | |||
| 89 | /** @var LoggerInterface */ |
||
| 90 | private $logger; |
||
| 91 | |||
| 92 | public function __construct(IDBConnection $connection, |
||
| 93 | IConfig $config, |
||
| 94 | EventDispatcherInterface $eventDispatcher, |
||
| 95 | IJobList $jobList, |
||
| 96 | LoggerInterface $logger) { |
||
| 97 | $this->connection = $connection; |
||
| 98 | $this->config = $config; |
||
| 99 | $this->eventDispatcher = $eventDispatcher; |
||
| 100 | $this->jobList = $jobList; |
||
| 101 | $this->logger = $logger; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $input |
||
| 106 | * @return string Provided phone number in E.164 format when it was a valid number |
||
| 107 | * @throws InvalidArgumentException When the phone number was invalid or no default region is set and the number doesn't start with a country code |
||
| 108 | */ |
||
| 109 | protected function parsePhoneNumber(string $input): string { |
||
| 110 | $defaultRegion = $this->config->getSystemValueString('default_phone_region', ''); |
||
| 111 | |||
| 112 | if ($defaultRegion === '') { |
||
| 113 | // When no default region is set, only +49… numbers are valid |
||
| 114 | if (strpos($input, '+') !== 0) { |
||
| 115 | throw new InvalidArgumentException(self::PROPERTY_PHONE); |
||
| 116 | } |
||
| 117 | |||
| 118 | $defaultRegion = 'EN'; |
||
| 119 | } |
||
| 120 | |||
| 121 | $phoneUtil = PhoneNumberUtil::getInstance(); |
||
| 122 | try { |
||
| 123 | $phoneNumber = $phoneUtil->parse($input, $defaultRegion); |
||
| 124 | if ($phoneNumber instanceof PhoneNumber && $phoneUtil->isValidNumber($phoneNumber)) { |
||
| 125 | return $phoneUtil->format($phoneNumber, PhoneNumberFormat::E164); |
||
| 126 | } |
||
| 127 | } catch (NumberParseException $e) { |
||
|
|
|||
| 128 | } |
||
| 129 | |||
| 130 | throw new InvalidArgumentException(self::PROPERTY_PHONE); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * |
||
| 135 | * @param string $input |
||
| 136 | * @return string |
||
| 137 | * @throws InvalidArgumentException When the website did not have http(s) as protocol or the host name was empty |
||
| 138 | */ |
||
| 139 | protected function parseWebsite(string $input): string { |
||
| 140 | $parts = parse_url($input); |
||
| 141 | if (!isset($parts['scheme']) || ($parts['scheme'] !== 'https' && $parts['scheme'] !== 'http')) { |
||
| 142 | throw new InvalidArgumentException(self::PROPERTY_WEBSITE); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (!isset($parts['host']) || $parts['host'] === '') { |
||
| 146 | throw new InvalidArgumentException(self::PROPERTY_WEBSITE); |
||
| 147 | } |
||
| 148 | |||
| 149 | return $input; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param IAccountProperty[] $properties |
||
| 154 | */ |
||
| 155 | protected function testValueLengths(array $properties, bool $throwOnData = false): void { |
||
| 156 | foreach ($properties as $property) { |
||
| 157 | if (strlen($property->getValue()) > 2048) { |
||
| 158 | if ($throwOnData) { |
||
| 159 | throw new InvalidArgumentException(); |
||
| 160 | } else { |
||
| 161 | $property->setValue(''); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | protected function testPropertyScope(IAccountProperty $property, array $allowedScopes, bool $throwOnData): void { |
||
| 168 | if ($throwOnData && !in_array($property->getScope(), $allowedScopes, true)) { |
||
| 169 | throw new InvalidArgumentException('scope'); |
||
| 170 | } |
||
| 171 | |||
| 172 | if ( |
||
| 173 | $property->getScope() === self::SCOPE_PRIVATE |
||
| 174 | && in_array($property->getName(), [self::PROPERTY_DISPLAYNAME, self::PROPERTY_EMAIL]) |
||
| 175 | ) { |
||
| 176 | if ($throwOnData) { |
||
| 177 | // v2-private is not available for these fields |
||
| 178 | throw new InvalidArgumentException('scope'); |
||
| 179 | } else { |
||
| 180 | // default to local |
||
| 181 | $property->setScope(self::SCOPE_LOCAL); |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | // migrate scope values to the new format |
||
| 185 | // invalid scopes are mapped to a default value |
||
| 186 | $property->setScope(AccountProperty::mapScopeToV2($property->getScope())); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | protected function sanitizePhoneNumberValue(IAccountProperty $property, bool $throwOnData = false) { |
||
| 191 | if ($property->getName() !== self::PROPERTY_PHONE) { |
||
| 192 | if ($throwOnData) { |
||
| 193 | throw new InvalidArgumentException(sprintf('sanitizePhoneNumberValue can only sanitize phone numbers, %s given', $property->getName())); |
||
| 194 | } |
||
| 195 | return; |
||
| 196 | } |
||
| 197 | if ($property->getValue() === '') { |
||
| 198 | return; |
||
| 199 | } |
||
| 200 | try { |
||
| 201 | $property->setValue($this->parsePhoneNumber($property->getValue())); |
||
| 202 | } catch (InvalidArgumentException $e) { |
||
| 203 | if ($throwOnData) { |
||
| 204 | throw $e; |
||
| 205 | } |
||
| 206 | $property->setValue(''); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | protected function sanitizeWebsite(IAccountProperty $property, bool $throwOnData = false) { |
||
| 211 | if ($property->getName() !== self::PROPERTY_WEBSITE) { |
||
| 212 | if ($throwOnData) { |
||
| 213 | throw new InvalidArgumentException(sprintf('sanitizeWebsite can only sanitize web domains, %s given', $property->getName())); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | try { |
||
| 217 | $property->setValue($this->parseWebsite($property->getValue())); |
||
| 218 | } catch (InvalidArgumentException $e) { |
||
| 219 | if ($throwOnData) { |
||
| 220 | throw $e; |
||
| 221 | } |
||
| 222 | $property->setValue(''); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | protected function updateUser(IUser $user, array $data, bool $throwOnData = false): array { |
||
| 227 | $oldUserData = $this->getUser($user, false); |
||
| 228 | $updated = true; |
||
| 229 | |||
| 230 | if ($oldUserData !== $data) { |
||
| 231 | $this->updateExistingUser($user, $data); |
||
| 232 | } else { |
||
| 233 | // nothing needs to be done if new and old data set are the same |
||
| 234 | $updated = false; |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($updated) { |
||
| 238 | $this->eventDispatcher->dispatch( |
||
| 239 | 'OC\AccountManager::userUpdated', |
||
| 240 | new GenericEvent($user, $data) |
||
| 241 | ); |
||
| 242 | } |
||
| 243 | |||
| 244 | return $data; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * delete user from accounts table |
||
| 249 | * |
||
| 250 | * @param IUser $user |
||
| 251 | */ |
||
| 252 | public function deleteUser(IUser $user) { |
||
| 253 | $uid = $user->getUID(); |
||
| 254 | $query = $this->connection->getQueryBuilder(); |
||
| 255 | $query->delete($this->table) |
||
| 256 | ->where($query->expr()->eq('uid', $query->createNamedParameter($uid))) |
||
| 257 | ->execute(); |
||
| 258 | |||
| 259 | $this->deleteUserData($user); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * delete user from accounts table |
||
| 264 | * |
||
| 265 | * @param IUser $user |
||
| 266 | */ |
||
| 267 | public function deleteUserData(IUser $user): void { |
||
| 268 | $uid = $user->getUID(); |
||
| 269 | $query = $this->connection->getQueryBuilder(); |
||
| 270 | $query->delete($this->dataTable) |
||
| 271 | ->where($query->expr()->eq('uid', $query->createNamedParameter($uid))) |
||
| 272 | ->execute(); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * get stored data from a given user |
||
| 277 | */ |
||
| 278 | protected function getUser(IUser $user, bool $insertIfNotExists = true): array { |
||
| 279 | $uid = $user->getUID(); |
||
| 280 | $query = $this->connection->getQueryBuilder(); |
||
| 281 | $query->select('data') |
||
| 282 | ->from($this->table) |
||
| 283 | ->where($query->expr()->eq('uid', $query->createParameter('uid'))) |
||
| 284 | ->setParameter('uid', $uid); |
||
| 285 | $result = $query->executeQuery(); |
||
| 286 | $accountData = $result->fetchAll(); |
||
| 287 | $result->closeCursor(); |
||
| 288 | |||
| 289 | if (empty($accountData)) { |
||
| 290 | $userData = $this->buildDefaultUserRecord($user); |
||
| 291 | if ($insertIfNotExists) { |
||
| 292 | $this->insertNewUser($user, $userData); |
||
| 293 | } |
||
| 294 | return $userData; |
||
| 295 | } |
||
| 296 | |||
| 297 | $userDataArray = $this->importFromJson($accountData[0]['data'], $uid); |
||
| 298 | if ($userDataArray === null || $userDataArray === []) { |
||
| 299 | return $this->buildDefaultUserRecord($user); |
||
| 300 | } |
||
| 301 | |||
| 302 | return $this->addMissingDefaultValues($userDataArray); |
||
| 303 | } |
||
| 304 | |||
| 305 | public function searchUsers(string $property, array $values): array { |
||
| 306 | $chunks = array_chunk($values, 500); |
||
| 307 | $query = $this->connection->getQueryBuilder(); |
||
| 308 | $query->select('*') |
||
| 309 | ->from($this->dataTable) |
||
| 310 | ->where($query->expr()->eq('name', $query->createNamedParameter($property))) |
||
| 311 | ->andWhere($query->expr()->in('value', $query->createParameter('values'))); |
||
| 312 | |||
| 313 | $matches = []; |
||
| 314 | foreach ($chunks as $chunk) { |
||
| 315 | $query->setParameter('values', $chunk, IQueryBuilder::PARAM_STR_ARRAY); |
||
| 316 | $result = $query->executeQuery(); |
||
| 317 | |||
| 318 | while ($row = $result->fetch()) { |
||
| 319 | $matches[$row['uid']] = $row['value']; |
||
| 320 | } |
||
| 321 | $result->closeCursor(); |
||
| 322 | } |
||
| 323 | |||
| 324 | $result = array_merge($matches, $this->searchUsersForRelatedCollection($property, $values)); |
||
| 325 | |||
| 326 | return array_flip($result); |
||
| 327 | } |
||
| 328 | |||
| 329 | protected function searchUsersForRelatedCollection(string $property, array $values): array { |
||
| 330 | switch ($property) { |
||
| 331 | case IAccountManager::PROPERTY_EMAIL: |
||
| 332 | return array_flip($this->searchUsers(IAccountManager::COLLECTION_EMAIL, $values)); |
||
| 333 | default: |
||
| 334 | return []; |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * check if we need to ask the server for email verification, if yes we create a cronjob |
||
| 340 | * |
||
| 341 | */ |
||
| 342 | protected function checkEmailVerification(IAccount $updatedAccount, array $oldData): void { |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * make sure that all expected data are set |
||
| 370 | * |
||
| 371 | */ |
||
| 372 | protected function addMissingDefaultValues(array $userData): array { |
||
| 380 | } |
||
| 381 | |||
| 382 | protected function updateVerificationStatus(IAccount $updatedAccount, array $oldData): void { |
||
| 405 | } |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * add new user to accounts table |
||
| 412 | * |
||
| 413 | * @param IUser $user |
||
| 414 | * @param array $data |
||
| 415 | */ |
||
| 416 | protected function insertNewUser(IUser $user, array $data): void { |
||
| 417 | $uid = $user->getUID(); |
||
| 418 | $jsonEncodedData = $this->prepareJson($data); |
||
| 419 | $query = $this->connection->getQueryBuilder(); |
||
| 420 | $query->insert($this->table) |
||
| 421 | ->values( |
||
| 422 | [ |
||
| 423 | 'uid' => $query->createNamedParameter($uid), |
||
| 424 | 'data' => $query->createNamedParameter($jsonEncodedData), |
||
| 425 | ] |
||
| 426 | ) |
||
| 427 | ->executeStatement(); |
||
| 428 | |||
| 429 | $this->deleteUserData($user); |
||
| 430 | $this->writeUserData($user, $data); |
||
| 431 | } |
||
| 432 | |||
| 433 | protected function prepareJson(array $data): string { |
||
| 434 | $preparedData = []; |
||
| 435 | foreach ($data as $dataRow) { |
||
| 436 | $propertyName = $dataRow['name']; |
||
| 437 | unset($dataRow['name']); |
||
| 438 | if (!$this->isCollection($propertyName)) { |
||
| 439 | $preparedData[$propertyName] = $dataRow; |
||
| 440 | continue; |
||
| 441 | } |
||
| 442 | if (!isset($preparedData[$propertyName])) { |
||
| 443 | $preparedData[$propertyName] = []; |
||
| 444 | } |
||
| 445 | $preparedData[$propertyName][] = $dataRow; |
||
| 446 | } |
||
| 447 | return json_encode($preparedData); |
||
| 448 | } |
||
| 449 | |||
| 450 | protected function importFromJson(string $json, string $userId): ?array { |
||
| 451 | $result = []; |
||
| 452 | $jsonArray = json_decode($json, true); |
||
| 453 | $jsonError = json_last_error(); |
||
| 454 | if ($jsonError !== JSON_ERROR_NONE) { |
||
| 455 | $this->logger->critical( |
||
| 456 | 'User data of {uid} contained invalid JSON (error {json_error}), hence falling back to a default user record', |
||
| 457 | [ |
||
| 458 | 'uid' => $userId, |
||
| 459 | 'json_error' => $jsonError |
||
| 460 | ] |
||
| 461 | ); |
||
| 462 | return null; |
||
| 463 | } |
||
| 464 | foreach ($jsonArray as $propertyName => $row) { |
||
| 465 | if (!$this->isCollection($propertyName)) { |
||
| 466 | $result[] = array_merge($row, ['name' => $propertyName]); |
||
| 467 | continue; |
||
| 468 | } |
||
| 469 | foreach ($row as $singleRow) { |
||
| 470 | $result[] = array_merge($singleRow, ['name' => $propertyName]); |
||
| 471 | } |
||
| 472 | } |
||
| 473 | return $result; |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * update existing user in accounts table |
||
| 478 | * |
||
| 479 | * @param IUser $user |
||
| 480 | * @param array $data |
||
| 481 | */ |
||
| 482 | protected function updateExistingUser(IUser $user, array $data): void { |
||
| 483 | $uid = $user->getUID(); |
||
| 484 | $jsonEncodedData = $this->prepareJson($data); |
||
| 485 | $query = $this->connection->getQueryBuilder(); |
||
| 486 | $query->update($this->table) |
||
| 487 | ->set('data', $query->createNamedParameter($jsonEncodedData)) |
||
| 488 | ->where($query->expr()->eq('uid', $query->createNamedParameter($uid))) |
||
| 489 | ->executeStatement(); |
||
| 490 | |||
| 491 | $this->deleteUserData($user); |
||
| 492 | $this->writeUserData($user, $data); |
||
| 493 | } |
||
| 494 | |||
| 495 | protected function writeUserData(IUser $user, array $data): void { |
||
| 506 | } |
||
| 507 | |||
| 508 | protected function writeUserDataProperties(IQueryBuilder $query, array $data): void { |
||
| 509 | foreach ($data as $property) { |
||
| 510 | if ($property['name'] === self::PROPERTY_AVATAR) { |
||
| 511 | continue; |
||
| 512 | } |
||
| 513 | |||
| 514 | |||
| 515 | $query->setParameter('name', $property['name']) |
||
| 516 | ->setParameter('value', $property['value'] ?? ''); |
||
| 517 | $query->executeStatement(); |
||
| 518 | } |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * build default user record in case not data set exists yet |
||
| 523 | * |
||
| 524 | * @param IUser $user |
||
| 525 | * @return array |
||
| 526 | */ |
||
| 527 | protected function buildDefaultUserRecord(IUser $user) { |
||
| 528 | return [ |
||
| 529 | |||
| 530 | [ |
||
| 531 | 'name' => self::PROPERTY_DISPLAYNAME, |
||
| 532 | 'value' => $user->getDisplayName(), |
||
| 533 | 'scope' => self::SCOPE_FEDERATED, |
||
| 534 | 'verified' => self::NOT_VERIFIED, |
||
| 535 | ], |
||
| 536 | |||
| 537 | [ |
||
| 538 | 'name' => self::PROPERTY_ADDRESS, |
||
| 539 | 'value' => '', |
||
| 540 | 'scope' => self::SCOPE_LOCAL, |
||
| 541 | 'verified' => self::NOT_VERIFIED, |
||
| 542 | ], |
||
| 543 | |||
| 544 | [ |
||
| 545 | 'name' => self::PROPERTY_WEBSITE, |
||
| 546 | 'value' => '', |
||
| 547 | 'scope' => self::SCOPE_LOCAL, |
||
| 548 | 'verified' => self::NOT_VERIFIED, |
||
| 549 | ], |
||
| 550 | |||
| 551 | [ |
||
| 552 | 'name' => self::PROPERTY_EMAIL, |
||
| 553 | 'value' => $user->getEMailAddress(), |
||
| 554 | 'scope' => self::SCOPE_FEDERATED, |
||
| 555 | 'verified' => self::NOT_VERIFIED, |
||
| 556 | ], |
||
| 557 | |||
| 558 | [ |
||
| 559 | 'name' => self::PROPERTY_AVATAR, |
||
| 560 | 'scope' => self::SCOPE_FEDERATED |
||
| 561 | ], |
||
| 562 | |||
| 563 | [ |
||
| 564 | 'name' => self::PROPERTY_PHONE, |
||
| 565 | 'value' => '', |
||
| 566 | 'scope' => self::SCOPE_LOCAL, |
||
| 567 | 'verified' => self::NOT_VERIFIED, |
||
| 568 | ], |
||
| 569 | |||
| 570 | [ |
||
| 571 | 'name' => self::PROPERTY_TWITTER, |
||
| 572 | 'value' => '', |
||
| 573 | 'scope' => self::SCOPE_LOCAL, |
||
| 574 | 'verified' => self::NOT_VERIFIED, |
||
| 575 | ], |
||
| 576 | |||
| 577 | ]; |
||
| 578 | } |
||
| 579 | |||
| 580 | private function arrayDataToCollection(IAccount $account, array $data): IAccountPropertyCollection { |
||
| 581 | $collection = $account->getPropertyCollection($data['name']); |
||
| 582 | |||
| 583 | $p = new AccountProperty( |
||
| 584 | $data['name'], |
||
| 585 | $data['value'] ?? '', |
||
| 586 | $data['scope'] ?? self::SCOPE_LOCAL, |
||
| 587 | $data['verified'] ?? self::NOT_VERIFIED, |
||
| 588 | '' |
||
| 589 | ); |
||
| 590 | $collection->addProperty($p); |
||
| 591 | |||
| 592 | return $collection; |
||
| 593 | } |
||
| 594 | |||
| 595 | private function parseAccountData(IUser $user, $data): Account { |
||
| 596 | $account = new Account($user); |
||
| 597 | foreach ($data as $accountData) { |
||
| 598 | if ($this->isCollection($accountData['name'])) { |
||
| 599 | $account->setPropertyCollection($this->arrayDataToCollection($account, $accountData)); |
||
| 600 | } else { |
||
| 601 | $account->setProperty($accountData['name'], $accountData['value'] ?? '', $accountData['scope'] ?? self::SCOPE_LOCAL, $accountData['verified'] ?? self::NOT_VERIFIED); |
||
| 602 | } |
||
| 603 | } |
||
| 604 | return $account; |
||
| 605 | } |
||
| 606 | |||
| 607 | public function getAccount(IUser $user): IAccount { |
||
| 608 | return $this->parseAccountData($user, $this->getUser($user)); |
||
| 609 | } |
||
| 610 | |||
| 611 | public function updateAccount(IAccount $account): void { |
||
| 655 | } |
||
| 656 | } |
||
| 657 |