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

ChunkSplitStream::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
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
 * Inserts line ending characters after the set number of characters have been
14
 * written to the underlying stream.
15
 *
16
 * @author Zaahid Bateson
17
 */
18
class ChunkSplitStream implements StreamInterface
19
{
20
    use StreamDecoratorTrait;
21
22
    /**
23
     * @var int Number of bytes written, and importantly, if non-zero, writes a
24
     *      final $lineEnding on close (and so maintained instead of using
25
     *      tell() directly)
26
     */
27
    private $position;
28
29
    /**
30
     * @var int The number of characters in a line before inserting $lineEnding.
31
     */
32
    private $lineLength;
33
34
    /**
35
     * @var string The line ending characters to insert.
36
     */
37
    private $lineEnding;
38
39
    /**
40
     * @var int The strlen() of $lineEnding
41
     */
42
    private $lineEndingLength;
43
44
    /**
45
     * @param StreamInterface $stream
46
     * @param int $lineLength
47
     * @param string $lineEnding
48
     */
49 2
    public function __construct(StreamInterface $stream, $lineLength = 76, $lineEnding = "\r\n")
50
    {
51 2
        $this->stream = $stream;
52 2
        $this->lineLength = $lineLength;
53 2
        $this->lineEnding = $lineEnding;
54 2
        $this->lineEndingLength = strlen($this->lineEnding);
55 2
    }
56
57
    /**
58
     * Inserts the line ending character after each line length characters in
59
     * the passed string, making sure previously written bytes are taken into
60
     * account.
61
     *
62
     * @param string $string
63
     * @return string
64
     */
65 2
    private function getChunkedString($string)
66
    {
67 2
        $firstLine = '';
68 2
        if ($this->tell() !== 0) {
69 2
            $next = $this->lineLength - ($this->position % ($this->lineLength + $this->lineEndingLength));
70 2
            if (strlen($string) > $next) {
71 2
                $firstLine = substr($string, 0, $next) . $this->lineEnding;
72 2
                $string = substr($string, $next);
73
            }
74
        }
75
        // chunk_split always ends with the passed line ending
76 2
        $chunked = $firstLine . chunk_split($string, $this->lineLength, $this->lineEnding);
77 2
        return substr($chunked, 0, strlen($chunked) - $this->lineEndingLength);
78
    }
79
80
    /**
81
     * Writes the passed string to the underlying stream, ensuring line endings
82
     * are inserted every "line length" characters in the string.
83
     *
84
     * @param string $string
85
     * @return number of bytes written
86
     */
87 2
    public function write($string)
88
    {
89 2
        $chunked = $this->getChunkedString($string);
90 2
        $this->position += strlen($chunked);
91 2
        return $this->stream->write($chunked);
92
    }
93
94
    /**
95
     * Inserts a final line ending character.
96
     */
97 2
    private function beforeClose()
98
    {
99 2
        if ($this->position !== 0) {
100 2
            $this->stream->write($this->lineEnding);
101
        }
102 2
    }
103
104
    /**
105
     * Closes the stream after ensuring a final line ending character is
106
     * inserted.
107
     */
108 2
    public function close()
109
    {
110 2
        $this->beforeClose();
111 2
        $this->stream->close();
112 2
    }
113
114
    /**
115
     * Detaches the stream after ensuring a final line ending character is
116
     * inserted.
117
     */
118
    public function detach()
119
    {
120
        $this->beforeClose();
121
        $this->stream->detach();
122
    }
123
}
124