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.
Passed
Push — master ( 5cefd1...492078 )
by Anton
04:08
created

UnixServer::getAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
nc 2
nop 0
dl 0
loc 7
c 1
b 0
f 0
cc 2
rs 10
1
<?php
2
3
namespace React\Socket;
4
5
use Evenement\EventEmitter;
6
use React\EventLoop\Loop;
7
use React\EventLoop\LoopInterface;
8
use InvalidArgumentException;
9
use RuntimeException;
10
11
/**
12
 * The `UnixServer` class implements the `ServerInterface` and
13
 * is responsible for accepting plaintext connections on unix domain sockets.
14
 *
15
 * ```php
16
 * $server = new React\Socket\UnixServer('unix:///tmp/app.sock');
17
 * ```
18
 *
19
 * See also the `ServerInterface` for more details.
20
 *
21
 * @see ServerInterface
22
 * @see ConnectionInterface
23
 */
24
final class UnixServer extends EventEmitter implements ServerInterface
25
{
26
    private $master;
27
    private $loop;
28
    private $listening = false;
29
30
    /**
31
     * Creates a plaintext socket server and starts listening on the given unix socket
32
     *
33
     * This starts accepting new incoming connections on the given address.
34
     * See also the `connection event` documented in the `ServerInterface`
35
     * for more details.
36
     *
37
     * ```php
38
     * $server = new React\Socket\UnixServer('unix:///tmp/app.sock');
39
     * ```
40
     *
41
     * This class takes an optional `LoopInterface|null $loop` parameter that can be used to
42
     * pass the event loop instance to use for this object. You can use a `null` value
43
     * here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
44
     * This value SHOULD NOT be given unless you're sure you want to explicitly use a
45
     * given event loop instance.
46
     *
47
     * @param string         $path
48
     * @param ?LoopInterface $loop
49
     * @param array          $context
50
     * @throws InvalidArgumentException if the listening address is invalid
51
     * @throws RuntimeException if listening on this address fails (already in use etc.)
52
     */
53
    public function __construct($path, LoopInterface $loop = null, array $context = array())
54
    {
55
        $this->loop = $loop ?: Loop::get();
56
57
        if (\strpos($path, '://') === false) {
58
            $path = 'unix://' . $path;
59
        } elseif (\substr($path, 0, 7) !== 'unix://') {
60
            throw new \InvalidArgumentException(
61
                'Given URI "' . $path . '" is invalid (EINVAL)',
62
                \defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
63
            );
64
        }
65
66
        $this->master = @\stream_socket_server(
67
            $path,
68
            $errno,
69
            $errstr,
70
            \STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN,
71
            \stream_context_create(array('socket' => $context))
72
        );
73
        if (false === $this->master) {
74
            // PHP does not seem to report errno/errstr for Unix domain sockets (UDS) right now.
75
            // This only applies to UDS server sockets, see also https://3v4l.org/NAhpr.
76
            // Parse PHP warning message containing unknown error, HHVM reports proper info at least.
77
            if ($errno === 0 && $errstr === '') {
78
                $error = \error_get_last();
79
                if (\preg_match('/\(([^\)]+)\)|\[(\d+)\]: (.*)/', $error['message'], $match)) {
80
                    $errstr = isset($match[3]) ? $match['3'] : $match[1];
81
                    $errno = isset($match[2]) ? (int)$match[2] : 0;
82
                }
83
            }
84
85
            throw new \RuntimeException(
86
                'Failed to listen on Unix domain socket "' . $path . '": ' . $errstr . SocketServer::errconst($errno),
87
                $errno
88
            );
89
        }
90
        \stream_set_blocking($this->master, 0);
91
92
        $this->resume();
93
    }
94
95
    public function getAddress()
96
    {
97
        if (!\is_resource($this->master)) {
98
            return null;
99
        }
100
101
        return 'unix://' . \stream_socket_get_name($this->master, false);
102
    }
103
104
    public function pause()
105
    {
106
        if (!$this->listening) {
107
            return;
108
        }
109
110
        $this->loop->removeReadStream($this->master);
111
        $this->listening = false;
112
    }
113
114
    public function resume()
115
    {
116
        if ($this->listening || !is_resource($this->master)) {
117
            return;
118
        }
119
120
        $that = $this;
121
        $this->loop->addReadStream($this->master, function ($master) use ($that) {
122
            try {
123
                $newSocket = SocketServer::accept($master);
124
            } catch (\RuntimeException $e) {
125
                $that->emit('error', array($e));
126
                return;
127
            }
128
            $that->handleConnection($newSocket);
129
        });
130
        $this->listening = true;
131
    }
132
133
    public function close()
134
    {
135
        if (!\is_resource($this->master)) {
136
            return;
137
        }
138
139
        $this->pause();
140
        \fclose($this->master);
141
        $this->removeAllListeners();
142
    }
143
144
    /** @internal */
145
    public function handleConnection($socket)
146
    {
147
        $connection = new Connection($socket, $this->loop);
148
        $connection->unix = true;
149
150
        $this->emit('connection', array(
151
            $connection
152
        ));
153
    }
154
}
155