| Total Complexity | 55 |
| Total Lines | 392 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 1 |
Complex classes like SystemService 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 SystemService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class SystemService { |
||
| 38 | |||
| 39 | /** @var IGroupManager */ |
||
| 40 | private $groupManager; |
||
| 41 | |||
| 42 | /** @var IUserManager */ |
||
| 43 | private $userManager; |
||
| 44 | |||
| 45 | /** @var VoteMapper */ |
||
| 46 | private $voteMapper; |
||
| 47 | |||
| 48 | /** @var ShareMapper */ |
||
| 49 | private $shareMapper; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * SystemService constructor. |
||
| 53 | * @param IGroupManager $groupManager |
||
| 54 | * @param IUserManager $userManager |
||
| 55 | * @param VoteMapper $voteMapper |
||
| 56 | * @param ShareMapper $shareMapper |
||
| 57 | */ |
||
| 58 | public function __construct( |
||
| 59 | IGroupManager $groupManager, |
||
| 60 | IUserManager $userManager, |
||
| 61 | VoteMapper $voteMapper, |
||
| 62 | ShareMapper $shareMapper |
||
| 63 | ) { |
||
| 64 | $this->groupManager = $groupManager; |
||
| 65 | $this->userManager = $userManager; |
||
| 66 | $this->voteMapper = $voteMapper; |
||
| 67 | $this->shareMapper = $shareMapper; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Validate string as email address |
||
| 72 | * @NoAdminRequired |
||
| 73 | * @param string $emailAddress |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | private function isValidEmail($emailAddress) { |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Get a list of users |
||
| 83 | * @NoAdminRequired |
||
| 84 | * @param string $query |
||
| 85 | * @param array $skip - usernames to skip in return array |
||
| 86 | * @return Array |
||
| 87 | */ |
||
| 88 | public function getSiteUsers($query = '', $skip = []) { |
||
| 89 | $users = []; |
||
| 90 | foreach ($this->userManager->searchDisplayName($query) as $user) { |
||
| 91 | if (!in_array($user->getUID(), $skip) && $user->isEnabled()) { |
||
| 92 | $users[] = [ |
||
| 93 | 'id' => $user->getUID(), |
||
| 94 | 'user' => $user->getUID(), |
||
| 95 | 'displayName' => $user->getDisplayName(), |
||
| 96 | 'organisation' => '', |
||
| 97 | 'emailAddress' => $user->getEMailAddress(), |
||
| 98 | 'desc' => 'User', |
||
| 99 | 'type' => 'user', |
||
| 100 | 'icon' => 'icon-user', |
||
| 101 | 'avatarURL' => '', |
||
| 102 | 'avatar' => '', |
||
| 103 | 'lastLogin' => $user->getLastLogin(), |
||
| 104 | 'cloudId' => $user->getCloudId() |
||
| 105 | ]; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | return $users; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get a list of user groups |
||
| 113 | * @NoAdminRequired |
||
| 114 | * @param string $query |
||
| 115 | * @param array $skip - group names to skip in return array |
||
| 116 | * @return Array |
||
| 117 | */ |
||
| 118 | public function getSiteGroups($query = '', $skip = []) { |
||
| 119 | $groups = []; |
||
| 120 | foreach ($this->groupManager->search($query) as $group) { |
||
| 121 | if (!in_array($group->getGID(), $skip)) { |
||
| 122 | try { |
||
| 123 | // seems to work only from NC19 on |
||
| 124 | $displayName = $group->getDisplayName(); |
||
| 125 | } catch (\Exception $e) { |
||
| 126 | // fallback |
||
| 127 | $displayName = $group->getGID(); |
||
| 128 | } |
||
| 129 | |||
| 130 | $groups[] = [ |
||
| 131 | 'id' => $group->getGID(), |
||
| 132 | 'user' => $group->getGID(), |
||
| 133 | 'organisation' => '', |
||
| 134 | 'displayName' => $displayName, |
||
| 135 | 'emailAddress' => '', |
||
| 136 | 'desc' => 'Group', |
||
| 137 | 'type' => 'group', |
||
| 138 | 'icon' => 'icon-group', |
||
| 139 | 'avatarURL' => '', |
||
| 140 | 'avatar' => '', |
||
| 141 | 'lastLogin' => '', |
||
| 142 | 'cloudId' => '' |
||
| 143 | |||
| 144 | ]; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | return $groups; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get a list of contacts |
||
| 152 | * @NoAdminRequired |
||
| 153 | * @param string $query |
||
| 154 | * @return Array |
||
| 155 | */ |
||
| 156 | public function getContacts($query = '') { |
||
| 157 | $contacts = []; |
||
| 158 | foreach (\OC::$server->getContactsManager()->search($query, ['FN', 'EMAIL', 'ORG', 'CATEGORIES']) as $contact) { |
||
| 159 | if (!array_key_exists('isLocalSystemBook', $contact) && array_key_exists('EMAIL', $contact)) { |
||
| 160 | $emailAdresses = $contact['EMAIL']; |
||
| 161 | |||
| 162 | if (!is_array($emailAdresses)) { |
||
| 163 | $emailAdresses = [$emailAdresses]; |
||
| 164 | } else { |
||
| 165 | // take the first eMail address for now |
||
| 166 | $emailAdresses = [$emailAdresses[0]]; |
||
| 167 | } |
||
| 168 | |||
| 169 | foreach ($emailAdresses as $emailAddress) { |
||
| 170 | $contacts[] = [ |
||
| 171 | 'id' => $contact['UID'], |
||
| 172 | 'user' => $contact['FN'], |
||
| 173 | 'displayName' => $contact['FN'], |
||
| 174 | 'organisation' => isset($contact['ORG']) ? $contact['ORG'] : '', |
||
| 175 | 'emailAddress' => $emailAddress, |
||
| 176 | 'desc' => 'Contact', |
||
| 177 | 'type' => 'contact', |
||
| 178 | 'icon' => 'icon-mail', |
||
| 179 | 'avatarURL' => '', |
||
| 180 | 'avatar' => '', |
||
| 181 | 'lastLogin' => '', |
||
| 182 | 'cloudId' => '', |
||
| 183 | ]; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | return $contacts; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get a list of contacts |
||
| 192 | * @NoAdminRequired |
||
| 193 | * @param string $query |
||
| 194 | * @return Array |
||
| 195 | */ |
||
| 196 | public function getContactsGroupMembers($query = '') { |
||
| 197 | $contacts = []; |
||
| 198 | foreach (\OC::$server->getContactsManager()->search($query, ['CATEGORIES']) as $contact) { |
||
| 199 | if ( |
||
| 200 | !array_key_exists('isLocalSystemBook', $contact) |
||
| 201 | && array_key_exists('EMAIL', $contact) |
||
| 202 | && in_array($query, explode(',', $contact['CATEGORIES'])) |
||
| 203 | ) { |
||
| 204 | $emailAdresses = $contact['EMAIL']; |
||
| 205 | |||
| 206 | if (!is_array($emailAdresses)) { |
||
| 207 | $emailAdresses = [$emailAdresses]; |
||
| 208 | } else { |
||
| 209 | // take the first eMail address for now |
||
| 210 | $emailAdresses = [$emailAdresses[0]]; |
||
| 211 | } |
||
| 212 | |||
| 213 | foreach ($emailAdresses as $emailAddress) { |
||
| 214 | $contacts[] = [ |
||
| 215 | 'id' => $contact['UID'], |
||
| 216 | 'user' => $contact['FN'], |
||
| 217 | 'displayName' => $contact['FN'], |
||
| 218 | 'organisation' => isset($contact['ORG']) ? $contact['ORG'] : '', |
||
| 219 | 'emailAddress' => $emailAddress, |
||
| 220 | 'desc' => 'Contact', |
||
| 221 | 'type' => 'contact', |
||
| 222 | 'icon' => 'icon-mail', |
||
| 223 | 'avatarURL' => '', |
||
| 224 | 'avatar' => '', |
||
| 225 | 'lastLogin' => '', |
||
| 226 | 'cloudId' => '', |
||
| 227 | ]; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | return $contacts; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get a list of contact groups |
||
| 236 | * @NoAdminRequired |
||
| 237 | * @param string $query |
||
| 238 | * @return Array |
||
| 239 | */ |
||
| 240 | public function getContactsGroups($query = '') { |
||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Get a combined list of NC users, groups and contacts |
||
| 271 | * @NoAdminRequired |
||
| 272 | * @param string $query |
||
| 273 | * @param bool $getGroups - search in groups |
||
| 274 | * @param bool $getUsers - search in site users |
||
| 275 | * @param bool $getContacts - search in contacs |
||
| 276 | * @param bool $getContactGroups - search in contacs |
||
| 277 | * @param array $skipGroups - group names to skip in return array |
||
| 278 | * @param array $skipUsers - user names to skip in return array |
||
| 279 | * @return Array |
||
| 280 | */ |
||
| 281 | public function getSiteUsersAndGroups( |
||
| 282 | $query = '', |
||
| 283 | $getGroups = true, |
||
| 284 | $getUsers = true, |
||
| 285 | $getContacts = true, |
||
| 286 | $getContactGroups = true, |
||
|
|
|||
| 287 | $getMail = false, |
||
| 288 | $skipGroups = [], |
||
| 289 | $skipUsers = [] |
||
| 290 | ) { |
||
| 291 | $list = []; |
||
| 292 | |||
| 293 | if ($getMail && $this->isValidEmail($query)) { |
||
| 294 | $list[] = [ |
||
| 295 | 'id' => '', |
||
| 296 | 'user' => '', |
||
| 297 | 'organisation' => '', |
||
| 298 | 'displayName' => '', |
||
| 299 | 'emailAddress' => $query, |
||
| 300 | 'desc' => $query, |
||
| 301 | 'type' => 'email', |
||
| 302 | 'icon' => 'icon-mail', |
||
| 303 | 'avatarURL' => '', |
||
| 304 | 'avatar' => '', |
||
| 305 | 'lastLogin' => '', |
||
| 306 | 'cloudId' => '' |
||
| 307 | |||
| 308 | ]; |
||
| 309 | } |
||
| 310 | if ($getGroups) { |
||
| 311 | $list = array_merge($list, $this->getSiteGroups($query, $skipGroups)); |
||
| 312 | } |
||
| 313 | |||
| 314 | if ($getUsers) { |
||
| 315 | $list = array_merge($list, $this->getSiteUsers($query, $skipUsers)); |
||
| 316 | } |
||
| 317 | |||
| 318 | if (\OC::$server->getContactsManager()->isEnabled()) { |
||
| 319 | if ($getContacts) { |
||
| 320 | $list = array_merge($list, $this->getContacts($query, $skipUsers)); |
||
| 321 | } |
||
| 322 | |||
| 323 | if ($getContacts) { |
||
| 324 | $list = array_merge($list, $this->getContactsGroups($query, $skipGroups)); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | return $list; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Validate it the user name is reservrd |
||
| 333 | * return false, if this username already exists as a user or as |
||
| 334 | * a participant of the poll |
||
| 335 | * @NoAdminRequired |
||
| 336 | * @return Boolean |
||
| 337 | * @throws InvalidEmailAddress |
||
| 338 | */ |
||
| 339 | public function validateEmailAddress($emailAddress) { |
||
| 344 | } |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Validate it the user name is reservrd |
||
| 349 | * return false, if this username already exists as a user or as |
||
| 350 | * a participant of the poll |
||
| 351 | * @NoAdminRequired |
||
| 352 | * @return Boolean |
||
| 353 | * @throws NotAuthorizedException |
||
| 354 | * @throws TooShortException |
||
| 355 | * @throws InvalidUsernameException |
||
| 356 | */ |
||
| 357 | public function validatePublicUsername($pollId, $userName, $token) { |
||
| 429 | } |
||
| 430 | } |
||
| 431 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.