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 ( 12b4db...b4b56f )
by François
02:27
created

Pool::getRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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;
19
20
use RuntimeException;
21
22
class Pool
23
{
24
    /** @var int */
25
    private $id;
26
27
    /** @var string */
28
    private $poolName;
29
30
    /** @var string */
31
    private $hostName;
32
33
    /** @var bool */
34
    private $defaultGateway;
35
36
    /** @var IP */
37
    private $range;
38
39
    /** @var IP */
40
    private $range6;
41
42
    /** @var array */
43
    private $routes;
44
45
    /** @var array */
46
    private $dns;
47
48
    /** @var bool */
49
    private $useNat;
50
51
    /** @var string */
52
    private $extIf;
53
54
    /** @var bool */
55
    private $twoFactor;
56
57
    /** @var bool */
58
    private $clientToClient;
59
60
    /** @var IP */
61
    private $managementIp;
62
63
    /** @var IP */
64
    private $listen;
65
66
    /** @var array */
67
    private $instances;
68
69
    public function __construct($poolNumber, array $poolData)
70
    {
71
        $this->setId(self::validate($poolData, 'id'));
72
        $this->setName(self::validate($poolData, 'name', false, $this->getId()));
0 ignored issues
show
Documentation introduced by
$this->getId() is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
        $this->setHostName(self::validate($poolData, 'hostName'));
74
        $this->setDefaultGateway(self::validate($poolData, 'defaultGateway', false, false));
75
        $this->setRange(new IP(self::validate($poolData, 'range')));
76
        $this->setRange6(new IP(self::validate($poolData, 'range6')));
77
        $this->setRoutes(self::validate($poolData, 'routes', false, []));
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        $this->setDns(self::validate($poolData, 'dns', false, []));
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
        $this->setUseNat(self::validate($poolData, 'useNat', false, false));
80
        $this->setExtIf(self::validate($poolData, 'extIf'));
81
        $this->setTwoFactor(self::validate($poolData, 'twoFactor', false, false));
82
        $this->setClientToClient(self::validate($poolData, 'clientToClient', false, false));
83
        $this->setManagementIp(new IP(sprintf('127.42.%d.1', $poolNumber)));
84
        $this->setListen(new IP(self::validate($poolData, 'listen', false, '::')));
0 ignored issues
show
Documentation introduced by
'::' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
86
        $this->populateInstances();
87
    }
88
89
    public function setId($id)
90
    {
91
        // XXX validate
92
        $this->id = $id;
93
    }
94
95
    public function getId()
96
    {
97
        return $this->id;
98
    }
99
100
    public function setName($poolName)
101
    {
102
        // XXX validate
103
        $this->poolName = $poolName;
104
    }
105
106
    public function getName()
107
    {
108
        return $this->poolName;
109
    }
110
111
    public function setHostName($hostName)
112
    {
113
        // XXX validate
114
        $this->hostName = $hostName;
115
    }
116
117
    public function getHostName()
118
    {
119
        return $this->hostName;
120
    }
121
122
    public function setDefaultGateway($defaultGateway)
123
    {
124
        $this->defaultGateway = (bool) $defaultGateway;
125
    }
126
127
    public function getDefaultGateway()
128
    {
129
        return $this->defaultGateway;
130
    }
131
132
    public function setRange(IP $range)
133
    {
134
        $this->range = $range;
135
    }
136
137
    public function getRange()
138
    {
139
        return $this->range;
140
    }
141
142
    public function setRange6(IP $range6)
143
    {
144
        $this->range6 = $range6;
145
    }
146
147
    public function getRange6()
148
    {
149
        return $this->range6;
150
    }
151
152
    public function setRoutes(array $routes)
153
    {
154
        $this->routes = [];
155
156
        foreach ($routes as $route) {
157
            $this->routes[] = new IP($route);
158
        }
159
    }
160
161
    public function getRoutes()
162
    {
163
        return $this->routes;
164
    }
165
166
    public function setDns(array $dns)
167
    {
168
        $this->dns = [];
169
170
        foreach ($dns as $server) {
171
            $this->dns[] = new IP($server);
172
        }
173
    }
174
175
    public function getDns()
176
    {
177
        return $this->dns;
178
    }
179
180
    public function setUseNat($useNat)
181
    {
182
        $this->useNat = (bool) $useNat;
183
    }
184
185
    public function getUseNat()
186
    {
187
        return $this->useNat;
188
    }
189
190
    public function setExtIf($extIf)
191
    {
192
        // XXX validate
193
        $this->extIf = $extIf;
194
    }
195
196
    public function getExtIf()
197
    {
198
        return $this->extIf;
199
    }
200
201
    public function setTwoFactor($twoFactor)
202
    {
203
        $this->twoFactor = (bool) $twoFactor;
204
    }
205
206
    public function getTwoFactor()
207
    {
208
        return $this->twoFactor;
209
    }
210
211
    public function setClientToClient($clientToClient)
212
    {
213
        $this->clientToClient = (bool) $clientToClient;
214
    }
215
216
    public function getClientToClient()
217
    {
218
        return $this->clientToClient;
219
    }
220
221
    public function setManagementIp(IP $managementIp)
222
    {
223
        $this->managementIp = $managementIp;
224
    }
225
226
    public function getManagementIp()
227
    {
228
        return $this->managementIp;
229
    }
230
231
    public function setListen(IP $listen)
232
    {
233
        $this->listen = $listen;
234
    }
235
236
    public function getListen()
237
    {
238
        return $this->listen;
239
    }
240
241
    public function getInstances()
242
    {
243
        return $this->instances;
244
    }
245
246
    private function populateInstances()
247
    {
248
        $instanceCount = self::getNetCount($this->getRange()->getPrefix());
249
        $splitRange = $this->getRange()->split($instanceCount);
250
        $splitRange6 = $this->getRange6()->split($instanceCount);
251
252
        for ($i = 0; $i < $instanceCount; ++$i) {
253
            // protocol is udp unless it is the last instance when there is
254
            // not just one instance
255
            if (1 !== $instanceCount && $i !== $instanceCount - 1) {
256
                $proto = 'udp';
257
                $port = 1194 + $i;
258
            } else {
259
                $proto = 'tcp';
260
                $port = 1194;
261
            }
262
263
            $this->instances[] = new Instance(
264
                [
265
                    'range' => $splitRange[$i],
266
                    'range6' => $splitRange6[$i],
267
                    'dev' => sprintf('tun-%s-%d', $this->getId(), $i),
268
                    'proto' => $proto,
269
                    'port' => $port,
270
                    'managementPort' => 11940 + $i,
271
                ]
272
            );
273
        }
274
    }
275
276
    /**
277
     * Depending on the prefix we will divide it in a number of nets to 
278
     * balance the load over the instances, it is recommended to use a least
279
     * a /24.
280
     * 
281
     * A /24 or 'bigger' will be split in 4 networks, everything 'smaller'  
282
     * will be either be split in 2 networks or remain 1 network.
283
     */
284
    private static function getNetCount($prefix)
285
    {
286
        switch ($prefix) {
287
            case 32:    // 1 IP   
288
            case 31:    // 2 IPs
289
                throw new RuntimeException('not enough available IPs in range');
290
            case 30:    // 4 IPs
291
                return 1;
292
            case 29:    // 8 IPs
293
            case 28:    // 16 IPs
294
            case 27:    // 32 IPs
295
            case 26:    // 64 IPs
296
            case 25:    // 128 IPs
297
                return 2;
298
            case 24:
299
                return 4;
300
        }
301
302
        return 8;
303
    }
304
305
    public function toArray()
306
    {
307
        $routesList = [];
308
        foreach ($this->getRoutes() as $route) {
309
            $routesList[] = $route->getAddressPrefix();
310
        }
311
312
        $dnsList = [];
313
        foreach ($this->getDns() as $dns) {
314
            $dnsList[] = $dns->getAddress();
315
        }
316
317
        $instancesList = [];
318
        foreach ($this->getInstances() as $instance) {
319
            $instancesList[] = $instance->toArray();
320
        }
321
322
        return [
323
            'clientToClient' => $this->getClientToClient(),
324
            'defaultGateway' => $this->getDefaultGateway(),
325
            'dns' => $dnsList,
326
            'extIf' => $this->getExtIf(),
327
            'hostName' => $this->getHostName(),
328
            'id' => $this->getId(),
329
            'instances' => $instancesList,
330
            'listen' => $this->getListen()->getAddress(),
331
            'managementIp' => $this->getManagementIp()->getAddress(),
332
            'name' => $this->getName(),
333
            'range' => $this->getRange()->getAddressPrefix(),
334
            'range6' => $this->getRange6()->getAddressPrefix(),
335
            'routes' => $routesList,
336
            'twoFactor' => $this->getTwoFactor(),
337
            'useNat' => $this->getUseNat(),
338
        ];
339
    }
340
341
    private static function validate(array $configData, $configName, $requiredField = true, $defaultValue = false)
342
    {
343
        if (!array_key_exists($configName, $configData)) {
344
            if ($requiredField) {
345
                throw new RuntimeException(sprintf('missing configuration field "%s"', $configName));
346
            }
347
348
            return $defaultValue;
349
        }
350
351
        return $configData[$configName];
352
    }
353
}
354