Completed
Push — master ( 7459b3...a2b5cf )
by Tobias
02:26
created

Stream::getContents()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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