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.
Completed
Push — master ( a9b118...da2aa4 )
by François
01:58
created

VootProvider::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Server\Acl\Provider;
20
21
use fkooman\OAuth\Client\BearerClient;
22
use SURFnet\VPN\Server\Acl\ProviderInterface;
23
24
class VootProvider implements ProviderInterface
25
{
26
    /** @var \fkooman\OAuth\Client\BearerClient */
27
    private $bearerClient;
28
29
    /** @var string */
30
    private $vootUri;
31
32
    public function __construct(BearerClient $bearerClient, $vootUri)
33
    {
34
        $this->bearerClient = $bearerClient;
35
        $this->vootUri = $vootUri;
36
    }
37
38
    /**
39
     * Get the groups a user is a member of.
40
     *
41
     * @param string userId the userID of the user to request the groups of
42
     *
43
     * @return array the groups as an array containing the keys "id" and
44
     *               "displayName", empty array if no groups are available for this user
45
     */
46
    public function getGroups($userId)
47
    {
48
        $this->bearerClient->setUserId($userId);
49
50
        // fetch the groups and extract the membership data
51
        return self::extractMembership(
52
            $this->fetchGroups()
53
        );
54
    }
55
56
    private function fetchGroups()
57
    {
58
        if (false === $response = $this->bearerClient->get($this->vootUri)) {
59
            return [];
60
        }
61
62
        if (!$response->isOkay()) {
63
            // we should probably log some stuff here, but for now just assume
64
            // there are no groups for the user...
65
            return [];
66
        }
67
68
        return $response->json();
69
    }
70
71
    private static function extractMembership(array $responseData)
72
    {
73
        $memberOf = [];
74
        foreach ($responseData as $groupEntry) {
75
            if (!is_array($groupEntry)) {
76
                continue;
77
            }
78
            if (!array_key_exists('id', $groupEntry)) {
79
                continue;
80
            }
81
            if (!is_string($groupEntry['id'])) {
82
                continue;
83
            }
84
            $displayName = self::getDisplayName($groupEntry);
85
86
            $memberOf[] = [
87
                'id' => $groupEntry['id'],
88
                'displayName' => $displayName,
89
            ];
90
        }
91
92
        return $memberOf;
93
    }
94
95
    private static function getDisplayName(array $groupEntry)
96
    {
97
        if (!array_key_exists('displayName', $groupEntry)) {
98
            return $groupEntry['id'];
99
        }
100
101
        if (is_string($groupEntry['displayName'])) {
102
            return $groupEntry['displayName'];
103
        }
104
105
        if (is_array($groupEntry['displayName'])) {
106
            if (array_key_exists('en', $groupEntry['displayName'])) {
107
                return $groupEntry['displayName']['en'];
108
            }
109
110
            return array_values($groupEntry['displayName'])[0];
111
        }
112
113
        return $groupEntry['id'];
114
    }
115
}
116