GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

VootProvider   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getGroups() 0 9 1
A fetchGroups() 0 15 3
B extractMembership() 0 23 5
B getDisplayName() 0 20 5
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Server\Acl\Provider;
11
12
use fkooman\OAuth\Client\OAuthClient;
13
use SURFnet\VPN\Server\Acl\ProviderInterface;
14
15
class VootProvider implements ProviderInterface
16
{
17
    /** @var \fkooman\OAuth\Client\OAuthClient */
18
    private $client;
19
20
    /** @var string */
21
    private $vootUri;
22
23
    public function __construct(OAuthClient $client, $vootUri)
24
    {
25
        $this->client = $client;
26
        $this->vootUri = $vootUri;
27
    }
28
29
    /**
30
     * Get the groups a user is a member of.
31
     *
32
     * @param string userId the userID of the user to request the groups of
33
     *
34
     * @return array the groups as an array containing the keys "id" and
35
     *               "displayName", empty array if no groups are available for this user
36
     */
37
    public function getGroups($userId)
38
    {
39
        $this->client->setUserId($userId);
40
41
        // fetch the groups and extract the membership data
42
        return self::extractMembership(
43
            $this->fetchGroups()
44
        );
45
    }
46
47
    private function fetchGroups()
48
    {
49
        if (false === $response = $this->client->get('groups', $this->vootUri)) {
50
            return [];
51
            // XXX need to delete the token?
52
        }
53
54
        if (!$response->isOkay()) {
55
            // we should probably log some stuff here, but for now just assume
56
            // there are no groups for the user...
57
            return [];
58
        }
59
60
        return $response->json();
61
    }
62
63
    private static function extractMembership(array $responseData)
64
    {
65
        $memberOf = [];
66
        foreach ($responseData as $groupEntry) {
67
            if (!is_array($groupEntry)) {
68
                continue;
69
            }
70
            if (!array_key_exists('id', $groupEntry)) {
71
                continue;
72
            }
73
            if (!is_string($groupEntry['id'])) {
74
                continue;
75
            }
76
            $displayName = self::getDisplayName($groupEntry);
77
78
            $memberOf[] = [
79
                'id' => $groupEntry['id'],
80
                'displayName' => $displayName,
81
            ];
82
        }
83
84
        return $memberOf;
85
    }
86
87
    private static function getDisplayName(array $groupEntry)
88
    {
89
        if (!array_key_exists('displayName', $groupEntry)) {
90
            return $groupEntry['id'];
91
        }
92
93
        if (is_string($groupEntry['displayName'])) {
94
            return $groupEntry['displayName'];
95
        }
96
97
        if (is_array($groupEntry['displayName'])) {
98
            if (array_key_exists('en', $groupEntry['displayName'])) {
99
                return $groupEntry['displayName']['en'];
100
            }
101
102
            return array_values($groupEntry['displayName'])[0];
103
        }
104
105
        return $groupEntry['id'];
106
    }
107
}
108