Passed
Push — master ( 13c68b...d6cf3f )
by Angel Fernando Quiroz
09:49 queued 19s
created

AzureSyncUsergroupsCommand::execute()   B

Complexity

Conditions 10
Paths 67

Size

Total Lines 93
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 52
c 1
b 0
f 0
nc 67
nop 2
dl 0
loc 93
rs 7.1806

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Command;
8
9
use Chamilo\CoreBundle\Entity\Usergroup;
10
use Exception;
11
use Symfony\Component\Console\Attribute\AsCommand;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
17
#[AsCommand(
18
    name: 'app:azure-sync-usergroups',
19
    description: 'Synchronize groups registered in Azure with Chamilo user groups',
20
)]
21
class AzureSyncUsergroupsCommand extends AzureSyncAbstractCommand
22
{
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
}