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.

TestSocket   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 18.31 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 1
dl 13
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A open() 0 7 2
B command() 13 17 5
A close() 0 4 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
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Server\Tests;
11
12
use SURFnet\VPN\Server\OpenVpn\Exception\ManagementSocketException;
13
use SURFnet\VPN\Server\OpenVpn\ManagementSocketInterface;
14
15
/**
16
 * Abstraction to use the OpenVPN management interface using a socket
17
 * connection.
18
 */
19
class TestSocket implements ManagementSocketInterface
20
{
21
    /** @var bool */
22
    private $connectFail;
23
24
    /** @var string|null */
25
    private $socketAddress;
26
27
    public function __construct($connectFail = false)
28
    {
29
        $this->connectFail = $connectFail;
30
        $this->socketAddress = null;
31
    }
32
33
    /**
34
     * Open the socket.
35
     *
36
     * @param string $socketAddress the socket to connect to, e.g.:
37
     *                              "tcp://localhost:7505"
38
     * @param int    $timeOut       the amount of time to wait before
39
     *                              giving up on trying to connect
40
     *
41
     * @throws \SURFnet\VPN\Server\OpenVpn\Exception\ServerSocketException if the socket cannot be opened
42
     *                                                                     within timeout
43
     */
44
    public function open($socketAddress, $timeOut = 5)
45
    {
46
        $this->socketAddress = $socketAddress;
47
        if ($this->connectFail) {
48
            throw new ManagementSocketException('unable to connect to socket');
49
        }
50
    }
51
52
    /**
53
     * Send an OpenVPN command and get the response.
54
     *
55
     * @param string $command a OpenVPN management command and parameters
56
     *
57
     * @return array the response lines as array values
58
     *
59
     * @throws \SURFnet\VPN\Server\OpenVpn\Exception\ServerSocketException in case read/write fails or
60
     *                                                                     socket is not open
61
     */
62
    public function command($command)
63
    {
64
        if ('status 2' === $command) {
65 View Code Duplication
            if ('tcp://127.0.0.1:11940' === $this->socketAddress) {
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...
66
                // send back the returnData as an array
67
                return explode("\n", file_get_contents(__DIR__.'/data/socket/status_with_clients.txt'));
68
            } else {
69
                return explode("\n", file_get_contents(__DIR__.'/data/socket/status_no_clients.txt'));
70
            }
71 View Code Duplication
        } elseif ('kill' === $command) {
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...
72
            if ('tcp://127.0.0.1:11940' === $this->socketAddress) {
73
                return explode("\n", file_get_contents(__DIR__.'/data/socket/kill_success.txt'));
74
            } else {
75
                return explode("\n", file_get_contents(__DIR__.'/data/socket/kill_error.txt'));
76
            }
77
        }
78
    }
79
80
    /**
81
     * Close the socket connection.
82
     *
83
     * @throws \SURFnet\VPN\Server\OpenVpn\Exception\ServerSocketException if socket is not open
84
     */
85
    public function close()
86
    {
87
        $this->socketAddress = null;
88
    }
89
}
90