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 ( 6c4153...d181b6 )
by François
03:10
created

VootProvider::fetchGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
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
19
namespace SURFnet\VPN\Server\Acl\Provider;
20
21
use GuzzleHttp\Client;
22
use GuzzleHttp\Exception\TransferException;
23
use SURFnet\VPN\Server\Acl\ProviderInterface;
24
use SURFnet\VPN\Common\Config;
25
26
class VootProvider implements ProviderInterface
27
{
28
    /** @var \SURFnet\VPN\Common\Config */
29
    private $config;
30
31
    /** @var string */
32
    private $dataDir;
33
34
    public function __construct(Config $config, $dataDir)
35
    {
36
        $this->config = $config;
37
        $this->dataDir = $dataDir;
38
    }
39
40
    /**
41
     * Get the groups a user is a member of.
42
     *
43
     * @param string userId the userID of the user to request the groups of
44
     *
45
     * @return array the groups as an array containing the keys "id" and
46
     *               "displayName", empty array if no groups are available for this user
47
     */
48
    public function getGroups($userId)
49
    {
50
        if (false === $bearerToken = @file_get_contents(sprintf('%s/users/voot_tokens/%s', $this->dataDir, $userId))) {
51
            return [];
52
        }
53
54
        // fetch the groups and extract the membership data
55
        return self::extractMembership(
56
            $this->fetchGroups($bearerToken)
57
        );
58
    }
59
60
    private function fetchGroups($bearerToken)
61
    {
62
        $httpClient = new Client();
63
        try {
64
            return $httpClient->get(
65
                $this->config->v('apiUrl'),
66
                [
67
                    'headers' => [
68
                        'Authorization' => sprintf('Bearer %s', $bearerToken),
69
                    ],
70
                ]
71
            )->json();
72
        } catch (TransferException $e) {
73
            return [];
74
        }
75
    }
76
77
    private static function extractMembership(array $responseData)
78
    {
79
        $memberOf = [];
80
        foreach ($responseData as $groupEntry) {
81
            if (!is_array($groupEntry)) {
82
                continue;
83
            }
84
            if (!array_key_exists('id', $groupEntry)) {
85
                continue;
86
            }
87
            if (!is_string($groupEntry['id'])) {
88
                continue;
89
            }
90
            $displayName = self::getDisplayName($groupEntry);
91
92
            $memberOf[] = [
93
                'id' => $groupEntry['id'],
94
                'displayName' => $displayName,
95
            ];
96
        }
97
98
        return $memberOf;
99
    }
100
101
    private static function getDisplayName(array $groupEntry)
102
    {
103
        if (!array_key_exists('displayName', $groupEntry)) {
104
            return $groupEntry['id'];
105
        }
106
107
        if (is_string($groupEntry['displayName'])) {
108
            return $groupEntry['displayName'];
109
        }
110
111
        if (is_array($groupEntry['displayName'])) {
112
            if (array_key_exists('en', $groupEntry['displayName'])) {
113
                return $groupEntry['displayName']['en'];
114
            }
115
116
            return array_values($groupEntry['displayName'])[0];
117
        }
118
119
        return $groupEntry['id'];
120
    }
121
}
122