BufferedSink   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 57
ccs 27
cts 27
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A handlePipeEvent() 0 4 1
A handleErrorEvent() 0 4 1
A write() 0 4 1
A close() 0 10 2
A promise() 0 4 1
A createPromise() 0 8 1
1
<?php
2
3
namespace Thruster\Component\Stream;
4
5
use Thruster\Component\Promise\Deferred;
6
use Thruster\Component\Promise\PromiseInterface;
7
use Thruster\Component\Promise\PromisorInterface;
8
9
/**
10
 * Class BufferedSink
11
 *
12
 * @package Thruster\Component\Stream
13
 * @author  Aurimas Niekis <[email protected]>
14
 */
15
class BufferedSink extends WritableStream implements PromisorInterface
16
{
17
    use UtilsTrait;
18
19
    private $buffer = '';
20
    private $deferred;
21
22 11
    public function __construct()
23
    {
24 11
        $this->deferred = new Deferred();
25
26 11
        $this->on('pipe', [$this, 'handlePipeEvent']);
27 11
        $this->on('error', [$this, 'handleErrorEvent']);
28
29 11
        parent::__construct();
30 11
    }
31
32 3
    public function handlePipeEvent($source)
33
    {
34 3
        $this->forwardEvents($source, $this, ['error']);
35 3
    }
36
37 2
    public function handleErrorEvent($e)
38
    {
39 2
        $this->deferred->reject($e);
40 2
    }
41
42 6
    public function write($data)
43
    {
44 6
        $this->buffer .= $data;
45 6
    }
46
47 7
    public function close()
48
    {
49 7
        if (true === $this->closed) {
50 2
            return;
51
        }
52
53 7
        parent::close();
54
55 7
        $this->deferred->resolve($this->buffer);
56 7
    }
57
58 11
    public function promise() : PromiseInterface
59
    {
60 11
        return $this->deferred->promise();
61
    }
62
63 1
    public static function createPromise(ReadableStreamInterface $stream) : PromiseInterface
64
    {
65 1
        $sink = new static();
66
67 1
        $stream->pipe($sink);
68
69 1
        return $sink->promise();
70
    }
71
}
72