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.

ManagementSocket::isEndOfResponse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
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\OpenVpn;
11
12
use SURFnet\VPN\Server\OpenVpn\Exception\ManagementSocketException;
13
14
/**
15
 * Abstraction to use the OpenVPN management interface using a socket
16
 * connection.
17
 */
18
class ManagementSocket implements ManagementSocketInterface
19
{
20
    /** @var resource|null */
21
    private $socket;
22
23
    public function __construct()
24
    {
25
        $this->socket = null;
26
    }
27
28
    /**
29
     * Connect to an OpenVPN management socket.
30
     *
31
     * @param string $socketAddress the socket to connect to, e.g.:
32
     *                              "tcp://localhost:7505"
33
     */
34
    public function open($socketAddress, $timeOut = 5)
35
    {
36
        $this->socket = @stream_socket_client($socketAddress, $errno, $errstr, $timeOut);
37
        if (false === $this->socket) {
38
            throw new ManagementSocketException(
39
                sprintf('%s (%s)', $errstr, $errno)
40
            );
41
        }
42
        // turn off logging as the output may interfere with our management
43
        // session, we do not care about the output
44
        $this->command('log off');
45
    }
46
47
    /**
48
     * Send an OpenVPN command and get the response.
49
     *
50
     * @param string $command a OpenVPN management command and parameters
51
     *
52
     * @return array the response lines as array values
53
     *
54
     * @throws Exception\ServerSocketException in case read/write fails or
55
     *                                         socket is not open
56
     */
57
    public function command($command)
58
    {
59
        $this->requireOpenSocket();
60
        $this->write(
61
            sprintf("%s\n", $command)
62
        );
63
64
        return $this->read();
65
    }
66
67
    public function close()
68
    {
69
        $this->requireOpenSocket();
70
        if (false === @fclose($this->socket)) {
71
            throw new ManagementSocketException('unable to close the socket');
72
        }
73
    }
74
75
    private function write($data)
76
    {
77
        if (false === @fwrite($this->socket, $data)) {
78
            throw new ManagementSocketException('unable to write to socket');
79
        }
80
    }
81
82
    private function read()
83
    {
84
        $dataBuffer = [];
85
        while (!feof($this->socket) && !$this->isEndOfResponse(end($dataBuffer))) {
86
            if (false === $readData = @fgets($this->socket, 4096)) {
87
                throw new ManagementSocketException('unable to read from socket');
88
            }
89
            $dataBuffer[] = trim($readData);
90
        }
91
92
        return $dataBuffer;
93
    }
94
95
    private function isEndOfResponse($lastLine)
96
    {
97
        $endMarkers = ['END', 'SUCCESS: ', 'ERROR: '];
98
        foreach ($endMarkers as $endMarker) {
99
            if (0 === mb_strpos($lastLine, $endMarker)) {
100
                return true;
101
            }
102
        }
103
104
        return false;
105
    }
106
107
    private function requireOpenSocket()
108
    {
109
        if (is_null($this->socket)) {
110
            throw new ManagementSocketException('socket not open');
111
        }
112
    }
113
}
114