Sink   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 4
A pipe() 0 11 3
1
<?php
2
3
namespace mpyw\HyperBuiltinServer\Internal;
4
use React\Socket\ConnectionInterface;
5
use React\Stream\WritableStreamInterface;
6
7
class Sink
8
{
9
    protected $src;
10
    protected $dst;
11
    protected $buffer = '';
12
    protected $end = false;
13
14
    public function __construct(ConnectionInterface $src)
15
    {
16
        $this->src = $src;
17
        $src->on('data', function ($data) {
18
            $this->buffer .= $data;
19
            if ($this->dst) {
20
                $this->dst->write($this->buffer);
21
                $this->buffer = '';
22
            }
23
        });
24
        $src->on('end', function () {
25
            $this->end = true;
26
            if ($this->dst) {
27
                $this->dst->end();
28
            }
29
        });
30
        $src->on('error', function () {
31
            $this->end = true;
32
            if ($this->dst) {
33
                $this->dst->end();
34
            }
35
        });
36
    }
37
38
    public function pipe(WritableStreamInterface $dst)
39
    {
40
        $this->dst = $dst;
41
        if ($this->buffer !== '') {
42
            $this->dst->write($this->buffer);
43
            $this->buffer = '';
44
        }
45
        if ($this->end) {
46
            $this->dst->end();
47
        }
48
    }
49
}
50