Completed
Push — master ( ea4b32...aca53b )
by Joel
16s
created

Stream::read()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 21
ccs 8
cts 10
cp 0.8
rs 9.0534
cc 4
eloc 10
nc 4
nop 1
crap 4.128
1
<?php
2
3
namespace Http\Client\Socket;
4
5
use Http\Client\Socket\Exception\StreamException;
6
use Http\Client\Socket\Exception\TimeoutException;
7
use Psr\Http\Message\StreamInterface;
8
9
/**
10
 * Stream implementation for Socket Client.
11
 *
12
 * This implementation is used to have a Stream which react better to the Socket Client behavior.
13
 *
14
 * The main advantage is you can get the response of a request even if it's not finish, the response is available
15
 * as soon as all headers are received, this stream will have the remaining socket used for the request / response
16
 * call.
17
 *
18
 * It is only readable once, if you want to read the content multiple times, you can store contents of this
19
 * stream into a variable or encapsulate it in a buffered stream.
20
 *
21
 * Writing and seeking is disable to avoid weird behaviors.
22
 *
23
 * @author Joel Wurtz <[email protected]>
24
 */
25
class Stream implements StreamInterface
26
{
27
    /** @var resource Underlying socket */
28
    private $socket;
29
30
    /**
31
     * @var bool Is stream detached
32
     */
33
    private $isDetached = false;
34
35
    /**
36
     * @var int|null Size of the stream, so we know what we must read, null if not available (i.e. a chunked stream)
37
     */
38
    private $size;
39
40
    /**
41
     * @var int Size of the stream readed, to avoid reading more than available and have the user blocked
42
     */
43
    private $readed = 0;
44
45
    /**
46
     * Create the stream.
47
     *
48
     * @param resource $socket
49
     * @param int      $size
50
     */
51 71
    public function __construct($socket, $size = null)
52
    {
53 71
        $this->socket = $socket;
54 71
        $this->size = $size;
55 71
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function __toString()
61
    {
62 1
        return (string) $this->getContents();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 5
    public function close()
69
    {
70 5
        fclose($this->socket);
71 5
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function detach()
77
    {
78 1
        $this->isDetached = true;
79 1
        $socket = $this->socket;
80 1
        $this->socket = null;
81
82 1
        return $socket;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 58
    public function getSize()
89
    {
90 58
        return $this->size;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 1
    public function tell()
97
    {
98 1
        return ftell($this->socket);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function eof()
105
    {
106 1
        return feof($this->socket);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 1
    public function isSeekable()
113
    {
114 1
        return false;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 1
    public function seek($offset, $whence = SEEK_SET)
121
    {
122 1
        throw new StreamException('This stream is not seekable');
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 1
    public function rewind()
129
    {
130 1
        throw new StreamException('This stream is not seekable');
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 1
    public function isWritable()
137
    {
138 1
        return false;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 1
    public function write($string)
145
    {
146 1
        throw new StreamException('This stream is not writable');
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 1
    public function isReadable()
153
    {
154 1
        return true;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 4
    public function read($length)
161
    {
162 4
        if (null === $this->getSize()) {
163
            return fread($this->socket, $length);
164
        }
165
166 4
        if ($this->getSize() === $this->readed) {
167 1
            return '';
168
        }
169
170
        // Even if we request a length a non blocking stream can return less data than asked
171 4
        $read = fread($this->socket, $length);
172
173 4
        if ($this->getMetadata('timed_out')) {
174
            throw new TimeoutException('Stream timed out while reading data');
175
        }
176
177 4
        $this->readed += strlen($read);
178
179 4
        return $read;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 57
    public function getContents()
186
    {
187 57
        if (null === $this->getSize()) {
188 54
            return stream_get_contents($this->socket);
189
        }
190
191 3
        $contents = '';
192
193
        do {
194 3
            $contents .= $this->read($this->getSize() - $this->readed);
195 3
        } while ($this->readed < $this->getSize());
196
197 3
        return $contents;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203 5
    public function getMetadata($key = null)
204
    {
205 5
        $meta = stream_get_meta_data($this->socket);
206
207 5
        if (null === $key) {
208
            return $meta;
209
        }
210
211 5
        return $meta[$key];
212
    }
213
}
214