ConnectionHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 20 2
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