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 ( 6cbd46...4c991e )
by François
04:09
created

ConfigModule::init()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 68
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 68
rs 9.2447
cc 3
eloc 36
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright 2015 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\Config;
19
20
use fkooman\Http\Request;
21
use fkooman\Rest\Service;
22
use fkooman\Rest\ServiceModuleInterface;
23
use fkooman\Http\JsonResponse;
24
use Psr\Log\LoggerInterface;
25
use fkooman\VPN\Server\InputValidation;
26
use fkooman\Json\Json;
27
use fkooman\Http\Exception\BadRequestException;
28
use fkooman\Http\Exception\ForbiddenException;
29
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo;
30
31
class ConfigModule implements ServiceModuleInterface
32
{
33
    /** @var ConfigStorageInterface */
34
    private $configStorage;
35
36
    /** @var array */
37
    private $allowedPools;
38
39
    /** @var \Psr\Log\LoggerInterface */
40
    private $logger;
41
42
    public function __construct(ConfigStorageInterface $configStorage, array $allowedPools, LoggerInterface $logger)
43
    {
44
        $this->configStorage = $configStorage;
45
        $this->allowedPools = $allowedPools;
46
        $this->logger = $logger;
47
    }
48
49
    public function init(Service $service)
50
    {
51
        // get all configurations
52
        $service->get(
53
            '/config/',
54
            function (Request $request, TokenInfo $tokenInfo) {
55
                $userId = $request->getUrl()->getQueryParameter('user_id');
56
                if (!is_null($userId)) {
57
                    self::requireScope($tokenInfo, ['config_get', 'config_get_user']);
58
                    InputValidation::userId($userId);
59
                } else {
60
                    self::requireScope($tokenInfo, ['config_get']);
61
                }
62
63
                $response = new JsonResponse();
64
                $response->setBody(
65
                    [
66
                        'items' => $this->configStorage->getAllConfig($userId),
67
                    ]
68
                );
69
70
                return $response;
71
            }
72
        );
73
74
        // get configuration for a particular common_name
75
        $service->get(
76
            '/config/:commonName',
77
            function (Request $request, TokenInfo $tokenInfo, $commonName) {
78
                self::requireScope($tokenInfo, ['config_get']);
79
80
                InputValidation::commonName($commonName);
81
82
                $response = new JsonResponse();
83
                $response->setBody(
84
                    $this->configStorage->getConfig($commonName)->toArray()
85
                );
86
87
                return $response;
88
            }
89
        );
90
91
        // set configuration for a particular common_name
92
        $service->put(
93
            '/config/:commonName',
94
            function (Request $request, TokenInfo $tokenInfo, $commonName) {
95
                self::requireScope($tokenInfo, ['config_update']);
96
97
                // XXX check content type
98
                // XXX allow for disconnect as well when updating config
99
100
                InputValidation::commonName($commonName);
101
102
                $configData = new ConfigData(Json::decode($request->getBody()));
103
                if (!in_array($configData->getPool(), $this->allowedPools)) {
104
                    throw new BadRequestException('invalid "pool"');
105
                }
106
                $this->configStorage->setConfig($commonName, $configData);
107
108
                $response = new JsonResponse();
109
                $response->setBody(
110
                    ['ok' => true]
111
                );
112
113
                return $response;
114
            }
115
        );
116
    }
117
118
    private static function requireScope(TokenInfo $tokenInfo, array $requiredScope)
119
    {
120
        foreach ($requiredScope as $s) {
121
            if ($tokenInfo->getScope()->hasScope($s)) {
122
                return;
123
            }
124
        }
125
126
        throw new ForbiddenException('insufficient_scope', sprintf('"%s" scope required', implode(',', $requiredScope)));
127
    }
128
}
129