LimitStream::seek()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 11
nc 4
nop 2
crap 5
1
<?php
2
3
namespace Thruster\Component\HttpMessage;
4
5
use Psr\Http\Message\StreamInterface;
6
7
/**
8
 * Class LimitStream
9
 *
10
 * @package Thruster\Component\HttpMessage
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class LimitStream implements StreamInterface
14
{
15
    use StreamDecoratorTrait;
16
17
    /**
18
     * @var int Offset to start reading from
19
     */
20
    private $offset;
21
22
    /**
23
     * @var int Limit the number of bytes that can be read
24
     */
25
    private $limit;
26
27
    /**
28
     * @param StreamInterface $stream Stream to wrap
29
     * @param int             $limit  Total number of bytes to allow to be read
30
     *                                from the stream. Pass -1 for no limit.
31
     * @param int|null        $offset Position to seek to before reading (only
32
     *                                works on seekable streams).
33
     */
34 17
    public function __construct(
35
        StreamInterface $stream,
36
        $limit = -1,
37
        $offset = 0
38
    ) {
39 17
        $this->stream = $stream;
0 ignored issues
show
Bug introduced by
The property stream does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40 17
        $this->setLimit($limit);
41 17
        $this->setOffset($offset);
42 17
    }
43
44 9
    public function eof()
45
    {
46
        // Always return true if the underlying stream is EOF
47 9
        if ($this->stream->eof()) {
48 2
            return true;
49
        }
50
51
        // No limit and the underlying stream is not at EOF
52 9
        if ($this->limit == -1) {
53 3
            return false;
54
        }
55
56 6
        return $this->stream->tell() >= $this->offset + $this->limit;
57
    }
58
59
    /**
60
     * Returns the size of the limited subset of data
61
     * {@inheritdoc}
62
     */
63 3
    public function getSize()
64
    {
65 3
        if (null === ($length = $this->stream->getSize())) {
66 1
            return null;
67 2
        } elseif (-1 == $this->limit) {
68 1
            return $length - $this->offset;
69
        } else {
70 1
            return min($this->limit, $length - $this->offset);
71
        }
72
    }
73
74
    /**
75
     * Allow for a bounded seek on the read limited stream
76
     * {@inheritdoc}
77
     */
78 6
    public function seek($offset, $whence = SEEK_SET)
79
    {
80 6
        if (SEEK_SET !== $whence || 0 > $offset) {
81 1
            throw new \RuntimeException(sprintf(
82 1
                'Cannot seek to offset % with whence %s',
83
                $offset,
84
                $whence
85
            ));
86
        }
87
88 6
        $offset += $this->offset;
89
90 6
        if (-1 !== $this->limit) {
91 4
            if ($offset > $this->offset + $this->limit) {
92 1
                $offset = $this->offset + $this->limit;
93
            }
94
        }
95
96 6
        $this->stream->seek($offset);
97 6
    }
98
99
    /**
100
     * Give a relative tell()
101
     * {@inheritdoc}
102
     */
103 3
    public function tell()
104
    {
105 3
        return $this->stream->tell() - $this->offset;
106
    }
107
108
    /**
109
     * Set the offset to start limiting from
110
     *
111
     * @param int $offset Offset to seek to and begin byte limiting from
112
     *
113
     * @throws \RuntimeException if the stream cannot be seeked.
114
     */
115 17
    public function setOffset($offset)
116
    {
117 17
        $current = $this->stream->tell();
118
119 17
        if ($current !== $offset) {
120
            // If the stream cannot seek to the offset position, then read to it
121 16
            if ($this->stream->isSeekable()) {
122 16
                $this->stream->seek($offset);
123 1
            } elseif ($current > $offset) {
124 1
                throw new \RuntimeException("Could not seek to stream offset $offset");
125
            } else {
126
                $this->stream->read($offset - $current);
127
            }
128
        }
129
130 17
        $this->offset = $offset;
131 17
    }
132
133
    /**
134
     * Set the limit of bytes that the decorator allows to be read from the
135
     * stream.
136
     *
137
     * @param int $limit Number of bytes to allow to be read from the stream.
138
     *                   Use -1 for no limit.
139
     */
140 17
    public function setLimit($limit)
141
    {
142 17
        $this->limit = $limit;
143 17
    }
144
145 9
    public function read($length)
146
    {
147 9
        if ($this->limit == -1) {
148 3
            return $this->stream->read($length);
149
        }
150
151
        // Check if the current position is less than the total allowed
152
        // bytes + original offset
153 6
        $remaining = ($this->offset + $this->limit) - $this->stream->tell();
154 6
        if ($remaining > 0) {
155
            // Only return the amount of requested data, ensuring that the byte
156
            // limit is not exceeded
157 6
            return $this->stream->read(min($remaining, $length));
158
        }
159
160 1
        return '';
161
    }
162
}
163