Passed
Branch master (e090ea)
by kacper
04:22
created

Socket   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 53.66%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 101
ccs 22
cts 41
cp 0.5366
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 7 2
A isConnected() 0 4 1
A connectToStream() 0 19 3
A getSocketErrorMessage() 0 4 1
A getSocketErrorCode() 0 4 1
A readFromSocket() 0 17 3
A writeToSocket() 0 9 2
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(
52
                $this->getSocketErrorMessage(),
53
                $this->getSocketErrorCode()
54
            );
55
        }
56 54
    }
57
58
    /**
59
     * @return string
60
     */
61
    private function getSocketErrorMessage()
62
    {
63
        return socket_strerror($this->getSocketErrorCode());
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    private function getSocketErrorCode()
70
    {
71
        return socket_last_error();
72
    }
73
74
    /**
75
     * @param int $length
76
     * @return string
77
     * @throws SocketException
78
     */
79 54
    public function readFromSocket($length)
80
    {
81 54
        $received = socket_recv($this->socket, $buf, $length, MSG_WAITALL);
82 54
        if ($length === $received) {
83 54
            return $buf;
84
        }
85
86
        // http://php.net/manual/pl/function.socket-recv.php#47182
87
        if (0 === $received) {
88
            throw new SocketException(
89
                SocketException::SOCKET_DISCONNECTED_MESSAGE,
90
                SocketException::SOCKET_DISCONNECTED_CODE
91
            );
92
        }
93
94
        throw new SocketException(socket_strerror($this->getSocketErrorCode()), $this->getSocketErrorCode());
95
    }
96
97
    /**
98
     * @param string $data
99
     * @throws SocketException
100
     */
101 54
    public function writeToSocket($data)
102
    {
103 54
        if (!socket_write($this->socket, $data, strlen($data))) {
104
            throw new SocketException(
105
                SocketException::SOCKET_UNABLE_TO_WRITE_MESSAGE . $this->getSocketErrorMessage(),
106
                SocketException::SOCKET_UNABLE_TO_WRITE_CODE
107
            );
108
        }
109
    }
110
}