| Total Complexity | 44 |
| Total Lines | 235 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ContactsStore 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 ContactsStore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class ContactsStore implements IContactsStore { |
||
| 42 | |||
| 43 | /** @var IManager */ |
||
| 44 | private $contactsManager; |
||
| 45 | |||
| 46 | /** @var IConfig */ |
||
| 47 | private $config; |
||
| 48 | |||
| 49 | /** @var IUserManager */ |
||
| 50 | private $userManager; |
||
| 51 | |||
| 52 | /** @var IGroupManager */ |
||
| 53 | private $groupManager; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param IManager $contactsManager |
||
| 57 | * @param IConfig $config |
||
| 58 | * @param IUserManager $userManager |
||
| 59 | * @param IGroupManager $groupManager |
||
| 60 | */ |
||
| 61 | public function __construct(IManager $contactsManager, |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param IUser $user |
||
| 73 | * @param string|null $filter |
||
| 74 | * @return IEntry[] |
||
| 75 | */ |
||
| 76 | public function getContacts(IUser $user, $filter, ?int $limit = null, ?int $offset = null) { |
||
| 77 | $options = []; |
||
| 78 | if ($limit !== null) { |
||
| 79 | $options['limit'] = $limit; |
||
| 80 | } |
||
| 81 | if ($offset !== null) { |
||
| 82 | $options['offset'] = $offset; |
||
| 83 | } |
||
| 84 | |||
| 85 | $allContacts = $this->contactsManager->search( |
||
| 86 | $filter ?: '', |
||
| 87 | [ |
||
| 88 | 'FN', |
||
| 89 | 'EMAIL' |
||
| 90 | ], |
||
| 91 | $options |
||
| 92 | ); |
||
| 93 | |||
| 94 | $entries = array_map(function (array $contact) { |
||
| 95 | return $this->contactArrayToEntry($contact); |
||
| 96 | }, $allContacts); |
||
| 97 | return $this->filterContacts( |
||
| 98 | $user, |
||
| 99 | $entries, |
||
| 100 | $filter |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Filters the contacts. Applies 3 filters: |
||
| 106 | * 1. filter the current user |
||
| 107 | * 2. if the `shareapi_allow_share_dialog_user_enumeration` config option is |
||
| 108 | * enabled it will filter all local users |
||
| 109 | * 3. if the `shareapi_exclude_groups` config option is enabled and the |
||
| 110 | * current user is in an excluded group it will filter all local users. |
||
| 111 | * 4. if the `shareapi_only_share_with_group_members` config option is |
||
| 112 | * enabled it will filter all users which doens't have a common group |
||
| 113 | * with the current user. |
||
| 114 | * |
||
| 115 | * @param IUser $self |
||
| 116 | * @param Entry[] $entries |
||
| 117 | * @param string $filter |
||
| 118 | * @return Entry[] the filtered contacts |
||
| 119 | */ |
||
| 120 | private function filterContacts(IUser $self, |
||
| 121 | array $entries, |
||
| 122 | $filter) { |
||
| 123 | $disallowEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') !== 'yes'; |
||
| 124 | $restrictEnumeration = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes'; |
||
| 125 | $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes'; |
||
| 126 | |||
| 127 | // whether to filter out local users |
||
| 128 | $skipLocal = false; |
||
| 129 | // whether to filter out all users which doesn't have the same group as the current user |
||
| 130 | $ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes' || $restrictEnumeration; |
||
| 131 | |||
| 132 | $selfGroups = $this->groupManager->getUserGroupIds($self); |
||
| 133 | |||
| 134 | if ($excludedGroups) { |
||
| 135 | $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
||
| 136 | $decodedExcludeGroups = json_decode($excludedGroups, true); |
||
| 137 | $excludeGroupsList = ($decodedExcludeGroups !== null) ? $decodedExcludeGroups : []; |
||
| 138 | |||
| 139 | if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) { |
||
| 140 | // a group of the current user is excluded -> filter all local users |
||
| 141 | $skipLocal = true; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | $selfUID = $self->getUID(); |
||
| 146 | |||
| 147 | return array_values(array_filter($entries, function (IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID, $disallowEnumeration, $filter) { |
||
|
|
|||
| 148 | if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) { |
||
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Prevent enumerating local users |
||
| 153 | if ($disallowEnumeration && $entry->getProperty('isLocalSystemBook')) { |
||
| 154 | $filterUser = true; |
||
| 155 | |||
| 156 | $mailAddresses = $entry->getEMailAddresses(); |
||
| 157 | foreach ($mailAddresses as $mailAddress) { |
||
| 158 | if ($mailAddress === $filter) { |
||
| 159 | $filterUser = false; |
||
| 160 | break; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($entry->getProperty('UID') && $entry->getProperty('UID') === $filter) { |
||
| 165 | $filterUser = false; |
||
| 166 | } |
||
| 167 | |||
| 168 | if ($filterUser) { |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) { |
||
| 174 | $uid = $this->userManager->get($entry->getProperty('UID')); |
||
| 175 | |||
| 176 | if ($uid === null) { |
||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | $contactGroups = $this->groupManager->getUserGroupIds($uid); |
||
| 181 | if (count(array_intersect($contactGroups, $selfGroups)) === 0) { |
||
| 182 | // no groups in common, so shouldn't see the contact |
||
| 183 | return false; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return $entry->getProperty('UID') !== $selfUID; |
||
| 188 | })); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param IUser $user |
||
| 193 | * @param integer $shareType |
||
| 194 | * @param string $shareWith |
||
| 195 | * @return IEntry|null |
||
| 196 | */ |
||
| 197 | public function findOne(IUser $user, $shareType, $shareWith) { |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param array $contact |
||
| 247 | * @return Entry |
||
| 248 | */ |
||
| 249 | private function contactArrayToEntry(array $contact) { |
||
| 276 | } |
||
| 277 | } |
||
| 278 |
This check looks for imports that have been defined, but are not used in the scope.