Passed
Pull Request — 1.11.x (#5763)
by Angel Fernando Quiroz
08:54
created

AzureSyncUsersCommand::__invoke()   B

Complexity

Conditions 9
Paths 36

Size

Total Lines 81
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 45
c 3
b 0
f 0
dl 0
loc 81
rs 7.6444
cc 9
nc 36
nop 0

How to fix   Long Method   

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 license terms, see /license.txt */
4
5
use Chamilo\UserBundle\Entity\User;
6
7
class AzureSyncUsersCommand extends AzureCommand
8
{
9
    /**
10
     * @throws Exception
11
     *
12
     * @return Generator<int, string>
13
     */
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
            );
96
        }
97
    }
98
}
99