Passed
Push — master ( 8c77bf...be291e )
by Zaahid
04:42
created

NonClosingStream::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the ZBateson\StreamDecorators project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\StreamDecorators;
8
9
use Psr\Http\Message\StreamInterface;
10
use GuzzleHttp\Psr7\StreamDecoratorTrait;
11
12
/**
13
 * Doesn't close the underlying stream when 'close' is called on it.  Instead,
14
 * calling close simply removes any reference to the underlying stream.  Note
15
 * that GuzzleHttp\Psr7\Stream calls close in __destruct, so a reference to the
16
 * Stream needs to be kept.  For example:
17
 *
18
 * ```
19
 * $f = fopen('php://temp', 'r+');
20
 * $test = new NonClosingStream(Psr7\stream_for('test'));
21
 * // work
22
 * $test->close();
23
 * rewind($f);      // error, $f is a closed resource
24
 * ```
25
 *
26
 * Instead, this would work:
27
 *
28
 * ```
29
 * $stream = Psr7\stream_for(fopen('php://temp', 'r+'));
30
 * $test = new NonClosingStream($stream);
31
 * // work
32
 * $test->close();
33
 * $stream->rewind();  // works
34
 * ```
35
 *
36
 * @author Zaahid Bateson
37
 */
38
class NonClosingStream implements StreamInterface
39
{
40
    use StreamDecoratorTrait;
41
42
    /**
43
     * Overridden to detach the underlying stream without closing it.
44
     */
45 1
    public function close()
46
    {
47 1
        $this->stream = null;
48 1
    }
49
50
    /**
51
     * Overridden to detach the underlying stream without closing it.
52
     */
53 1
    public function detach()
54
    {
55 1
        $this->stream = null;
56 1
    }
57
}
58