Buffer   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.34%
Metric Value
wmc 22
lcom 1
cbo 2
dl 0
loc 224
ccs 69
cts 79
cp 0.8734
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getSoftLimit() 0 4 1
A isListening() 0 4 1
A isWritable() 0 4 1
A __construct() 0 17 1
A setSoftLimit() 0 6 1
A setListening() 0 6 1
A write() 0 18 3
A end() 0 14 3
A close() 0 10 1
C handleWrite() 0 49 8
A errorHandler() 0 7 1
1
<?php
2
3
namespace Thruster\Component\Stream;
4
5
use Thruster\Component\EventEmitter\EventEmitterInterface;
6
use Thruster\Component\EventEmitter\EventEmitterTrait;
7
use Thruster\Component\EventLoop\EventLoopInterface;
8
9
/**
10
 * Class Buffer
11
 *
12
 * @package Thruster\Component\Stream
13
 * @author  Aurimas Niekis <[email protected]>
14
 */
15
class Buffer implements WritableStreamInterface
16
{
17
    use EventEmitterTrait;
18
19
    /**
20
     * @var resource
21
     */
22
    protected $stream;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $listening;
28
29
    /**
30
     * @var int
31
     */
32
    protected $softLimit;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $writable;
38
39
    /**
40
     * @var EventLoopInterface
41
     */
42
    protected $loop;
43
44
    /**
45
     * @var string
46
     */
47
    protected $data;
48
49
    /**
50
     * @var array
51
     */
52
    protected $lastError;
53
54
    /**
55
     * Buffer constructor.
56
     *
57
     * @param resource           $stream
58
     * @param EventLoopInterface $loop
59
     * @param int                $softLimit
60
     */
61 18
    public function __construct($stream, EventLoopInterface $loop, int $softLimit = 2048)
62
    {
63 18
        $this->stream    = $stream;
64 18
        $this->loop      = $loop;
65 18
        $this->setSoftLimit($softLimit);
66
67 18
        $this->setListening(false);
68 18
        $this->writable  = true;
69 18
        $this->data      = '';
70
71 18
        $this->lastError = [
72
            'number'  => 0,
73
            'message' => '',
74
            'file'    => '',
75
            'line'    => 0,
76
        ];
77 18
    }
78
79
    /**
80
     * @return int
81
     */
82 9
    public function getSoftLimit() : int
83
    {
84 9
        return $this->softLimit;
85
    }
86
87
    /**
88
     * @param int $softLimit
89
     *
90
     * @return $this
91
     */
92 18
    public function setSoftLimit(int $softLimit) : self
93
    {
94 18
        $this->softLimit = $softLimit;
95
96 18
        return $this;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102 11
    public function isListening() : bool
103
    {
104 11
        return $this->listening;
105
    }
106
107
    /**
108
     * @param bool $listening
109
     *
110
     * @return $this
111
     */
112 18
    public function setListening(bool $listening) : self
113
    {
114 18
        $this->listening = $listening;
115
116 18
        return $this;
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122 2
    public function isWritable() : bool
123
    {
124 2
        return $this->writable;
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130 10
    public function write($data)
131
    {
132 10
        if (!$this->writable) {
133 1
            return;
134
        }
135
136 9
        $this->data .= $data;
137
138 9
        if (false === $this->isListening()) {
139 9
            $this->setListening(true);
140
141 9
            $this->loop->addWriteStream($this->stream, [$this, 'handleWrite']);
142
        }
143
144 9
        $belowSoftLimit = strlen($this->data) < $this->getSoftLimit();
145
146 9
        return $belowSoftLimit;
147
    }
148
149
    /**
150
     * @inheritdoc
151
     */
152 3
    public function end($data = null)
153
    {
154 3
        if (null !== $data) {
155 1
            $this->write($data);
156
        }
157
158 3
        $this->writable = false;
159
160 3
        if ($this->isListening()) {
161
            $this->on('full-drain', [$this, 'close']);
162
        } else {
163 3
            $this->close();
164
        }
165 3
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170 5
    public function close() : self
171
    {
172 5
        $this->writable  = false;
173 5
        $this->setListening(false);
174 5
        $this->data      = '';
175
176 5
        $this->emit('close', [$this]);
177
178 5
        return $this;
179
    }
180
181 8
    public function handleWrite()
182
    {
183 8
        if (!is_resource($this->stream)) {
184 1
            $this->emit('error', [new \RuntimeException('Tried to write to invalid stream.'), $this]);
185
186 1
            return;
187
        }
188
189 7
        set_error_handler([$this, 'errorHandler']);
190
191 7
        $sent = fwrite($this->stream, $this->data);
192
193 7
        restore_error_handler();
194
195 7
        if (false === $sent) {
196
            $this->emit('error', [
197
                new \ErrorException(
198
                    $this->lastError['message'],
199
                    0,
200
                    $this->lastError['number'],
201
                    $this->lastError['file'],
202
                    $this->lastError['line']
203
                ),
204
                $this
205
            ]);
206
207
            return;
208
        }
209
210 7
        if (0 === $sent && feof($this->stream)) {
211 2
            $this->emit('error', [new \RuntimeException('Tried to write to closed stream.'), $this]);
212
213 2
            return;
214
        }
215
216 6
        $len        = strlen($this->data);
217 6
        $this->data = (string)substr($this->data, $sent);
218
219 6
        if ($len >= $this->softLimit && $len - $sent < $this->getSoftLimit()) {
220 2
            $this->emit('drain', [$this]);
221
        }
222
223 6
        if (0 === strlen($this->data)) {
224 6
            $this->loop->removeWriteStream($this->stream);
225 6
            $this->setListening(false);
226
227 6
            $this->emit('full-drain', [$this]);
228
        }
229 6
    }
230
231 2
    protected function errorHandler($errno, $errstr, $errfile, $errline)
232
    {
233 2
        $this->lastError['number']  = $errno;
234 2
        $this->lastError['message'] = $errstr;
235 2
        $this->lastError['file']    = $errfile;
236 2
        $this->lastError['line']    = $errline;
237 2
    }
238
}
239