Total Complexity | 65 |
Total Lines | 432 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Principal 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 Principal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class Principal implements BackendInterface { |
||
54 | |||
55 | /** @var IUserManager */ |
||
56 | private $userManager; |
||
57 | |||
58 | /** @var IGroupManager */ |
||
59 | private $groupManager; |
||
60 | |||
61 | /** @var IShareManager */ |
||
62 | private $shareManager; |
||
63 | |||
64 | /** @var IUserSession */ |
||
65 | private $userSession; |
||
66 | |||
67 | /** @var IAppManager */ |
||
68 | private $appManager; |
||
69 | |||
70 | /** @var string */ |
||
71 | private $principalPrefix; |
||
72 | |||
73 | /** @var bool */ |
||
74 | private $hasGroups; |
||
75 | |||
76 | /** @var bool */ |
||
77 | private $hasCircles; |
||
78 | |||
79 | /** @var ProxyMapper */ |
||
80 | private $proxyMapper; |
||
81 | |||
82 | /** |
||
83 | * Principal constructor. |
||
84 | * |
||
85 | * @param IUserManager $userManager |
||
86 | * @param IGroupManager $groupManager |
||
87 | * @param IShareManager $shareManager |
||
88 | * @param IUserSession $userSession |
||
89 | * @param IAppManager $appManager |
||
90 | * @param ProxyMapper $proxyMapper |
||
91 | * @param string $principalPrefix |
||
92 | */ |
||
93 | public function __construct(IUserManager $userManager, |
||
94 | IGroupManager $groupManager, |
||
95 | IShareManager $shareManager, |
||
96 | IUserSession $userSession, |
||
97 | IAppManager $appManager, |
||
98 | ProxyMapper $proxyMapper, |
||
99 | string $principalPrefix = 'principals/users/') { |
||
100 | $this->userManager = $userManager; |
||
101 | $this->groupManager = $groupManager; |
||
102 | $this->shareManager = $shareManager; |
||
103 | $this->userSession = $userSession; |
||
104 | $this->appManager = $appManager; |
||
105 | $this->principalPrefix = trim($principalPrefix, '/'); |
||
106 | $this->hasGroups = $this->hasCircles = ($principalPrefix === 'principals/users/'); |
||
107 | $this->proxyMapper = $proxyMapper; |
||
108 | } |
||
109 | |||
110 | use PrincipalProxyTrait { |
||
111 | getGroupMembership as protected traitGetGroupMembership; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Returns a list of principals based on a prefix. |
||
116 | * |
||
117 | * This prefix will often contain something like 'principals'. You are only |
||
118 | * expected to return principals that are in this base path. |
||
119 | * |
||
120 | * You are expected to return at least a 'uri' for every user, you can |
||
121 | * return any additional properties if you wish so. Common properties are: |
||
122 | * {DAV:}displayname |
||
123 | * |
||
124 | * @param string $prefixPath |
||
125 | * @return string[] |
||
126 | */ |
||
127 | public function getPrincipalsByPrefix($prefixPath) { |
||
128 | $principals = []; |
||
129 | |||
130 | if ($prefixPath === $this->principalPrefix) { |
||
131 | foreach($this->userManager->search('') as $user) { |
||
132 | $principals[] = $this->userToPrincipal($user); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return $principals; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Returns a specific principal, specified by it's path. |
||
141 | * The returned structure should be the exact same as from |
||
142 | * getPrincipalsByPrefix. |
||
143 | * |
||
144 | * @param string $path |
||
145 | * @return array |
||
146 | */ |
||
147 | public function getPrincipalByPath($path) { |
||
148 | list($prefix, $name) = \Sabre\Uri\split($path); |
||
149 | |||
150 | if ($name === 'calendar-proxy-write' || $name === 'calendar-proxy-read') { |
||
151 | list($prefix2, $name2) = \Sabre\Uri\split($prefix); |
||
152 | |||
153 | if ($prefix2 === $this->principalPrefix) { |
||
154 | $user = $this->userManager->get($name2); |
||
155 | |||
156 | if ($user !== null) { |
||
157 | return [ |
||
158 | 'uri' => 'principals/users/' . $user->getUID() . '/' . $name, |
||
159 | ]; |
||
160 | } |
||
161 | return null; |
||
162 | } |
||
163 | } |
||
164 | |||
165 | if ($prefix === $this->principalPrefix) { |
||
166 | $user = $this->userManager->get($name); |
||
167 | |||
168 | if ($user !== null) { |
||
169 | return $this->userToPrincipal($user); |
||
170 | } |
||
171 | } else if ($prefix === 'principals/circles') { |
||
172 | try { |
||
173 | return $this->circleToPrincipal($name); |
||
174 | } catch (QueryException $e) { |
||
175 | return null; |
||
176 | } |
||
177 | } |
||
178 | return null; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Returns the list of groups a principal is a member of |
||
183 | * |
||
184 | * @param string $principal |
||
185 | * @param bool $needGroups |
||
186 | * @return array |
||
187 | * @throws Exception |
||
188 | */ |
||
189 | public function getGroupMembership($principal, $needGroups = false) { |
||
190 | list($prefix, $name) = \Sabre\Uri\split($principal); |
||
191 | |||
192 | if ($prefix !== $this->principalPrefix) { |
||
193 | return []; |
||
194 | } |
||
195 | |||
196 | $user = $this->userManager->get($name); |
||
197 | if (!$user) { |
||
198 | throw new Exception('Principal not found'); |
||
199 | } |
||
200 | |||
201 | $groups = []; |
||
202 | |||
203 | if ($this->hasGroups || $needGroups) { |
||
204 | $userGroups = $this->groupManager->getUserGroups($user); |
||
205 | foreach($userGroups as $userGroup) { |
||
206 | $groups[] = 'principals/groups/' . urlencode($userGroup->getGID()); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | $groups = array_unique(array_merge( |
||
211 | $groups, |
||
212 | $this->traitGetGroupMembership($principal, $needGroups) |
||
213 | )); |
||
214 | |||
215 | return $groups; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param string $path |
||
220 | * @param PropPatch $propPatch |
||
221 | * @return int |
||
222 | */ |
||
223 | function updatePrincipal($path, PropPatch $propPatch) { |
||
224 | return 0; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Search user principals |
||
229 | * |
||
230 | * @param array $searchProperties |
||
231 | * @param string $test |
||
232 | * @return array |
||
233 | */ |
||
234 | protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
||
235 | $results = []; |
||
236 | |||
237 | // If sharing is disabled, return the empty array |
||
238 | $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
||
239 | if (!$shareAPIEnabled) { |
||
240 | return []; |
||
241 | } |
||
242 | |||
243 | // If sharing is restricted to group members only, |
||
244 | // return only members that have groups in common |
||
245 | $restrictGroups = false; |
||
246 | if ($this->shareManager->shareWithGroupMembersOnly()) { |
||
247 | $user = $this->userSession->getUser(); |
||
248 | if (!$user) { |
||
249 | return []; |
||
250 | } |
||
251 | |||
252 | $restrictGroups = $this->groupManager->getUserGroupIds($user); |
||
253 | } |
||
254 | |||
255 | foreach ($searchProperties as $prop => $value) { |
||
256 | switch ($prop) { |
||
257 | case '{http://sabredav.org/ns}email-address': |
||
258 | $users = $this->userManager->getByEmail($value); |
||
259 | |||
260 | $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
||
261 | // is sharing restricted to groups only? |
||
262 | if ($restrictGroups !== false) { |
||
263 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
264 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
265 | return $carry; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
||
270 | return $carry; |
||
271 | }, []); |
||
272 | break; |
||
273 | |||
274 | case '{DAV:}displayname': |
||
275 | $users = $this->userManager->searchDisplayName($value); |
||
276 | |||
277 | $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
||
278 | // is sharing restricted to groups only? |
||
279 | if ($restrictGroups !== false) { |
||
280 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
281 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
282 | return $carry; |
||
283 | } |
||
284 | } |
||
285 | |||
286 | $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
||
287 | return $carry; |
||
288 | }, []); |
||
289 | break; |
||
290 | |||
291 | case '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set': |
||
292 | // If you add support for more search properties that qualify as a user-address, |
||
293 | // please also add them to the array below |
||
294 | $results[] = $this->searchUserPrincipals([ |
||
295 | // In theory this should also search for principal:principals/users/... |
||
296 | // but that's used internally only anyway and i don't know of any client querying that |
||
297 | '{http://sabredav.org/ns}email-address' => $value, |
||
298 | ], 'anyof'); |
||
299 | break; |
||
300 | |||
301 | default: |
||
302 | $results[] = []; |
||
303 | break; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | // results is an array of arrays, so this is not the first search result |
||
308 | // but the results of the first searchProperty |
||
309 | if (count($results) === 1) { |
||
310 | return $results[0]; |
||
311 | } |
||
312 | |||
313 | switch ($test) { |
||
314 | case 'anyof': |
||
315 | return array_values(array_unique(array_merge(...$results))); |
||
316 | |||
317 | case 'allof': |
||
318 | default: |
||
319 | return array_values(array_intersect(...$results)); |
||
320 | } |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @param string $prefixPath |
||
325 | * @param array $searchProperties |
||
326 | * @param string $test |
||
327 | * @return array |
||
328 | */ |
||
329 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
340 | } |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @param string $uri |
||
345 | * @param string $principalPrefix |
||
346 | * @return string |
||
347 | */ |
||
348 | function findByUri($uri, $principalPrefix) { |
||
349 | // If sharing is disabled, return the empty array |
||
350 | $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
||
351 | if (!$shareAPIEnabled) { |
||
352 | return null; |
||
353 | } |
||
354 | |||
355 | // If sharing is restricted to group members only, |
||
356 | // return only members that have groups in common |
||
357 | $restrictGroups = false; |
||
358 | if ($this->shareManager->shareWithGroupMembersOnly()) { |
||
359 | $user = $this->userSession->getUser(); |
||
360 | if (!$user) { |
||
361 | return null; |
||
362 | } |
||
363 | |||
364 | $restrictGroups = $this->groupManager->getUserGroupIds($user); |
||
365 | } |
||
366 | |||
367 | if (strpos($uri, 'mailto:') === 0) { |
||
368 | if ($principalPrefix === 'principals/users') { |
||
369 | $users = $this->userManager->getByEmail(substr($uri, 7)); |
||
370 | if (count($users) !== 1) { |
||
371 | return null; |
||
372 | } |
||
373 | $user = $users[0]; |
||
374 | |||
375 | if ($restrictGroups !== false) { |
||
376 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
377 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
378 | return null; |
||
379 | } |
||
380 | } |
||
381 | |||
382 | return $this->principalPrefix . '/' . $user->getUID(); |
||
383 | } |
||
384 | } |
||
385 | if (substr($uri, 0, 10) === 'principal:') { |
||
386 | $principal = substr($uri, 10); |
||
387 | $principal = $this->getPrincipalByPath($principal); |
||
388 | if ($principal !== null) { |
||
389 | return $principal['uri']; |
||
390 | } |
||
391 | } |
||
392 | |||
393 | return null; |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * @param IUser $user |
||
398 | * @return array |
||
399 | */ |
||
400 | protected function userToPrincipal($user) { |
||
401 | $userId = $user->getUID(); |
||
402 | $displayName = $user->getDisplayName(); |
||
403 | $principal = [ |
||
404 | 'uri' => $this->principalPrefix . '/' . $userId, |
||
405 | '{DAV:}displayname' => is_null($displayName) ? $userId : $displayName, |
||
406 | '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL', |
||
407 | ]; |
||
408 | |||
409 | $email = $user->getEMailAddress(); |
||
410 | if (!empty($email)) { |
||
411 | $principal['{http://sabredav.org/ns}email-address'] = $email; |
||
412 | } |
||
413 | |||
414 | return $principal; |
||
415 | } |
||
416 | |||
417 | public function getPrincipalPrefix() { |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @param string $circleUniqueId |
||
423 | * @return array|null |
||
424 | * @throws \OCP\AppFramework\QueryException |
||
425 | * @suppress PhanUndeclaredClassMethod |
||
426 | * @suppress PhanUndeclaredClassCatch |
||
427 | */ |
||
428 | protected function circleToPrincipal($circleUniqueId) { |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Returns the list of circles a principal is a member of |
||
455 | * |
||
456 | * @param string $principal |
||
457 | * @return array |
||
458 | * @throws Exception |
||
459 | * @throws \OCP\AppFramework\QueryException |
||
460 | * @suppress PhanUndeclaredClassMethod |
||
461 | */ |
||
462 | public function getCircleMembership($principal):array { |
||
485 | } |
||
486 | } |
||
487 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths