| Conditions | 10 |
| Paths | 67 |
| Total Lines | 93 |
| Code Lines | 52 |
| 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 |
||
| 23 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 24 | { |
||
| 25 | $io = new SymfonyStyle($input, $output); |
||
| 26 | $io->title('Synchronizing groups from Azure.'); |
||
| 27 | |||
| 28 | $accessUrl = $this->accessUrlHelper->getCurrent(); |
||
| 29 | |||
| 30 | /** @var array<string, Usergroup> $groupIdByUid */ |
||
| 31 | $groupIdByUid = []; |
||
| 32 | |||
| 33 | $admin = $this->userRepository->getRootUser(); |
||
| 34 | |||
| 35 | try { |
||
| 36 | foreach ($this->getAzureGroups() as $azureGroupInfo) { |
||
| 37 | $userGroup = $this->usergroupRepository->getOneByTitleInUrl($azureGroupInfo['displayName'], $accessUrl); |
||
| 38 | |||
| 39 | if ($userGroup) { |
||
| 40 | $userGroup->getUsers()->clear(); |
||
| 41 | |||
| 42 | $io->text( |
||
| 43 | sprintf( |
||
| 44 | 'Class exists, all users unsubscribed: %s (ID %d)', |
||
| 45 | $userGroup->getTitle(), |
||
| 46 | $userGroup->getId() |
||
| 47 | ) |
||
| 48 | ); |
||
| 49 | } else { |
||
| 50 | $userGroup = (new Usergroup()) |
||
| 51 | ->setTitle($azureGroupInfo['displayName']) |
||
| 52 | ->setDescription($azureGroupInfo['description']) |
||
| 53 | ->setCreator($admin) |
||
| 54 | ; |
||
| 55 | |||
| 56 | if ('true' === $this->settingsManager->getSetting('profile.allow_teachers_to_classes')) { |
||
| 57 | $userGroup->setAuthorId( |
||
| 58 | $this->userHelper->getCurrent()->getId() |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | |||
| 62 | $userGroup->addAccessUrl($accessUrl); |
||
| 63 | |||
| 64 | $this->usergroupRepository->create($userGroup); |
||
| 65 | |||
| 66 | $io->text(sprintf('Class created: %s (ID %d)', $userGroup->getTitle(), $userGroup->getId())); |
||
| 67 | } |
||
| 68 | |||
| 69 | $groupIdByUid[$azureGroupInfo['id']] = $userGroup; |
||
| 70 | } |
||
| 71 | } catch (Exception $e) { |
||
| 72 | $io->error($e->getMessage()); |
||
| 73 | |||
| 74 | return Command::INVALID; |
||
| 75 | } |
||
| 76 | |||
| 77 | $io->section('Subscribing users to groups'); |
||
| 78 | |||
| 79 | foreach ($groupIdByUid as $azureGroupUid => $group) { |
||
| 80 | $newGroupMembers = []; |
||
| 81 | |||
| 82 | $io->text(sprintf('Obtaining members for group (ID %d)', $group->getId())); |
||
| 83 | |||
| 84 | try { |
||
| 85 | foreach ($this->getAzureGroupMembers($azureGroupUid) as $azureGroupMember) { |
||
| 86 | if ($userId = $this->azureHelper->getUserIdByVerificationOrder($azureGroupMember)) { |
||
| 87 | $newGroupMembers[] = $userId; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } catch (Exception $e) { |
||
| 91 | $io->warning($e->getMessage()); |
||
| 92 | |||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | foreach ($newGroupMembers as $newGroupMemberId) { |
||
| 97 | $user = $this->userRepository->find($newGroupMemberId); |
||
| 98 | |||
| 99 | $group->addUser($user); |
||
| 100 | } |
||
| 101 | |||
| 102 | $io->text( |
||
| 103 | sprintf( |
||
| 104 | 'User IDs subscribed in class (ID %d): %s', |
||
| 105 | $group->getId(), |
||
| 106 | implode(', ', $newGroupMembers) |
||
| 107 | ) |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | $this->entityManager->flush(); |
||
| 112 | |||
| 113 | $io->success('Done.'); |
||
| 114 | |||
| 115 | return Command::SUCCESS; |
||
| 116 | } |
||
| 117 | } |