Completed
Push — 1.x ( ee8545...084efb )
by Joel
02:48
created

CallbackStream   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 44
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onFrame() 0 4 1
readFrame() 0 1 ?
A wait() 0 12 4
1
<?php
2
3
namespace Docker\Stream;
4
5
use Psr\Http\Message\StreamInterface;
6
7
abstract class CallbackStream
8
{
9
    protected $stream;
10
11
    private $onNewFrameCallables = [];
12
13
    public function __construct(StreamInterface $stream)
14
    {
15
        $this->stream = $stream;
16
    }
17
18
    /**
19
     * Called when there is a new frame from the stream
20
     *
21
     * @param callable $onNewFrame
22
     */
23
    public function onFrame(callable $onNewFrame)
24
    {
25
        $this->onNewFrameCallables[] = $onNewFrame;
26
    }
27
28
    /**
29
     * Read a frame in the stream
30
     *
31
     * @return mixed
32
     */
33
    abstract protected function readFrame();
34
35
    /**
36
     * Wait for stream to finish and call callables if defined
37
     */
38
    public function wait()
39
    {
40
        while (!$this->stream->eof()) {
41
            $frame = $this->readFrame();
42
43
            if ($frame !== null) {
44
                foreach ($this->onNewFrameCallables as $newFrameCallable) {
45
                    $newFrameCallable($frame);
46
                }
47
            }
48
        }
49
    }
50
}
51