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 ( 046bfa...91fb1e )
by François
02:21
created

ConfigModule   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 145
Duplicated Lines 35.86 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 1
cbo 8
dl 52
loc 145
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B init() 52 130 3

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\Http\JsonResponse;
23
use Psr\Log\LoggerInterface;
24
use fkooman\VPN\Server\InputValidation;
25
26
class ConfigModule implements ServiceModuleInterface
27
{
28
    /** @var StaticConfig */
29
    private $staticConfig;
30
31
    /** @var \Psr\Log\LoggerInterface */
32
    private $logger;
33
34
    public function __construct(StaticConfig $staticConfig, LoggerInterface $logger)
35
    {
36
        $this->staticConfig = $staticConfig;
37
        $this->logger = $logger;
38
    }
39
40
    public function init(Service $service)
41
    {
42
        $service->get(
43
            '/static/ip',
44
           function (Request $request) {
45
                // we typically deal with CNs, not user IDs, but the part of 
46
                // the CN before the first '_' is considered the user ID
47
                $userId = InputValidation::userId(
48
                    $request->getUrl()->getQueryParameter('user_id'),
49
                    false // OPTIONAL
50
                );
51
                $commonName = InputValidation::commonName(
52
                    $request->getUrl()->getQueryParameter('common_name'),
53
                    false // OPTIONAL
54
                );
55
56
                if (!is_null($userId)) {
57
                    // per user
58
                    $ipConfig = $this->staticConfig->getStaticAddresses($userId);
59
                } elseif (!is_null($commonName)) {
60
                    // per CN
61
                    $ipConfig = $this->staticConfig->getStaticAddress($commonName);
62
                } else {
63
                    // all
64
                    $ipConfig = $this->staticConfig->getStaticAddresses();
65
                }
66
67
                $response = new JsonResponse();
68
                $response->setBody(
69
                    array(
70
                        'ok' => true,
71
                        'ip' => $ipConfig,
72
                        'ipRange' => $this->staticConfig->getIpRange()->getRange(),
73
                        'poolRange' => $this->staticConfig->getPoolRange()->getRange(),
74
                    )
75
                );
76
77
                return $response;
78
            }
79
        );
80
81
        $service->post(
82
            '/static/ip',
83
           function (Request $request) {
84
                $commonName = InputValidation::commonName(
85
                    $request->getPostParameter('common_name'),
86
                    true // REQUIRED
87
                );
88
                $v4 = InputValidation::v4(
89
                    $request->getPostParameter('v4'),
90
                    false // OPTIONAL
91
                );
92
93
                $response = new JsonResponse();
94
                $response->setBody(
95
                    array(
96
                        'ok' => $this->staticConfig->setStaticAddresses($commonName, $v4),
97
                    )
98
                );
99
100
                $this->logger->info('setting static IP', array('cn' => $commonName, 'v4' => $v4));
101
102
                return $response;
103
            }
104
        );
105
106
        $service->post(
107
            '/ccd/disable',
108 View Code Duplication
            function (Request $request) {
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...
109
                $commonName = InputValidation::commonName(
110
                    $request->getPostParameter('common_name'),
111
                    true // REQUIRED
112
                );
113
114
                $this->logger->info('disabling cn', array('cn' => $commonName));
115
116
                $response = new JsonResponse();
117
                $response->setBody(
118
                    array(
119
                        'ok' => $this->staticConfig->disableCommonName($commonName),
120
                    )
121
                );
122
123
                return $response;
124
            }
125
        );
126
127
        $service->delete(
128
            '/ccd/disable',
129 View Code Duplication
            function (Request $request) {
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...
130
                $commonName = InputValidation::commonName(
131
                    $request->getUrl()->getQueryParameter('common_name'),
132
                    true // REQUIRED
133
                );
134
135
                $this->logger->info('enabling cn', array('cn' => $commonName));
136
137
                $response = new JsonResponse();
138
                $response->setBody(
139
                    array(
140
                        'ok' => $this->staticConfig->enableCommonName($commonName),
141
                    )
142
                );
143
144
                return $response;
145
            }
146
        );
147
148
        $service->get(
149
            '/ccd/disable',
150 View Code Duplication
            function (Request $request) {
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...
151
                // we typically deal with CNs, not user IDs, but the part of 
152
                // the CN before the first '_' is considered the user ID
153
                $userId = InputValidation::userId(
154
                    $request->getUrl()->getQueryParameter('user_id'),
155
                    false // OPTIONAL
156
                );
157
158
                $response = new JsonResponse();
159
                $response->setBody(
160
                    array(
161
                        'ok' => true,
162
                        'disabled' => $this->staticConfig->getDisabledCommonNames($userId),
163
                    )
164
                );
165
166
                return $response;
167
            }
168
        );
169
    }
170
}
171