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 ( 35efb6...ee0423 )
by François
02:44
created

UsersModule   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 119
Duplicated Lines 17.65 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
c 2
b 0
f 0
lcom 1
cbo 10
dl 21
loc 119
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B init() 21 96 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Api;
19
20
use fkooman\Http\Request;
21
use fkooman\Rest\Service;
22
use fkooman\Rest\ServiceModuleInterface;
23
use Psr\Log\LoggerInterface;
24
use fkooman\VPN\Server\InputValidation;
25
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo;
26
use fkooman\VPN\Server\AclInterface;
27
use fkooman\VPN\Server\Disable;
28
use fkooman\VPN\Server\OtpSecret;
29
use fkooman\VPN\Server\ApiResponse;
30
31
class UsersModule implements ServiceModuleInterface
32
{
33
    /** @var \fkooman\VPN\Server\Disable */
34
    private $users;
35
36
    /** @var \fkooman\VPN\Server\OtpSecret */
37
    private $otpSecret;
38
39
    /** @var \fkooman\VPN\Server\AclInterface */
40
    private $acl;
41
42
    /** @var \Psr\Log\LoggerInterface */
43
    private $logger;
44
45
    public function __construct(Disable $users, OtpSecret $otpSecret, AclInterface $acl, LoggerInterface $logger)
46
    {
47
        $this->users = $users;
48
        $this->otpSecret = $otpSecret;
49
        $this->acl = $acl;
50
        $this->logger = $logger;
51
    }
52
53
    public function init(Service $service)
54
    {
55
        $service->get(
56
            '/users/disabled',
57 View Code Duplication
            function (Request $request, TokenInfo $tokenInfo) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
                $tokenInfo->getScope()->requireScope(['admin']);
59
60
                return new ApiResponse('users', $this->users->getDisabled());
61
            }
62
        );
63
64
        $service->get(
65
            '/users/disabled/:userId',
66
            function ($userId, Request $request, TokenInfo $tokenInfo) {
67
                $tokenInfo->getScope()->requireScope(['admin', 'portal']);
68
                InputValidation::userId($userId);
69
70
                return new ApiResponse('disabled', $this->users->getDisable($userId));
71
            }
72
        );
73
74
        $service->post(
75
            '/users/disabled/:userId',
76
            function ($userId, Request $request, TokenInfo $tokenInfo) {
77
                $tokenInfo->getScope()->requireScope(['admin']);
78
                InputValidation::userId($userId);
79
                $this->logger->info(sprintf('disabling user "%s"', $userId));
80
81
                return new ApiResponse('ok', $this->users->setDisable($userId, true));
82
            }
83
        );
84
85
        $service->delete(
86
            '/users/disabled/:userId',
87
            function ($userId, Request $request, TokenInfo $tokenInfo) {
88
                $tokenInfo->getScope()->requireScope(['admin']);
89
                InputValidation::userId($userId);
90
                $this->logger->info(sprintf('enabling user "%s"', $userId));
91
92
                return new ApiResponse('ok', $this->users->setDisable($userId, false));
93
            }
94
        );
95
96
        $service->get(
97
            '/users/otp_secrets',
98
            function (Request $request, TokenInfo $tokenInfo) {
99
                $tokenInfo->getScope()->requireScope(['admin']);
100
101
                return new ApiResponse('users', $this->otpSecret->getOtpSecrets());
102
            }
103
        );
104
105
        $service->get(
106
            '/users/otp_secrets/:userId',
107 View Code Duplication
            function ($userId, Request $request, TokenInfo $tokenInfo) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
                $tokenInfo->getScope()->requireScope(['admin', 'portal']);
109
                InputValidation::userId($userId);
110
111
                $hasOtpSecret = false !== $this->otpSecret->getOtpSecret($userId);
112
113
                return new ApiResponse('otp_secret', $hasOtpSecret);
114
            }
115
        );
116
117
        $service->post(
118
            '/users/otp_secrets/:userId',
119 View Code Duplication
            function ($userId, Request $request, TokenInfo $tokenInfo) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
                $tokenInfo->getScope()->requireScope(['portal']);
121
                InputValidation::userId($userId);
122
                $otpSecret = $request->getPostParameter('otp_secret');
123
                InputValidation::otpSecret($otpSecret);
124
125
                return new ApiResponse('ok', $this->otpSecret->setOtpSecret($userId, $otpSecret));
126
            }
127
        );
128
129
        $service->delete(
130
            '/users/otp_secrets/:userId',
131
            function ($userId, Request $request, TokenInfo $tokenInfo) {
132
                $tokenInfo->getScope()->requireScope(['admin']);
133
                InputValidation::userId($userId);
134
135
                return new ApiResponse('ok', $this->otpSecret->setOtpSecret($userId, false));
136
            }
137
        );
138
139
        $service->get(
140
            '/users/groups/:userId',
141
            function ($userId, Request $request, TokenInfo $tokenInfo) {
142
                $tokenInfo->getScope()->requireScope(['admin', 'portal']);
143
                InputValidation::userId($userId);
144
145
                return new ApiResponse('groups', $this->acl->getGroups($userId));
146
            }
147
        );
148
    }
149
}
150