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

AzureSyncUsergroupsCommand::__invoke()   B

Complexity

Conditions 10
Paths 60

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 58
rs 7.6666
cc 10
nc 60
nop 0

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 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
            try {
52
                foreach ($this->getAzureGroupMembers($azureGroupUid) as $azureGroupMember) {
53
                    if ($userId = $this->plugin->getUserIdByVerificationOrder($azureGroupMember, 'id')) {
54
                        $newGroupMembers[] = $userId;
55
                    }
56
                }
57
            } catch (Exception $e) {
58
                yield $e->getMessage();
59
60
                continue;
61
            }
62
63
            if ($newGroupMembers) {
64
                $usergroup->subscribe_users_to_usergroup($groupId, $newGroupMembers);
65
66
                yield sprintf(
67
                    'User IDs subscribed in class (ID %d): %s',
68
                    $groupId,
69
                    implode(', ', $newGroupMembers)
70
                );
71
            }
72
        }
73
    }
74
}
75