Completed
Push — master ( 68cdb6...83696f )
by Robin
02:27
created

Socket::gets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Rvdv\Nntp\Socket;
4
5
use Rvdv\Nntp\Exception\SocketException;
6
7
/**
8
 * @author Robin van der Vleuten <[email protected]>
9
 */
10
class Socket implements SocketInterface
11
{
12
    /**
13
     * @var resource
14
     */
15
    private $stream;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function enableCrypto($enable, $cryptoType = STREAM_CRYPTO_METHOD_TLS_CLIENT)
21
    {
22
        if (!stream_socket_enable_crypto($this->stream, $enable, $cryptoType)) {
23
            throw new SocketException();
24
        }
25
26
        return $this;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function connect($address)
33
    {
34 2
        if (!$this->stream = @stream_socket_client($address, $errno, $errstr, 1.0, STREAM_CLIENT_CONNECT)) {
35 1
            throw new SocketException(sprintf('Connection to %s failed: %s', $address, $errstr));
36
        }
37
38 1
        stream_set_blocking($this->stream, 1);
39
40
        // Use unbuffered read operations on the underlying stream resource.
41
        // Reading chunks from the stream may otherwise leave unread bytes in
42
        // PHP's stream buffers which some event loop implementations do not
43
        // trigger events on (edge triggered).
44
        // This does not affect the default event loop implementation (level
45
        // triggered), so we can ignore platforms not supporting this (HHVM).
46 1
        if (function_exists('stream_set_read_buffer')) {
47 1
            stream_set_read_buffer($this->stream, 0);
48 1
        }
49
50 1
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public function disconnect()
57
    {
58 1
        if (!fclose($this->stream)) {
59
            throw new SocketException();
60
        }
61
62 1
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function eof()
69
    {
70 1
        return feof($this->stream);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function gets($length = null)
77
    {
78
        if (($data = fgets($this->stream, $length)) === false) {
79
            throw new SocketException();
80
        }
81
82
        return $data;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 1
    public function read($length)
89
    {
90 1
        if (($data = fread($this->stream, $length)) === false) {
91
            throw new SocketException();
92
        }
93
94 1
        return $data;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 1
    public function write($data)
101
    {
102 1
        if (($bytes = fwrite($this->stream, $data)) === false) {
103
            throw new SocketException();
104
        }
105
106 1
        return $bytes;
107
    }
108
}
109