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