Passed
Push — master ( 80ac3a...2487f9 )
by kacper
05:49
created

Socket::writeToSocket()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 2
cts 4
cp 0.5
crap 2.5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace MySQLReplication\Socket;
5
6
/**
7
 * Class Socket
8
 * @package MySQLReplication\Socket
9
 */
10
class Socket implements SocketInterface
11
{
12
    /**
13
     * @var resource
14
     */
15
    private $socket;
16
17 54
    public function __destruct()
18
    {
19 54
        if ($this->isConnected()) {
20 54
            socket_shutdown($this->socket);
21 54
            socket_close($this->socket);
22 54
        }
23 54
    }
24
25
    /**
26
     * @return bool
27
     */
28 54
    public function isConnected()
29
    {
30 54
        return is_resource($this->socket);
31
    }
32
33
    /**
34
     * @param string $host
35
     * @param int $port
36
     * @throws SocketException
37
     */
38 54
    public function connectToStream($host, $port)
39
    {
40 54
        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
41 54
        if (!$this->socket) {
42
            throw new SocketException(
43
                SocketException::SOCKET_UNABLE_TO_CREATE_MESSAGE . $this->getSocketErrorMessage(),
44
                SocketException::SOCKET_UNABLE_TO_CREATE_CODE
45
            );
46
        }
47 54
        socket_set_block($this->socket);
48 54
        socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
49
50 54
        if (!socket_connect($this->socket, $host, $port)) {
51
            throw new SocketException($this->getSocketErrorMessage(), $this->getSocketErrorCode());
52
        }
53 54
    }
54
55
    /**
56
     * @return string
57
     */
58
    private function getSocketErrorMessage()
59
    {
60
        return socket_strerror($this->getSocketErrorCode());
61
    }
62
63
    /**
64
     * @return int
65
     */
66
    private function getSocketErrorCode()
67
    {
68
        return socket_last_error();
69
    }
70
71
    /**
72
     * @param int $length
73
     * @return string
74
     * @throws SocketException
75
     */
76 54
    public function readFromSocket($length)
77
    {
78 54
        $received = socket_recv($this->socket, $buf, $length, MSG_WAITALL);
79 54
        if ($length === $received) {
80 54
            return $buf;
81
        }
82
83
        // http://php.net/manual/en/function.socket-recv.php#47182
84
        if (0 === $received) {
85
            throw new SocketException(
86
                SocketException::SOCKET_DISCONNECTED_MESSAGE,
87
                SocketException::SOCKET_DISCONNECTED_CODE
88
            );
89
        }
90
91
        throw new SocketException($this->getSocketErrorMessage(), $this->getSocketErrorCode());
92
    }
93
94
    /**
95
     * @param string $data
96
     * @throws SocketException
97
     */
98 54
    public function writeToSocket($data)
99
    {
100 54
        if (!socket_write($this->socket, $data, strlen($data))) {
101
            throw new SocketException(
102
                SocketException::SOCKET_UNABLE_TO_WRITE_MESSAGE . $this->getSocketErrorMessage(),
103
                SocketException::SOCKET_UNABLE_TO_WRITE_CODE
104
            );
105
        }
106
    }
107
}