Psr7Stream::isSeekable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: tamaskovacs
5
 * Date: 2017. 01. 27.
6
 * Time: 21:45
7
 */
8
9
namespace Callisto;
10
11
12
use Psr\Http\Message\StreamInterface;
13
14
abstract class Psr7Stream implements StreamInterface
15
{
16
	/**
17
	 * Connection resource.
18
	 *
19
	 * @var resource
20
	 */
21
	protected $connection;
22
23
24
	/**
25
	 * @throws \Exception
26
	 */
27 1
	public function __toString()
28
	{
29 1
		throw new \RuntimeException('Cannot read the entire stream.');
30
	}
31
32
	/**
33
	 * Closes the stream and any underlying resources.
34
	 *
35
	 * @return void
36
	 */
37 3
	public function close() : void
38
	{
39 3
		fclose($this->connection);
40 3
	}
41
42
	/**
43
	 * Separates any underlying resources from the stream.
44
	 *
45
	 * After the stream has been detached, the stream is in an unusable state.
46
	 *
47
	 * @return resource|null Underlying PHP stream, if any
48
	 */
49 1
	public function detach()
50
	{
51 1
		$this->close();
52 1
		return null;
53
	}
54
55
	/**
56
	 * We do not know the size of the stream.
57
	 * @return void
58
	 * @throws \RuntimeException
59
	 */
60 1
	public function getSize() : void
61
	{
62 1
		throw new \RuntimeException('We no not know the size of the stream.');
63
	}
64
65
	/**
66
	 * We do not have the position of the cursor.
67
	 *
68
	 * @return void
69
	 * @throws \RuntimeException on error.
70
	 */
71 1
	public function tell() : void
72
	{
73 1
		throw new \RuntimeException('We do not know the position of the cursor.');
74
	}
75
76
	/**
77
	 * Returns true if the stream is at the end of the stream.
78
	 *
79
	 * @return bool
80
	 */
81 9
	public function eof() : bool
82
	{
83 9
		return feof($this->connection);
84
	}
85
86
	/**
87
	 * The stream is not seekable.
88
	 *
89
	 * @return bool
90
	 */
91 1
	public function isSeekable() : bool
92
	{
93 1
		return false;
94
	}
95
96
	/**
97
	 * The stream is not seekable.
98
	 *
99
	 * @throws \RuntimeException on failure.
100
	 */
101 1
	public function seek($offset, $whence = SEEK_SET) : void
102
	{
103 1
		throw new \RuntimeException('The stream is not seekable.');
104
	}
105
106
	/**
107
	 * The stream cannot be rewind.
108
	 *
109
	 * @throws \RuntimeException on failure.
110
	 */
111 1
	public function rewind() : void
112
	{
113 1
		throw new \RuntimeException('The stream is not seekable therefore it cannot be rewind.');
114
	}
115
116
	/**
117
	 * Returns whether or not the stream is writable.
118
	 *
119
	 * @return bool
120
	 */
121 1
	public function isWritable() : bool
122
	{
123 1
		return true;
124
	}
125
126
	/**
127
	 * Write data to the stream.
128
	 *
129
	 * @param string $string The string that is to be written.
130
	 * @return int Returns the number of bytes written to the stream.
131
	 * @throws \RuntimeException on failure.
132
	 */
133 9
	public function write($string) : int
134
	{
135 9
		$length = strlen($string);
136 9
		fwrite($this->connection, $string, $length);
137
138 9
		return $length;
139
	}
140
141
	/**
142
	 * Returns whether or not the stream is readable.
143
	 *
144
	 * @return bool
145
	 */
146 1
	public function isReadable() : bool
147
	{
148 1
		return true;
149
	}
150
151
	/**
152
	 * Read data from the stream.
153
	 *
154
	 * @param int $length Read up to $length bytes from the object and return
155
	 *     them. Fewer than $length bytes may be returned if underlying stream
156
	 *     call returns fewer bytes.
157
	 * @return string Returns the data read from the stream, or an empty string
158
	 *     if no bytes are available.
159
	 * @throws \RuntimeException if an error occurs.
160
	 */
161 3
	public function read($length)
162
	{
163 3
		return fread($this->connection, $length);
164
	}
165
166
	/**
167
	 * Reads the next line fron the stream.
168
	 *
169
	 * @param int $length Read up to $length bytes from the object and return
170
	 *     them. Fewer than $length bytes may be returned if underlying stream
171
	 *     call returns fewer bytes.
172
	 * @return string Returns the data read from the stream, or an empty string
173
	 *     if no bytes are available.
174
	 * @throws \RuntimeException if an error occurs.
175
	 */
176 9
	public function readLine($length = 1024) : string
177
	{
178 9
		return (string)fgets($this->connection, $length);
179
	}
180
181
	/**
182
	 * Returns the remaining contents in a string
183
	 *
184
	 * @return string
185
	 * @throws \RuntimeException if unable to read or an error occurs while
186
	 *     reading.
187
	 */
188 1
	public function getContents()
189
	{
190 1
		throw new \RuntimeException('We cannot return the remaining contents of the stream.');
191
	}
192
193
	/**
194
	 * Get stream metadata as an associative array or retrieve a specific key.
195
	 *
196
	 * The keys returned are identical to the keys returned from PHP's
197
	 * stream_get_meta_data() function.
198
	 *
199
	 * @link http://php.net/manual/en/function.stream-get-meta-data.php
200
	 * @param string|null $key Specific metadata to retrieve.
201
	 * @return array|mixed|null Returns an associative array if no key is
202
	 *     provided. Returns a specific key value if a key is provided and the
203
	 *     value is found, or null if the key is not found.
204
	 */
205 1
	public function getMetadata($key = null)
206
	{
207 1
		return stream_get_meta_data($this->connection);
208
	}
209
}