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 ( 9b308f...ba5807 )
by François
03:10
created

VootAcl   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 14
c 3
b 0
f 1
lcom 1
cbo 4
dl 0
loc 92
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getGroups() 0 19 2
A fetchGroups() 0 15 2
C extractMembership() 0 38 8
1
<?php
2
/**
3
 * Copyright 2016 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace fkooman\VPN\Server\Acl;
19
20
use fkooman\Config\Reader;
21
use GuzzleHttp\Client;
22
use GuzzleHttp\Exception\TransferException;
23
use fkooman\VPN\Server\AclInterface;
24
use fkooman\VPN\Server\VootToken;
25
26
class VootAcl implements AclInterface
27
{
28
    /** @var \fkooman\Config\Reader */
29
    private $configReader;
30
31
    /** @var \GuzzleHttp\Client */
32
    private $client;
33
34
    public function __construct(Reader $configReader, Client $client = null)
35
    {
36
        $this->configReader = $configReader;
37
        if (is_null($client)) {
38
            $client = new Client();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $client. This often makes code more readable.
Loading history...
39
        }
40
        $this->client = $client;
41
    }
42
43
    public function getGroups($userId)
44
    {
45
        $tokenDir = $this->configReader->v('VootAcl', 'tokenDir');
46
        $apiUrl = $this->configReader->v('VootAcl', 'apiUrl');
47
48
        $vootToken = new VootToken($tokenDir);
49
        $bearerToken = $vootToken->getVootToken($userId);
50
51
        if (false === $bearerToken) {
52
            // no Bearer token registered for this user, so assume user is not
53
            // a member of any groups
54
            return [];
55
        }
56
57
        // fetch the groups and extract the membership data
58
        return self::extractMembership(
59
            $this->fetchGroups($apiUrl, $bearerToken)
60
        );
61
    }
62
63
    private function fetchGroups($apiUrl, $bearerToken)
64
    {
65
        try {
66
            return $this->client->get(
67
                $apiUrl,
68
                [
69
                    'headers' => [
70
                        'Authorization' => sprintf('Bearer %s', $bearerToken),
71
                    ],
72
                ]
73
            )->json();
74
        } catch (TransferException $e) {
75
            return [];
76
        }
77
    }
78
79
    private static function extractMembership(array $responseData)
80
    {
81
        $memberOf = [];
82
        foreach ($responseData as $groupEntry) {
83
            if (!is_array($groupEntry)) {
84
                continue;
85
            }
86
            if (!array_key_exists('id', $groupEntry)) {
87
                continue;
88
            }
89
            if (!is_string($groupEntry['id'])) {
90
                continue;
91
            }
92
            $displayName = $groupEntry['id'];
93
94
            // override displayName if one is set
95
            if (array_key_exists('displayName', $groupEntry)) {
96
                // check if it is multilanguage
97
                if (is_string($groupEntry['displayName'])) {
98
                    $displayName = $groupEntry['displayName'];
99
                } else {
100
                    // take english if available, otherwise first
101
                    if (array_key_exists('en', $groupEntry['displayName'])) {
102
                        $displayName = $groupEntry['displayName']['en'];
103
                    } else {
104
                        $displayName = array_values($groupEntry['displayName'])[0];
105
                    }
106
                }
107
            }
108
109
            $memberOf[] = [
110
                'id' => $groupEntry['id'],
111
                'displayName' => $displayName,
112
            ];
113
        }
114
115
        return $memberOf;
116
    }
117
}
118