Completed
Pull Request — master (#30)
by Tobias
08:49 queued 07:36
created

Stream::read()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 21
ccs 7
cts 7
cp 1
rs 9.0534
cc 4
eloc 10
nc 4
nop 1
crap 4
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
        try {
63
            return $this->getContents();
64
        } catch (\Exception $e) {
65
            return '';
66
        }
67
    }
68 5
69
    /**
70 5
     * {@inheritdoc}
71 5
     */
72
    public function close()
73
    {
74
        fclose($this->socket);
75
    }
76 1
77
    /**
78 1
     * {@inheritdoc}
79 1
     */
80 1
    public function detach()
81
    {
82 1
        $this->isDetached = true;
83
        $socket = $this->socket;
84
        $this->socket = null;
85
86
        return $socket;
87
    }
88 58
89
    /**
90 58
     * {@inheritdoc}
91
     */
92
    public function getSize()
93
    {
94
        return $this->size;
95
    }
96 1
97
    /**
98 1
     * {@inheritdoc}
99
     */
100
    public function tell()
101
    {
102
        return ftell($this->socket);
103
    }
104 1
105
    /**
106 1
     * {@inheritdoc}
107
     */
108
    public function eof()
109
    {
110
        return feof($this->socket);
111
    }
112 1
113
    /**
114 1
     * {@inheritdoc}
115
     */
116
    public function isSeekable()
117
    {
118
        return false;
119
    }
120 1
121
    /**
122 1
     * {@inheritdoc}
123
     */
124
    public function seek($offset, $whence = SEEK_SET)
125
    {
126
        throw new StreamException('This stream is not seekable');
127
    }
128 1
129
    /**
130 1
     * {@inheritdoc}
131
     */
132
    public function rewind()
133
    {
134
        throw new StreamException('This stream is not seekable');
135
    }
136 1
137
    /**
138 1
     * {@inheritdoc}
139
     */
140
    public function isWritable()
141
    {
142
        return false;
143
    }
144 1
145
    /**
146 1
     * {@inheritdoc}
147
     */
148
    public function write($string)
149
    {
150
        throw new StreamException('This stream is not writable');
151
    }
152 1
153
    /**
154 1
     * {@inheritdoc}
155
     */
156
    public function isReadable()
157
    {
158
        return true;
159
    }
160 5
161
    /**
162 5
     * {@inheritdoc}
163
     */
164
    public function read($length)
165
    {
166 5
        if (null === $this->getSize()) {
167 1
            return fread($this->socket, $length);
168
        }
169
170
        if ($this->getSize() === $this->readed) {
171 5
            return '';
172
        }
173 5
174 1
        // Even if we request a length a non blocking stream can return less data than asked
175
        $read = fread($this->socket, $length);
176
177 4
        if ($this->getMetadata('timed_out')) {
178
            throw new TimeoutException('Stream timed out while reading data');
179 4
        }
180
181
        $this->readed += strlen($read);
182
183
        return $read;
184
    }
185 57
186
    /**
187 57
     * {@inheritdoc}
188 53
     */
189
    public function getContents()
190
    {
191 4
        if (null === $this->getSize()) {
192
            return stream_get_contents($this->socket);
193
        }
194 4
195 3
        $contents = '';
196
197 3
        do {
198
            $contents .= $this->read($this->getSize() - $this->readed);
199
        } while ($this->readed < $this->getSize());
200
201
        return $contents;
202
    }
203 6
204
    /**
205 6
     * {@inheritdoc}
206
     */
207 6
    public function getMetadata($key = null)
208
    {
209
        $meta = stream_get_meta_data($this->socket);
210
211 6
        if (null === $key) {
212
            return $meta;
213
        }
214
215
        return $meta[$key];
216
    }
217
}
218