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 ( ff24ee...902a28 )
by François
02:17
created

VootAcl::applyMapping()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
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();
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
        $aclMapping = $this->configReader->v('VootAcl', 'aclMapping', false, []);
0 ignored issues
show
Unused Code introduced by
$aclMapping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
49
        $vootToken = new VootToken($tokenDir);
50
        $bearerToken = $vootToken->getVootToken($userId);
51
52
        if (false === $bearerToken) {
53
            // no Bearer token registered for this user, so assume user is not
54
            // a member of any groups
55
            return [];
56
        }
57
58
        // fetch the groups and extract the membership data
59
        $memberOf = self::extractMembership(
60
            $this->fetchGroups($apiUrl, $bearerToken)
61
        );
62
63
        return self::applyMapping(
64
            $memberOf,
65
            $this->configReader->v('VootAcl', 'aclMapping', false, [])
66
        );
67
    }
68
69
    private function fetchGroups($apiUrl, $bearerToken)
70
    {
71
        try {
72
            return $this->client->get(
73
                $apiUrl,
74
                [
75
                    'headers' => [
76
                        'Authorization' => sprintf('Bearer %s', $bearerToken),
77
                    ],
78
                ]
79
            )->json();
80
        } catch (TransferException $e) {
81
            return [];
82
        }
83
    }
84
85
    private static function extractMembership(array $responseData)
86
    {
87
        $memberOf = [];
88
        foreach ($responseData as $groupEntry) {
89
            if (!is_array($groupEntry)) {
90
                continue;
91
            }
92
            if (!array_key_exists('id', $groupEntry)) {
93
                continue;
94
            }
95
            if (!is_string($groupEntry['id'])) {
96
                continue;
97
            }
98
            $memberOf[] = $groupEntry['id'];
99
        }
100
101
        return $memberOf;
102
    }
103
104
    private static function applyMapping(array $memberOf, array $groupMapping)
105
    {
106
        $returnGroups = [];
107
        foreach ($memberOf as $groupEntry) {
108
            // check if it is available in the mapping
109
            if (array_key_exists($groupEntry, $groupMapping)) {
110
                $returnGroups = array_merge($returnGroups, $groupMapping[$groupEntry]);
111
            }
112
        }
113
114
        return $returnGroups;
115
    }
116
}
117