| Conditions | 9 |
| Paths | 36 |
| Total Lines | 81 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 14 | public function __invoke(): Generator |
||
| 15 | { |
||
| 16 | yield 'Synchronizing users from Azure.'; |
||
| 17 | |||
| 18 | /** @var array<string, int> $existingUsers */ |
||
| 19 | $existingUsers = []; |
||
| 20 | |||
| 21 | foreach ($this->getAzureUsers() as $azureUserInfo) { |
||
| 22 | try { |
||
| 23 | $userId = $this->plugin->registerUser($azureUserInfo, 'id'); |
||
| 24 | } catch (Exception $e) { |
||
| 25 | yield $e->getMessage(); |
||
| 26 | |||
| 27 | continue; |
||
| 28 | } |
||
| 29 | |||
| 30 | $existingUsers[$azureUserInfo['id']] = $userId; |
||
| 31 | |||
| 32 | yield sprintf('User (ID %d) with received info: %s ', $userId, serialize($azureUserInfo)); |
||
| 33 | } |
||
| 34 | |||
| 35 | yield '----------------'; |
||
| 36 | yield 'Updating users status'; |
||
| 37 | |||
| 38 | $roleGroups = $this->plugin->getGroupUidByRole(); |
||
| 39 | $roleActions = $this->plugin->getUpdateActionByRole(); |
||
| 40 | |||
| 41 | $userManager = UserManager::getManager(); |
||
| 42 | $em = Database::getManager(); |
||
| 43 | |||
| 44 | foreach ($roleGroups as $userRole => $groupUid) { |
||
| 45 | try { |
||
| 46 | $azureGroupMembersInfo = iterator_to_array($this->getAzureGroupMembers($groupUid)); |
||
| 47 | } catch (Exception $e) { |
||
| 48 | yield $e->getMessage(); |
||
| 49 | |||
| 50 | continue; |
||
| 51 | } |
||
| 52 | |||
| 53 | $azureGroupMembersUids = array_column($azureGroupMembersInfo, 'id'); |
||
| 54 | |||
| 55 | foreach ($azureGroupMembersUids as $azureGroupMembersUid) { |
||
| 56 | $userId = $existingUsers[$azureGroupMembersUid] ?? null; |
||
| 57 | |||
| 58 | if (!$userId) { |
||
| 59 | continue; |
||
| 60 | } |
||
| 61 | |||
| 62 | if (isset($roleActions[$userRole])) { |
||
| 63 | /** @var User $user */ |
||
| 64 | $user = $userManager->find($userId); |
||
| 65 | |||
| 66 | $roleActions[$userRole]($user); |
||
| 67 | |||
| 68 | yield sprintf('User (ID %d) status %s', $userId, $userRole); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | $em->flush(); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ('true' === $this->plugin->get(AzureActiveDirectory::SETTING_DEACTIVATE_NONEXISTING_USERS)) { |
||
| 76 | yield '----------------'; |
||
| 77 | |||
| 78 | yield 'Trying deactivate non-existing users in Azure'; |
||
| 79 | |||
| 80 | $users = UserManager::getRepository()->findByAuthSource('azure'); |
||
| 81 | $userIdList = array_map( |
||
| 82 | function ($user) { |
||
| 83 | return $user->getId(); |
||
| 84 | }, |
||
| 85 | $users |
||
| 86 | ); |
||
| 87 | |||
| 88 | $nonExistingUsers = array_diff($userIdList, $existingUsers); |
||
| 89 | |||
| 90 | UserManager::deactivate_users($nonExistingUsers); |
||
| 91 | |||
| 92 | yield sprintf( |
||
| 93 | 'Deactivated users IDs: %s', |
||
| 94 | implode(', ', $nonExistingUsers) |
||
| 95 | ); |
||
| 99 |