Socket   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 63.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 99
ccs 19
cts 30
cp 0.6333
rs 10

7 Methods

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