Conditions | 41 |
Paths | 19728 |
Total Lines | 171 |
Code Lines | 115 |
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 | public function search($search, $limit, $offset, ISearchResult $searchResult) { |
||
98 | $currentUserId = $this->userSession->getUser()->getUID(); |
||
99 | |||
100 | $result = $userResults = ['wide' => [], 'exact' => []]; |
||
101 | $userType = new SearchResultType('users'); |
||
102 | $emailType = new SearchResultType('emails'); |
||
103 | |||
104 | // Search in contacts |
||
105 | $addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN'], ['limit' => $limit, 'offset' => $offset]); |
||
106 | $lowerSearch = strtolower($search); |
||
107 | foreach ($addressBookContacts as $contact) { |
||
108 | if (isset($contact['EMAIL'])) { |
||
109 | $emailAddresses = $contact['EMAIL']; |
||
110 | if (\is_string($emailAddresses)) { |
||
111 | $emailAddresses = [$emailAddresses]; |
||
112 | } |
||
113 | foreach ($emailAddresses as $type => $emailAddress) { |
||
114 | $displayName = $emailAddress; |
||
115 | $emailAddressType = null; |
||
116 | if (\is_array($emailAddress)) { |
||
117 | $emailAddressData = $emailAddress; |
||
118 | $emailAddress = $emailAddressData['value']; |
||
119 | $emailAddressType = $emailAddressData['type']; |
||
120 | } |
||
121 | if (isset($contact['FN'])) { |
||
122 | $displayName = $contact['FN'] . ' (' . $emailAddress . ')'; |
||
123 | } |
||
124 | $exactEmailMatch = strtolower($emailAddress) === $lowerSearch; |
||
125 | |||
126 | if (isset($contact['isLocalSystemBook'])) { |
||
127 | if ($this->shareWithGroupOnly) { |
||
128 | /* |
||
129 | * Check if the user may share with the user associated with the e-mail of the just found contact |
||
130 | */ |
||
131 | $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); |
||
132 | $found = false; |
||
133 | foreach ($userGroups as $userGroup) { |
||
134 | if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) { |
||
135 | $found = true; |
||
136 | break; |
||
137 | } |
||
138 | } |
||
139 | if (!$found) { |
||
140 | continue; |
||
141 | } |
||
142 | } |
||
143 | if ($exactEmailMatch && $this->shareeEnumerationFullMatch) { |
||
144 | try { |
||
145 | $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
||
146 | } catch (\InvalidArgumentException $e) { |
||
147 | continue; |
||
148 | } |
||
149 | |||
150 | if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) { |
||
151 | $singleResult = [[ |
||
152 | 'label' => $displayName, |
||
153 | 'uuid' => $contact['UID'], |
||
154 | 'name' => $contact['FN'], |
||
155 | 'value' => [ |
||
156 | 'shareType' => IShare::TYPE_USER, |
||
157 | 'shareWith' => $cloud->getUser(), |
||
158 | ], |
||
159 | 'shareWithDisplayNameUnique' => !empty($emailAddress) ? $emailAddress : $cloud->getUser() |
||
160 | |||
161 | ]]; |
||
162 | $searchResult->addResultSet($userType, [], $singleResult); |
||
163 | $searchResult->markExactIdMatch($emailType); |
||
164 | } |
||
165 | return false; |
||
166 | } |
||
167 | |||
168 | if ($this->shareeEnumeration) { |
||
169 | try { |
||
170 | $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]); |
||
171 | } catch (\InvalidArgumentException $e) { |
||
172 | continue; |
||
173 | } |
||
174 | |||
175 | $addToWide = !($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone); |
||
176 | if (!$addToWide && $this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $contact['UID'])) { |
||
177 | $addToWide = true; |
||
178 | } |
||
179 | |||
180 | if (!$addToWide && $this->shareeEnumerationInGroupOnly) { |
||
181 | $addToWide = false; |
||
182 | $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); |
||
183 | foreach ($userGroups as $userGroup) { |
||
184 | if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) { |
||
185 | $addToWide = true; |
||
186 | break; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | if ($addToWide && !$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) { |
||
191 | $userResults['wide'][] = [ |
||
192 | 'label' => $displayName, |
||
193 | 'uuid' => $contact['UID'], |
||
194 | 'name' => $contact['FN'], |
||
195 | 'value' => [ |
||
196 | 'shareType' => IShare::TYPE_USER, |
||
197 | 'shareWith' => $cloud->getUser(), |
||
198 | ], |
||
199 | 'shareWithDisplayNameUnique' => !empty($emailAddress) ? $emailAddress : $cloud->getUser() |
||
200 | ]; |
||
201 | continue; |
||
202 | } |
||
203 | } |
||
204 | continue; |
||
205 | } |
||
206 | |||
207 | if ($exactEmailMatch |
||
208 | || (isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch)) { |
||
209 | if ($exactEmailMatch) { |
||
210 | $searchResult->markExactIdMatch($emailType); |
||
211 | } |
||
212 | $result['exact'][] = [ |
||
213 | 'label' => $displayName, |
||
214 | 'uuid' => $contact['UID'], |
||
215 | 'name' => $contact['FN'], |
||
216 | 'type' => $emailAddressType ?? '', |
||
217 | 'value' => [ |
||
218 | 'shareType' => IShare::TYPE_EMAIL, |
||
219 | 'shareWith' => $emailAddress, |
||
220 | ], |
||
221 | ]; |
||
222 | } else { |
||
223 | $result['wide'][] = [ |
||
224 | 'label' => $displayName, |
||
225 | 'uuid' => $contact['UID'], |
||
226 | 'name' => $contact['FN'], |
||
227 | 'type' => $emailAddressType ?? '', |
||
228 | 'value' => [ |
||
229 | 'shareType' => IShare::TYPE_EMAIL, |
||
230 | 'shareWith' => $emailAddress, |
||
231 | ], |
||
232 | ]; |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | |||
238 | $reachedEnd = true; |
||
239 | if (!$this->shareeEnumeration) { |
||
240 | $result['wide'] = []; |
||
241 | $userResults['wide'] = []; |
||
242 | } else { |
||
243 | $reachedEnd = (count($result['wide']) < $offset + $limit) && |
||
244 | (count($userResults['wide']) < $offset + $limit); |
||
245 | |||
246 | $result['wide'] = array_slice($result['wide'], $offset, $limit); |
||
247 | $userResults['wide'] = array_slice($userResults['wide'], $offset, $limit); |
||
248 | } |
||
249 | |||
250 | |||
251 | if (!$searchResult->hasExactIdMatch($emailType) && filter_var($search, FILTER_VALIDATE_EMAIL)) { |
||
252 | $result['exact'][] = [ |
||
253 | 'label' => $search, |
||
254 | 'uuid' => $search, |
||
255 | 'value' => [ |
||
256 | 'shareType' => IShare::TYPE_EMAIL, |
||
257 | 'shareWith' => $search, |
||
258 | ], |
||
259 | ]; |
||
260 | } |
||
261 | |||
262 | if (!empty($userResults['wide'])) { |
||
263 | $searchResult->addResultSet($userType, $userResults['wide'], []); |
||
264 | } |
||
265 | $searchResult->addResultSet($emailType, $result['wide'], $result['exact']); |
||
266 | |||
267 | return !$reachedEnd; |
||
268 | } |
||
275 |