Completed
Push — master ( 1d0faf...7a5aed )
by Kacper
05:34
created

StreamDecorator::getDecorated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php declare (strict_types = 1);
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Utils;
17
18
use React\Stream\DuplexStreamInterface;
19
use React\Stream\WritableStreamInterface;
20
21
abstract class StreamDecorator implements DuplexStreamInterface
22
{
23
    use BetterEmitter;
24
25
    private $_redirectors;
26
27
    /**
28
     * @var DuplexStreamInterface
29
     */
30
    protected $_decorated = null;
31
32
    /**
33
     * StreamDecorator constructor.
34
     * @param DuplexStreamInterface $decorated
35
     */
36 19
    public function __construct(DuplexStreamInterface $decorated = null)
37
    {
38 19
        if ($decorated !== null) {
39 19
            $this->exchangeStream($decorated);
40
        }
41 19
    }
42
43 1
    public function isReadable()
44
    {
45 1
        return $this->_decorated->isReadable();
46
    }
47
48 1
    public function pause()
49
    {
50 1
        $this->_decorated->pause();
51 1
    }
52
53 1
    public function resume()
54
    {
55 1
        $this->_decorated->resume();
56 1
    }
57
58 1
    public function pipe(WritableStreamInterface $destination, array $options = array())
59
    {
60 1
        $this->_decorated->pipe($destination, $options);
61 1
    }
62
63 1
    public function close()
64
    {
65 1
        $this->_decorated->close();
66 1
    }
67
68 1
    public function isWritable()
69
    {
70 1
        return $this->_decorated->isWritable();
71
    }
72
73 1
    public function write($data)
74
    {
75 1
        return $this->_decorated->write($data);
76
    }
77
78 1
    public function end($data = null)
79
    {
80 1
        return $this->_decorated->end($data);
81
    }
82
83 19
    public function exchangeStream(DuplexStreamInterface $stream)
84
    {
85 19
        static $events = ['data', 'end', 'drain', 'error', 'close', 'pipe'];
86
87 19
        if ($this->_decorated !== null) {
88 6
            $this->unsubscribe($this->_decorated, $events);
89
        }
90
91 19
        $this->subscribe($stream, $events);
92 19
        $this->_decorated = $stream;
93 19
    }
94
95 6
    private function unsubscribe(DuplexStreamInterface $stream, array $events)
96
    {
97 6
        foreach ($events as $event) {
98 6
            if (!isset($this->_redirectors[$event])) {
99
                continue;
100
            }
101
102 6
            $stream->removeListener($event, $this->_redirectors[$event]);
103
        }
104 6
    }
105
106 19
    private function subscribe(DuplexStreamInterface $stream, array $events)
107
    {
108 19
        foreach ($events as $event) {
109 19
            if (!isset($this->_redirectors[$event])) {
110 11
                $this->_redirectors[$event] = function (...$arguments) use ($event) {
111 11
                    $this->emit($event, $arguments);
112 11
                };
113
            }
114
115 19
            $stream->on($event, $this->_redirectors[$event]);
116
        }
117 19
    }
118
119
    public function getDecorated() : DuplexStreamInterface
120
    {
121
        return $this->_decorated;
122
    }
123
}
124