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 ( bca83a...1ac405 )
by François
03:36
created

ConfigModule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 137
Duplicated Lines 37.23 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 8
dl 51
loc 137
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C init() 51 126 7

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 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
namespace fkooman\VPN\Server\Config;
18
19
use fkooman\Http\Request;
20
use fkooman\Rest\Service;
21
use fkooman\Rest\ServiceModuleInterface;
22
use fkooman\VPN\Server\Utils;
23
use fkooman\Rest\Plugin\Authentication\UserInfoInterface;
24
use fkooman\Http\JsonResponse;
25
use fkooman\Http\Exception\BadRequestException;
26
27
class ConfigModule implements ServiceModuleInterface
28
{
29
    /** @var StaticConfig */
30
    private $staticConfig;
31
32
    public function __construct(StaticConfig $staticConfig)
33
    {
34
        $this->staticConfig = $staticConfig;
35
    }
36
37
    public function init(Service $service)
38
    {
39
        $service->get(
40
            '/static/ip',
41
           function (Request $request, UserInfoInterface $userInfo) {
0 ignored issues
show
Unused Code introduced by
The parameter $userInfo 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...
42
                // we typically deal with CNs, not user IDs, but the part of 
43
                // the CN before the first '_' is considered the user ID
44
                $userId = $request->getUrl()->getQueryParameter('user_id');
45
                $commonName = $request->getUrl()->getQueryParameter('common_name');
46
                if (!is_null($userId)) {
47
                    // per user
48
                    Utils::validateUserId($userId);
49
                    $ipConfig = $this->staticConfig->getStaticAddresses($userId);
50
                } elseif (!is_null($commonName)) {
51
                    // per CN
52
                    Utils::validateCommonName($commonName);
53
                    $ipConfig = $this->staticConfig->getStaticAddress($commonName);
54
                } else {
55
                    // all
56
                    $ipConfig = $this->staticConfig->getStaticAddresses();
57
                }
58
59
                $response = new JsonResponse();
60
                $response->setBody(
61
                    array(
62
                        'ok' => true,
63
                        'ip' => $ipConfig,
64
                        'ipRange' => $this->staticConfig->getIpRange()->getRange(),
65
                        'poolRange' => $this->staticConfig->getPoolRange()->getRange(),
66
                    )
67
                );
68
69
                return $response;
70
            }
71
        );
72
73
        $service->post(
74
            '/static/ip',
75
           function (Request $request, UserInfoInterface $userInfo) {
0 ignored issues
show
Unused Code introduced by
The parameter $userInfo 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...
76
                $commonName = $request->getPostParameter('common_name');
77
                if (is_null($commonName)) {
78
                    throw new BadRequestException('missing common_name');
79
                }
80
                Utils::validateCommonName($commonName);
81
82
                $v4 = $request->getPostParameter('v4');
83
                if (empty($v4)) {
84
                    $v4 = null;
85
                }
86
87
                $response = new JsonResponse();
88
                $response->setBody(
89
                    array(
90
                        'ok' => $this->staticConfig->setStaticAddresses($commonName, $v4),
91
                    )
92
                );
93
94
                // $this->logInfo('setting static IP', array('api_user' => $userInfo->getUserId(), 'cn' => $commonName, 'v4' => $v4));
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
96
                return $response;
97
            }
98
        );
99
100
        $service->post(
101
            '/ccd/disable',
102 View Code Duplication
            function (Request $request, UserInfoInterface $userInfo) {
0 ignored issues
show
Unused Code introduced by
The parameter $userInfo 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...
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...
103
                $commonName = $request->getPostParameter('common_name');
104
                if (is_null($commonName)) {
105
                    throw new BadRequestException('missing common_name');
106
                }
107
                Utils::validateCommonName($commonName);
108
109
                // $this->logInfo('disabling cn', array('api_user' => $userInfo->getUserId(), 'cn' => $commonName));
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
111
                $response = new JsonResponse();
112
                $response->setBody(
113
                    array(
114
                        'ok' => $this->staticConfig->disableCommonName($commonName),
115
                    )
116
                );
117
118
                return $response;
119
            }
120
        );
121
122
        $service->delete(
123
            '/ccd/disable',
124 View Code Duplication
            function (Request $request, UserInfoInterface $userInfo) {
0 ignored issues
show
Unused Code introduced by
The parameter $userInfo 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...
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...
125
                $commonName = $request->getUrl()->getQueryParameter('common_name');
126
                Utils::validateCommonName($commonName);
127
128
                // $this->logInfo('enabling cn', array('api_user' => $userInfo->getUserId(), 'cn' => $commonName));
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
129
130
                $response = new JsonResponse();
131
                $response->setBody(
132
                    array(
133
                        'ok' => $this->staticConfig->enableCommonName($commonName),
134
                    )
135
                );
136
137
                return $response;
138
            }
139
        );
140
141
        $service->get(
142
            '/ccd/disable',
143 View Code Duplication
            function (Request $request, UserInfoInterface $userInfo) {
0 ignored issues
show
Unused Code introduced by
The parameter $userInfo 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...
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...
144
                // we typically deal with CNs, not user IDs, but the part of 
145
                // the CN before the first '_' is considered the user ID
146
                $userId = $request->getUrl()->getQueryParameter('user_id');
147
                if (!is_null($userId)) {
148
                    Utils::validateUserId($userId);
149
                }
150
151
                $response = new JsonResponse();
152
                $response->setBody(
153
                    array(
154
                        'ok' => true,
155
                        'disabled' => $this->staticConfig->getDisabledCommonNames($userId),
156
                    )
157
                );
158
159
                return $response;
160
            }
161
        );
162
    }
163
}
164