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

AzureSyncUsergroupsCommand::__invoke()   B

Complexity

Conditions 9
Paths 35

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 52
rs 8.0555
cc 9
nc 35
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
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