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 ( e69c3b...b35ce9 )
by François
08:06
created

VootProvider::extractMembership()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
rs 5.3846
cc 8
eloc 22
nc 8
nop 1
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
namespace SURFnet\VPN\Server\GroupProvider;
19
20
use GuzzleHttp\Client;
21
use GuzzleHttp\Exception\TransferException;
22
use SURFnet\VPN\Server\GroupProviderInterface;
23
use SURFnet\VPN\Server\InstanceConfig;
24
25
class VootProvider implements GroupProviderInterface
26
{
27
    /** @var string */
28
    private $dataDir;
29
30
    /** @var \SURFnet\VPN\Server\InstanceConfig */
31
    private $instanceConfig;
32
33
    public function __construct($dataDir, InstanceConfig $config)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        $this->dataDir = $dataDir;
36
        $this->instanceConfig = $instanceConfig;
0 ignored issues
show
Bug introduced by
The variable $instanceConfig does not exist. Did you mean $config?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
37
    }
38
39
    public function getGroups($userId)
40
    {
41
        if (false === $bearerToken = @file_get_contents(sprintf('%s/users/voot_tokens/%s', $dataDir, $userId))) {
0 ignored issues
show
Bug introduced by
The variable $dataDir does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
42
            return [];
43
        }
44
45
        // fetch the groups and extract the membership data
46
        return self::extractMembership(
47
            $this->fetchGroups($bearerToken)
48
        );
49
    }
50
51
    private function fetchGroups($bearerToken)
52
    {
53
        $httpClient = new Client();
54
        try {
55
            return $httpClient->get(
56
                $this->instanceConfig->v('GroupProviders', 'VootProvider', 'apiUrl'),
57
                [
58
                    'headers' => [
59
                        'Authorization' => sprintf('Bearer %s', $bearerToken),
60
                    ],
61
                ]
62
            )->json();
63
        } catch (TransferException $e) {
64
            return [];
65
        }
66
    }
67
68
    private static function extractMembership(array $responseData)
69
    {
70
        $memberOf = [];
71
        foreach ($responseData as $groupEntry) {
72
            if (!is_array($groupEntry)) {
73
                continue;
74
            }
75
            if (!array_key_exists('id', $groupEntry)) {
76
                continue;
77
            }
78
            if (!is_string($groupEntry['id'])) {
79
                continue;
80
            }
81
            $displayName = $groupEntry['id'];
82
83
            // override displayName if one is set
84
            if (array_key_exists('displayName', $groupEntry)) {
85
                // check if it is multilanguage
86
                if (is_string($groupEntry['displayName'])) {
87
                    $displayName = $groupEntry['displayName'];
88
                } else {
89
                    // take english if available, otherwise first
90
                    if (array_key_exists('en', $groupEntry['displayName'])) {
91
                        $displayName = $groupEntry['displayName']['en'];
92
                    } else {
93
                        $displayName = array_values($groupEntry['displayName'])[0];
94
                    }
95
                }
96
            }
97
98
            $memberOf[] = [
99
                'id' => $groupEntry['id'],
100
                'displayName' => $displayName,
101
            ];
102
        }
103
104
        return $memberOf;
105
    }
106
}
107