| Conditions | 14 |
| Paths | 390 |
| Total Lines | 83 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 97 | protected function getUserData(string $userId, bool $includeScopes = false): array { |
||
| 98 | $currentLoggedInUser = $this->userSession->getUser(); |
||
| 99 | |||
| 100 | $data = []; |
||
| 101 | |||
| 102 | // Check if the target user exists |
||
| 103 | $targetUserObject = $this->userManager->get($userId); |
||
| 104 | if ($targetUserObject === null) { |
||
| 105 | throw new OCSNotFoundException('User does not exist'); |
||
| 106 | } |
||
| 107 | |||
| 108 | // Should be at least Admin Or SubAdmin! |
||
| 109 | if ($this->groupManager->isAdmin($currentLoggedInUser->getUID()) |
||
| 110 | || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
||
|
|
|||
| 111 | $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true'; |
||
| 112 | } else { |
||
| 113 | // Check they are looking up themselves |
||
| 114 | if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
||
| 115 | return $data; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | // Get groups data |
||
| 120 | $userAccount = $this->accountManager->getAccount($targetUserObject); |
||
| 121 | $groups = $this->groupManager->getUserGroups($targetUserObject); |
||
| 122 | $gids = []; |
||
| 123 | foreach ($groups as $group) { |
||
| 124 | $gids[] = $group->getGID(); |
||
| 125 | } |
||
| 126 | |||
| 127 | try { |
||
| 128 | # might be thrown by LDAP due to handling of users disappears |
||
| 129 | # from the external source (reasons unknown to us) |
||
| 130 | # cf. https://github.com/nextcloud/server/issues/12991 |
||
| 131 | $data['storageLocation'] = $targetUserObject->getHome(); |
||
| 132 | } catch (NoUserException $e) { |
||
| 133 | throw new OCSNotFoundException($e->getMessage(), $e); |
||
| 134 | } |
||
| 135 | |||
| 136 | // Find the data |
||
| 137 | $data['id'] = $targetUserObject->getUID(); |
||
| 138 | $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000; |
||
| 139 | $data['backend'] = $targetUserObject->getBackendClassName(); |
||
| 140 | $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID()); |
||
| 141 | $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID()); |
||
| 142 | |||
| 143 | if ($includeScopes) { |
||
| 144 | $data[IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope(); |
||
| 145 | } |
||
| 146 | |||
| 147 | $data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress(); |
||
| 148 | if ($includeScopes) { |
||
| 149 | $data[IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(); |
||
| 150 | } |
||
| 151 | $data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
||
| 152 | if ($includeScopes) { |
||
| 153 | $data[IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope(); |
||
| 154 | } |
||
| 155 | |||
| 156 | foreach ([ |
||
| 157 | IAccountManager::PROPERTY_PHONE, |
||
| 158 | IAccountManager::PROPERTY_ADDRESS, |
||
| 159 | IAccountManager::PROPERTY_WEBSITE, |
||
| 160 | IAccountManager::PROPERTY_TWITTER, |
||
| 161 | ] as $propertyName) { |
||
| 162 | $property = $userAccount->getProperty($propertyName); |
||
| 163 | $data[$propertyName] = $property->getValue(); |
||
| 164 | if ($includeScopes) { |
||
| 165 | $data[$propertyName . self::SCOPE_SUFFIX] = $property->getScope(); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | $data['groups'] = $gids; |
||
| 170 | $data['language'] = $this->l10nFactory->getUserLanguage($targetUserObject); |
||
| 171 | $data['locale'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'locale'); |
||
| 172 | |||
| 173 | $backend = $targetUserObject->getBackend(); |
||
| 174 | $data['backendCapabilities'] = [ |
||
| 175 | 'setDisplayName' => $backend instanceof ISetDisplayNameBackend || $backend->implementsActions(Backend::SET_DISPLAYNAME), |
||
| 176 | 'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD), |
||
| 177 | ]; |
||
| 178 | |||
| 179 | return $data; |
||
| 180 | } |
||
| 241 |