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

AzureSyncUsergroupsCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 31
c 2
b 0
f 0
dl 0
loc 59
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 52 9
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
class AzureSyncUsergroupsCommand extends AzureCommand
6
{
7
    /**
8
     * @throws Exception
9
     *
10
     * @return Generator<int, string>
11
     */
12
    public function __invoke(): Generator
13
    {
14
        yield 'Synchronizing groups from Azure.';
15
16
        $usergroup = new UserGroup();
17
18
        $groupIdByUid = [];
19
20
        foreach ($this->getAzureGroups() as $azureGroupInfo) {
21
            if ($usergroup->usergroup_exists($azureGroupInfo['displayName'])) {
22
                $groupId = $usergroup->getIdByName($azureGroupInfo['displayName']);
23
24
                if ($groupId) {
25
                    $usergroup->subscribe_users_to_usergroup($groupId, []);
26
27
                    yield sprintf('Class exists, all users unsubscribed: %s', $azureGroupInfo['displayName']);
28
                }
29
            } else {
30
                $groupId = $usergroup->save([
31
                    'name' => $azureGroupInfo['displayName'],
32
                    'description' => $azureGroupInfo['description'],
33
                ]);
34
35
                if ($groupId) {
36
                    yield sprintf('Class created: %s', $azureGroupInfo['displayName']);
37
                }
38
            }
39
40
            $groupIdByUid[$azureGroupInfo['id']] = $groupId;
41
        }
42
43
        yield '----------------';
44
        yield 'Subscribing users to groups';
45
46
        foreach ($groupIdByUid as $azureGroupUid => $groupId) {
47
            $newGroupMembers = [];
48
49
            yield sprintf('Obtaining members for group (ID %d)', $groupId);
50
51
            foreach ($this->getAzureGroupMembers($azureGroupUid) as $azureGroupMember) {
52
                if ($userId = $this->plugin->getUserIdByVerificationOrder($azureGroupMember, 'id')) {
53
                    $newGroupMembers[] = $userId;
54
                }
55
            }
56
57
            if ($newGroupMembers) {
58
                $usergroup->subscribe_users_to_usergroup($groupId, $newGroupMembers);
59
60
                yield sprintf(
61
                    'User IDs subscribed in class (ID %d): %s',
62
                    $groupId,
63
                    implode(', ', $newGroupMembers)
64
                );
65
            }
66
        }
67
    }
68
}
69