ConnectionHandler::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace mpyw\HyperBuiltinServer\Internal;
4
use mpyw\HyperBuiltinServer\Master;
5
use React\Socket\ConnectionInterface;
6
use React\Stream\Stream;
7
use React\Promise\Deferred;
8
9
class ConnectionHandler
10
{
11
    protected $master;
12
13
    public function __construct(Master $master)
14
    {
15
        $this->master = $master;
16
    }
17
18
    public function __invoke(ConnectionInterface $conn)
19
    {
20
        $sink = new Sink($conn);
21
        $this->master->queue->executeAsync(function (BuiltinServer $process) use ($conn, $sink) {
22
            $deferred = new Deferred;
23
            try {
24
                $child = new Stream($process->getSocketClient(), $this->master->loop);
25
                $sink->pipe($child);
26
                $child->pipe($conn);
27
                $child->on('close', function () use ($deferred) {
28
                    $deferred->resolve();
29
                });
30
            } catch (\RuntimeException $e) {
31
                $conn->write("HTTP/1.0 502 Bad Gateway\r\n");
32
                $conn->end("\r\n");
33
                $deferred->resolve();
34
            }
35
            return $deferred->promise();
36
        });
37
    }
38
}
39