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

ServerSocket::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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