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 ( 737b3f...ca0726 )
by François
05:21
created

ServerApi::version()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 15
loc 15
rs 9.4286
cc 2
eloc 10
nc 5
nop 0
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;
18
19
use fkooman\VPN\Server\Exception\ServerSocketException;
20
21
/**
22
 * Higher level abstraction of ServerSocket providing a cleaner API that 
23
 * performs some post processing making it easier for applications to use.
24
 */
25
class ServerApi
26
{
27
    /** @var string */
28
    private $id;
29
30
    /** @var SocketInterface */
31
    private $serverSocket;
32
33
    public function __construct($id, ServerSocketInterface $serverSocket)
34
    {
35
        $this->id = $id;
36
        $this->serverSocket = $serverSocket;
0 ignored issues
show
Documentation Bug introduced by
It seems like $serverSocket of type object<fkooman\VPN\Server\ServerSocketInterface> is incompatible with the declared type object<fkooman\VPN\Server\SocketInterface> of property $serverSocket.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
    }
38
39
    public function getId()
40
    {
41
        return $this->id;
42
    }
43
44
    /**
45
     * Obtain information about connected clients.
46
     *
47
     * @return array information about the connected clients
48
     */
49
    public function status()
50
    {
51
        try {
52
            $this->serverSocket->open();
53
            $response = $this->serverSocket->command('status 2');
54
            $this->serverSocket->close();
55
56
            return $this->ok('status', StatusParser::parse($response));
57
        } catch (ServerSocketException $e) {
58
            return $this->error();
59
        }
60
    }
61
62
    /**
63
     * Obtain OpenVPN version information.
64
     * 
65
     * @return string the OpenVPN version string
66
     */
67 View Code Duplication
    public function version()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
68
    {
69
        try {
70
            $this->serverSocket->open();
71
            $response = $this->serverSocket->command('version');
72
            $this->serverSocket->close();
73
74
            return $this->ok(
75
                'version',
76
                substr($response[0], strlen('OpenVPN Version: '))
77
            );
78
        } catch (ServerSocketException $e) {
79
            return $this->error();
80
        }
81
    }
82
83
    /**
84
     * Obtain the current load statistics from OpenVPN.
85
     * 
86
     * @return array load statistics
87
     */
88
    public function loadStats()
89
    {
90
        try {
91
            $keyMapping = array(
92
                'nclients' => 'number_of_clients',
93
                'bytesin' => 'bytes_in',
94
                'bytesout' => 'bytes_out',
95
            );
96
97
            $this->serverSocket->open();
98
            $response = $this->serverSocket->command('load-stats');
99
            $this->serverSocket->close();
100
101
            $statArray = explode(',', substr($response[0], strlen('SUCCESS: ')));
102
            $loadStats = array();
103
            foreach ($statArray as $statItem) {
104
                list($key, $value) = explode('=', $statItem);
105
                if (array_key_exists($key, $keyMapping)) {
106
                    $loadStats[$keyMapping[$key]] = intval($value);
107
                }
108
            }
109
110
            return $this->ok('load-stats', $loadStats);
111
        } catch (ServerSocketException $e) {
112
            return $this->error();
113
        }
114
    }
115
116
    /**
117
     * Disconnect a client.
118
     *
119
     * @param string $commonName the common name of the connection to kill
120
     */
121 View Code Duplication
    public function kill($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
122
    {
123
        try {
124
            $this->serverSocket->open();
125
            $response = $this->serverSocket->command(sprintf('kill %s', $commonName));
126
            $this->serverSocket->close();
127
128
            return $this->ok('kill', 0 === strpos($response[0], 'SUCCESS: '));
129
        } catch (ServerSocketException $e) {
130
            return $this->error();
131
        }
132
    }
133
134
    private function ok($command, $response)
135
    {
136
        return array(
137
            'id' => $this->getId(),
138
            'ok' => true,
139
            $command => $response,
140
        );
141
    }
142
143
    private function error()
144
    {
145
        return array(
146
            'id' => $this->getId(),
147
            'ok' => false,
148
        );
149
    }
150
}
151