Connection::handleData()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 8.2222
cc 7
eloc 6
nc 4
nop 1
crap 7
1
<?php
2
3
namespace Thruster\Component\Socket;
4
5
use Thruster\Component\Stream\Stream;
6
7
/**
8
 * Class Connection
9
 *
10
 * @package Thruster\Component\Socket
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class Connection extends Stream implements ConnectionInterface
14
{
15
    /**
16
     * @param resource $stream
17
     */
18 9
    public function handleData($stream)
19
    {
20 9
        $data = stream_socket_recvfrom($stream, $this->bufferSize);
21 9
        if ('' !== $data && false !== $data) {
22 5
            $this->emit('data', array($data, $this));
23
        }
24
25 9
        if ('' === $data || false === $data || !is_resource($stream) || feof($stream)) {
26 7
            $this->end();
27
        }
28 9
    }
29
30 7
    public function handleClose()
31
    {
32 7
        if (is_resource($this->stream)) {
33 7
            stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
34 7
            stream_set_blocking($this->stream, false);
35
36 7
            fclose($this->stream);
37
        }
38 7
    }
39
40 1
    public function getRemoteAddress() : string
41
    {
42 1
        return $this->parseAddress(stream_socket_get_name($this->stream, true));
43
    }
44
45 4
    public function parseAddress(string $address) : string
46
    {
47 4
        return trim(substr($address, 0, strrpos($address, ':')), '[]');
48
    }
49
}
50