| Conditions | 8 |
| Paths | 30 |
| Total Lines | 74 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 | $azureGroupMembersInfo = iterator_to_array($this->getAzureGroupMembers($groupUid)); |
||
| 46 | $azureGroupMembersUids = array_column($azureGroupMembersInfo, 'id'); |
||
| 47 | |||
| 48 | foreach ($azureGroupMembersUids as $azureGroupMembersUid) { |
||
| 49 | $userId = $existingUsers[$azureGroupMembersUid] ?? null; |
||
| 50 | |||
| 51 | if (!$userId) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | |||
| 55 | if (isset($roleActions[$userRole])) { |
||
| 56 | /** @var User $user */ |
||
| 57 | $user = $userManager->find($userId); |
||
| 58 | |||
| 59 | $roleActions[$userRole]($user); |
||
| 60 | |||
| 61 | yield sprintf('User (ID %d) status %s', $userId, $userRole); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $em->flush(); |
||
| 66 | } |
||
| 67 | |||
| 68 | if ('true' === $this->plugin->get(AzureActiveDirectory::SETTING_DEACTIVATE_NONEXISTING_USERS)) { |
||
| 69 | yield '----------------'; |
||
| 70 | |||
| 71 | yield 'Trying deactivate non-existing users in Azure'; |
||
| 72 | |||
| 73 | $users = UserManager::getRepository()->findByAuthSource('azure'); |
||
| 74 | $userIdList = array_map( |
||
| 75 | function ($user) { |
||
| 76 | return $user->getId(); |
||
| 77 | }, |
||
| 78 | $users |
||
| 79 | ); |
||
| 80 | |||
| 81 | $nonExistingUsers = array_diff($userIdList, $existingUsers); |
||
| 82 | |||
| 83 | UserManager::deactivate_users($nonExistingUsers); |
||
| 84 | |||
| 85 | yield sprintf( |
||
| 86 | 'Deactivated users IDs: %s', |
||
| 87 | implode(', ', $nonExistingUsers) |
||
| 88 | ); |
||
| 92 |