| Conditions | 11 |
| Paths | 42 |
| Total Lines | 95 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 21 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 22 | { |
||
| 23 | $io = new SymfonyStyle($input, $output); |
||
| 24 | |||
| 25 | $io->title('Synchronizing users from Azure.'); |
||
| 26 | |||
| 27 | /** @var array<string, int> $azureCreatedUserIdList */ |
||
| 28 | $azureCreatedUserIdList = []; |
||
| 29 | |||
| 30 | try { |
||
| 31 | foreach ($this->getAzureUsers() as $azureUserInfo) { |
||
| 32 | try { |
||
| 33 | $user = $this->azureHelper->registerUser($azureUserInfo); |
||
| 34 | } catch (NonUniqueResultException $e) { |
||
| 35 | $io->warning($e->getMessage()); |
||
| 36 | |||
| 37 | continue; |
||
| 38 | } |
||
| 39 | |||
| 40 | $azureCreatedUserIdList[$azureUserInfo['id']] = $user->getId(); |
||
| 41 | |||
| 42 | $io->text( |
||
| 43 | sprintf('User (ID %d) with received info: %s ', $user->getId(), serialize($azureUserInfo)) |
||
| 44 | ); |
||
| 45 | } |
||
| 46 | } catch (Exception $e) { |
||
| 47 | $io->error($e->getMessage()); |
||
| 48 | |||
| 49 | return Command::INVALID; |
||
| 50 | } |
||
| 51 | |||
| 52 | $io->section('Updating users status'); |
||
| 53 | |||
| 54 | $roleGroups = $this->getGroupUidByRole(); |
||
| 55 | $roleActions = $this->getUpdateActionByRole(); |
||
| 56 | |||
| 57 | foreach ($roleGroups as $userRole => $groupUid) { |
||
| 58 | try { |
||
| 59 | $azureGroupMembersInfo = iterator_to_array($this->getAzureGroupMembers($groupUid)); |
||
| 60 | } catch (Exception $e) { |
||
| 61 | $io->warning($e->getMessage()); |
||
| 62 | |||
| 63 | continue; |
||
| 64 | } |
||
| 65 | |||
| 66 | $azureGroupMembersUids = array_column($azureGroupMembersInfo, 'id'); |
||
| 67 | |||
| 68 | foreach ($azureGroupMembersUids as $azureGroupMembersUid) { |
||
| 69 | $userId = $azureCreatedUserIdList[$azureGroupMembersUid] ?? null; |
||
| 70 | |||
| 71 | if (!$userId) { |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | |||
| 75 | if (isset($roleActions[$userRole])) { |
||
| 76 | $user = $this->userRepository->find($userId); |
||
| 77 | |||
| 78 | $roleActions[$userRole]($user); |
||
| 79 | |||
| 80 | $io->text( |
||
| 81 | sprintf('User (ID %d) status %s', $userId, $userRole) |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->entityManager->flush(); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($this->providerParams['deactivate_nonexisting_users'] |
||
| 90 | && !$this->providerParams['script_users_delta'] |
||
| 91 | ) { |
||
| 92 | $io->section('Trying deactivate non-existing users in Azure'); |
||
| 93 | |||
| 94 | $users = $this->userRepository->findByAuthsource(UserAuthSource::AZURE); |
||
| 95 | |||
| 96 | $chamiloUserIdList = array_map( |
||
| 97 | fn(User $user) => $user->getId(), |
||
| 98 | $users |
||
| 99 | ); |
||
| 100 | |||
| 101 | $nonExistingUsers = array_diff($chamiloUserIdList, $azureCreatedUserIdList); |
||
| 102 | |||
| 103 | $this->userRepository->deactivateUsers($nonExistingUsers); |
||
| 104 | |||
| 105 | $io->text( |
||
| 106 | sprintf( |
||
| 107 | 'Deactivated users IDs: %s', |
||
| 108 | implode(', ', $nonExistingUsers) |
||
| 109 | ) |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | $io->success('You have a new command! Now make it your own! Pass --help to see your options.'); |
||
| 114 | |||
| 115 | return Command::SUCCESS; |
||
| 116 | } |
||
| 118 |