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

AzureSyncUsergroupsCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

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