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 ( 297cb9...4ca79e )
by François
02:02
created

VootProvider   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A __destruct() 0 4 1
A getGroups() 0 12 2
A fetchGroups() 0 12 2
B extractMembership() 0 23 5
B getDisplayName() 0 20 5
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 RuntimeException;
22
use SURFnet\VPN\Server\Acl\ProviderInterface;
23
use SURFnet\VPN\Server\Storage;
24
25
class VootProvider implements ProviderInterface
26
{
27
    /** @var \SURFnet\VPN\Server\Storage */
28
    private $storage;
29
30
    /** @var VootClientInterface */
31
    private $vootClient;
32
33
    /** @var string */
34
    private $vootUri;
35
36
    /** @var resource */
37
    private $curlChannel;
38
39
    public function __construct(Storage $storage, VootClientInterface $vootClient, $vootUri)
40
    {
41
        $this->storage = $storage;
42
        $this->vootClient = $vootClient;
43
        $this->vootUri = $vootUri;
44
45
        if (false === $this->curlChannel = curl_init()) {
46
            throw new RuntimeException('unable to create cURL channel');
47
        }
48
    }
49
50
    public function __destruct()
51
    {
52
        curl_close($this->curlChannel);
53
    }
54
55
    /**
56
     * Get the groups a user is a member of.
57
     *
58
     * @param string userId the userID of the user to request the groups of
59
     *
60
     * @return array the groups as an array containing the keys "id" and
61
     *               "displayName", empty array if no groups are available for this user
62
     */
63
    public function getGroups($userId)
64
    {
65
        $vootToken = $this->storage->getVootToken($userId);
66
        if (is_null($vootToken)) {
67
            return [];
68
        }
69
70
        // fetch the groups and extract the membership data
71
        return self::extractMembership(
72
            $this->fetchGroups($vootToken)
73
        );
74
    }
75
76
    private function fetchGroups($bearerToken)
77
    {
78
        list($responseCode, $responseData) = $this->vootClient->get($this->vootUri, $bearerToken);
79
80
        if (200 !== $responseCode) {
81
            // we should probably log some stuff here, but for now just assume
82
            // there are no groups for the user...
83
            return [];
84
        }
85
86
        return $responseData;
87
    }
88
89
    private static function extractMembership(array $responseData)
90
    {
91
        $memberOf = [];
92
        foreach ($responseData as $groupEntry) {
93
            if (!is_array($groupEntry)) {
94
                continue;
95
            }
96
            if (!array_key_exists('id', $groupEntry)) {
97
                continue;
98
            }
99
            if (!is_string($groupEntry['id'])) {
100
                continue;
101
            }
102
            $displayName = self::getDisplayName($groupEntry);
103
104
            $memberOf[] = [
105
                'id' => $groupEntry['id'],
106
                'displayName' => $displayName,
107
            ];
108
        }
109
110
        return $memberOf;
111
    }
112
113
    private static function getDisplayName(array $groupEntry)
114
    {
115
        if (!array_key_exists('displayName', $groupEntry)) {
116
            return $groupEntry['id'];
117
        }
118
119
        if (is_string($groupEntry['displayName'])) {
120
            return $groupEntry['displayName'];
121
        }
122
123
        if (is_array($groupEntry['displayName'])) {
124
            if (array_key_exists('en', $groupEntry['displayName'])) {
125
                return $groupEntry['displayName']['en'];
126
            }
127
128
            return array_values($groupEntry['displayName'])[0];
129
        }
130
131
        return $groupEntry['id'];
132
    }
133
}
134