| Total Complexity | 61 |
| Total Lines | 418 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 51 | class Principal implements BackendInterface { |
||
| 52 | |||
| 53 | /** @var IUserManager */ |
||
| 54 | private $userManager; |
||
| 55 | |||
| 56 | /** @var IGroupManager */ |
||
| 57 | private $groupManager; |
||
| 58 | |||
| 59 | /** @var IShareManager */ |
||
| 60 | private $shareManager; |
||
| 61 | |||
| 62 | /** @var IUserSession */ |
||
| 63 | private $userSession; |
||
| 64 | |||
| 65 | /** @var IConfig */ |
||
| 66 | private $config; |
||
| 67 | |||
| 68 | /** @var IAppManager */ |
||
| 69 | private $appManager; |
||
| 70 | |||
| 71 | /** @var string */ |
||
| 72 | private $principalPrefix; |
||
| 73 | |||
| 74 | /** @var bool */ |
||
| 75 | private $hasGroups; |
||
| 76 | |||
| 77 | /** @var bool */ |
||
| 78 | private $hasCircles; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param IUserManager $userManager |
||
| 82 | * @param IGroupManager $groupManager |
||
| 83 | * @param IShareManager $shareManager |
||
| 84 | * @param IUserSession $userSession |
||
| 85 | * @param IConfig $config |
||
| 86 | * @param string $principalPrefix |
||
| 87 | */ |
||
| 88 | public function __construct(IUserManager $userManager, |
||
| 89 | IGroupManager $groupManager, |
||
| 90 | IShareManager $shareManager, |
||
| 91 | IUserSession $userSession, |
||
| 92 | IConfig $config, |
||
| 93 | IAppManager $appManager, |
||
| 94 | $principalPrefix = 'principals/users/') { |
||
| 95 | $this->userManager = $userManager; |
||
| 96 | $this->groupManager = $groupManager; |
||
| 97 | $this->shareManager = $shareManager; |
||
| 98 | $this->userSession = $userSession; |
||
| 99 | $this->config = $config; |
||
| 100 | $this->appManager = $appManager; |
||
| 101 | $this->principalPrefix = trim($principalPrefix, '/'); |
||
| 102 | $this->hasGroups = $this->hasCircles = ($principalPrefix === 'principals/users/'); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns a list of principals based on a prefix. |
||
| 107 | * |
||
| 108 | * This prefix will often contain something like 'principals'. You are only |
||
| 109 | * expected to return principals that are in this base path. |
||
| 110 | * |
||
| 111 | * You are expected to return at least a 'uri' for every user, you can |
||
| 112 | * return any additional properties if you wish so. Common properties are: |
||
| 113 | * {DAV:}displayname |
||
| 114 | * |
||
| 115 | * @param string $prefixPath |
||
| 116 | * @return string[] |
||
| 117 | */ |
||
| 118 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 119 | $principals = []; |
||
| 120 | |||
| 121 | if ($prefixPath === $this->principalPrefix) { |
||
| 122 | foreach($this->userManager->search('') as $user) { |
||
| 123 | $principals[] = $this->userToPrincipal($user); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | return $principals; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns a specific principal, specified by it's path. |
||
| 132 | * The returned structure should be the exact same as from |
||
| 133 | * getPrincipalsByPrefix. |
||
| 134 | * |
||
| 135 | * @param string $path |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function getPrincipalByPath($path) { |
||
| 139 | list($prefix, $name) = \Sabre\Uri\split($path); |
||
| 140 | |||
| 141 | if ($prefix === $this->principalPrefix) { |
||
| 142 | $user = $this->userManager->get($name); |
||
| 143 | |||
| 144 | if ($user !== null) { |
||
| 145 | return $this->userToPrincipal($user); |
||
| 146 | } |
||
| 147 | } else if ($prefix === 'principals/circles') { |
||
| 148 | return $this->circleToPrincipal($name); |
||
| 149 | } |
||
| 150 | return null; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the list of members for a group-principal |
||
| 155 | * |
||
| 156 | * @param string $principal |
||
| 157 | * @return string[] |
||
| 158 | * @throws Exception |
||
| 159 | */ |
||
| 160 | public function getGroupMemberSet($principal) { |
||
| 161 | // TODO: for now the group principal has only one member, the user itself |
||
| 162 | $principal = $this->getPrincipalByPath($principal); |
||
| 163 | if (!$principal) { |
||
| 164 | throw new Exception('Principal not found'); |
||
| 165 | } |
||
| 166 | |||
| 167 | return [$principal['uri']]; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the list of groups a principal is a member of |
||
| 172 | * |
||
| 173 | * @param string $principal |
||
| 174 | * @param bool $needGroups |
||
| 175 | * @return array |
||
| 176 | * @throws Exception |
||
| 177 | */ |
||
| 178 | public function getGroupMembership($principal, $needGroups = false) { |
||
| 179 | list($prefix, $name) = \Sabre\Uri\split($principal); |
||
| 180 | |||
| 181 | if ($prefix === $this->principalPrefix) { |
||
| 182 | $user = $this->userManager->get($name); |
||
| 183 | if (!$user) { |
||
| 184 | throw new Exception('Principal not found'); |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($this->hasGroups || $needGroups) { |
||
| 188 | $groups = $this->groupManager->getUserGroups($user); |
||
| 189 | $groups = array_map(function($group) { |
||
| 190 | /** @var IGroup $group */ |
||
| 191 | return 'principals/groups/' . urlencode($group->getGID()); |
||
| 192 | }, $groups); |
||
| 193 | |||
| 194 | return $groups; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | return []; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Updates the list of group members for a group principal. |
||
| 202 | * |
||
| 203 | * The principals should be passed as a list of uri's. |
||
| 204 | * |
||
| 205 | * @param string $principal |
||
| 206 | * @param string[] $members |
||
| 207 | * @throws Exception |
||
| 208 | */ |
||
| 209 | public function setGroupMemberSet($principal, array $members) { |
||
| 210 | throw new Exception('Setting members of the group is not supported yet'); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $path |
||
| 215 | * @param PropPatch $propPatch |
||
| 216 | * @return int |
||
| 217 | */ |
||
| 218 | function updatePrincipal($path, PropPatch $propPatch) { |
||
| 219 | return 0; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Search user principals |
||
| 224 | * |
||
| 225 | * @param array $searchProperties |
||
| 226 | * @param string $test |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
||
| 230 | $results = []; |
||
| 231 | |||
| 232 | // If sharing is disabled, return the empty array |
||
| 233 | $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
||
| 234 | if (!$shareAPIEnabled) { |
||
| 235 | return []; |
||
| 236 | } |
||
| 237 | |||
| 238 | // If sharing is restricted to group members only, |
||
| 239 | // return only members that have groups in common |
||
| 240 | $restrictGroups = false; |
||
| 241 | if ($this->shareManager->shareWithGroupMembersOnly()) { |
||
| 242 | $user = $this->userSession->getUser(); |
||
| 243 | if (!$user) { |
||
| 244 | return []; |
||
| 245 | } |
||
| 246 | |||
| 247 | $restrictGroups = $this->groupManager->getUserGroupIds($user); |
||
| 248 | } |
||
| 249 | |||
| 250 | foreach ($searchProperties as $prop => $value) { |
||
| 251 | switch ($prop) { |
||
| 252 | case '{http://sabredav.org/ns}email-address': |
||
| 253 | $users = $this->userManager->getByEmail($value); |
||
| 254 | |||
| 255 | $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
||
| 256 | // is sharing restricted to groups only? |
||
| 257 | if ($restrictGroups !== false) { |
||
| 258 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
| 259 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
| 260 | return $carry; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
||
| 265 | return $carry; |
||
| 266 | }, []); |
||
| 267 | break; |
||
| 268 | |||
| 269 | case '{DAV:}displayname': |
||
| 270 | $users = $this->userManager->searchDisplayName($value); |
||
| 271 | |||
| 272 | $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
||
| 273 | // is sharing restricted to groups only? |
||
| 274 | if ($restrictGroups !== false) { |
||
| 275 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
| 276 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
| 277 | return $carry; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
||
| 282 | return $carry; |
||
| 283 | }, []); |
||
| 284 | break; |
||
| 285 | |||
| 286 | default: |
||
| 287 | $results[] = []; |
||
| 288 | break; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | // results is an array of arrays, so this is not the first search result |
||
| 293 | // but the results of the first searchProperty |
||
| 294 | if (count($results) === 1) { |
||
| 295 | return $results[0]; |
||
| 296 | } |
||
| 297 | |||
| 298 | switch ($test) { |
||
| 299 | case 'anyof': |
||
| 300 | return array_values(array_unique(array_merge(...$results))); |
||
| 301 | |||
| 302 | case 'allof': |
||
| 303 | default: |
||
| 304 | return array_values(array_intersect(...$results)); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $prefixPath |
||
| 310 | * @param array $searchProperties |
||
| 311 | * @param string $test |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $uri |
||
| 330 | * @param string $principalPrefix |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | function findByUri($uri, $principalPrefix) { |
||
| 334 | // If sharing is disabled, return the empty array |
||
| 335 | $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
||
| 336 | if (!$shareAPIEnabled) { |
||
| 337 | return null; |
||
| 338 | } |
||
| 339 | |||
| 340 | // If sharing is restricted to group members only, |
||
| 341 | // return only members that have groups in common |
||
| 342 | $restrictGroups = false; |
||
| 343 | if ($this->shareManager->shareWithGroupMembersOnly()) { |
||
| 344 | $user = $this->userSession->getUser(); |
||
| 345 | if (!$user) { |
||
| 346 | return null; |
||
| 347 | } |
||
| 348 | |||
| 349 | $restrictGroups = $this->groupManager->getUserGroupIds($user); |
||
| 350 | } |
||
| 351 | |||
| 352 | if (strpos($uri, 'mailto:') === 0) { |
||
| 353 | if ($principalPrefix === 'principals/users') { |
||
| 354 | $users = $this->userManager->getByEmail(substr($uri, 7)); |
||
| 355 | if (count($users) !== 1) { |
||
| 356 | return null; |
||
| 357 | } |
||
| 358 | $user = $users[0]; |
||
| 359 | |||
| 360 | if ($restrictGroups !== false) { |
||
| 361 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
| 362 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
| 363 | return null; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | return $this->principalPrefix . '/' . $user->getUID(); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | if (substr($uri, 0, 10) === 'principal:') { |
||
| 371 | $principal = substr($uri, 10); |
||
| 372 | $principal = $this->getPrincipalByPath($principal); |
||
| 373 | if ($principal !== null) { |
||
| 374 | return $principal['uri']; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | return null; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param IUser $user |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | protected function userToPrincipal($user) { |
||
| 386 | $userId = $user->getUID(); |
||
| 387 | $displayName = $user->getDisplayName(); |
||
| 388 | $principal = [ |
||
| 389 | 'uri' => $this->principalPrefix . '/' . $userId, |
||
| 390 | '{DAV:}displayname' => is_null($displayName) ? $userId : $displayName, |
||
| 391 | '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL', |
||
| 392 | ]; |
||
| 393 | |||
| 394 | $email = $user->getEMailAddress(); |
||
| 395 | if (!empty($email)) { |
||
| 396 | $principal['{http://sabredav.org/ns}email-address'] = $email; |
||
| 397 | } |
||
| 398 | |||
| 399 | return $principal; |
||
| 400 | } |
||
| 401 | |||
| 402 | public function getPrincipalPrefix() { |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $circleUniqueId |
||
| 408 | * @return array|null |
||
| 409 | * @suppress PhanUndeclaredClassMethod |
||
| 410 | * @suppress PhanUndeclaredClassCatch |
||
| 411 | */ |
||
| 412 | protected function circleToPrincipal($circleUniqueId) { |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Returns the list of circles a principal is a member of |
||
| 439 | * |
||
| 440 | * @param string $principal |
||
| 441 | * @param bool $needGroups |
||
| 442 | * @return array |
||
| 443 | * @throws Exception |
||
| 444 | * @suppress PhanUndeclaredClassMethod |
||
| 445 | */ |
||
| 446 | public function getCircleMembership($principal):array { |
||
| 469 | } |
||
| 470 | |||
| 471 | } |
||
| 472 |
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